码迷,mamicode.com
首页 > 其他好文 > 详细

单链表练习

时间:2014-07-30 23:13:45      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:des   io   for   ar   div   size   type   res   

//code:
#include
#include
using namespace std;
typedef int status;
typedef int elemType;
typedef struct lNode
{
elemType  num;
struct lNode *next;
}lNode,*linkNode;
 
linkNode initListNode();
void echNode(linkNode L);
linkNode insertNode(linkNode L);
void creatList(linkNode L,int n);
void echList(linkNode L);
linkNode delNode(linkNode L,int n);
linkNode searchNode(linkNode head,int n);
int main()
{
 
linkNode l,m;
l=initListNode();
printf("After Initialization:\n");
echNode(l);
printf("\nCreat and echo List:\n");
creatList(l,4);
echList(l);
printf("\nFind 2nd elemet:\n");
m=searchNode(l,2);
printf("Find result:\n");
echNode(m);
printf("\nDelt nodes and echo List\n");
m=delNode(l,1);
m=delNode(m,1);
echList(m);
}
 
linkNode initListNode()
{
linkNode L;
L=(linkNode)malloc(sizeof(lNode));
L->num=0;
L->next=NULL;
return L;
}
void creatList(linkNode L,int n)
{
int i;
for(i=0;i
{
L=insertNode(L);
L->num=i+1;//num value
}
}
 
void echNode(linkNode L)
{
printf("Node num is %d\n",L->num);
}
linkNode insertNode(linkNode L)
{
linkNode tmpp;
tmpp=(linkNode)malloc(sizeof(lNode));
//tmpp->num=9;
tmpp->next=NULL;
L->next=tmpp;
return tmpp;
}
 
void echList(linkNode L)
{
linkNode tmpp;
tmpp=L;
do
{
echNode(tmpp);
tmpp=tmpp->next;
}
while(tmpp);
}
 
linkNode searchNode(linkNode head,int n)
{
linkNode tmpp;
tmpp=head;
//printf("\nSearch %dnd element!!\n",n);
while(--n)
{
if(tmpp->next)
{
tmpp=tmpp->next;
}
else
{
//cout<<"Over max quantity of Nodes of the List";
exit(0) ;
}
}
//printf("The num is %d.\n\n",tmpp->num);
return tmpp;
}
 
linkNode delNode(linkNode head,int n)
{
linkNode tmpp,nodep;
if(n>1)
{
tmpp=searchNode(head,n-1);
nodep=tmpp->next;
tmpp->next=tmpp->next->next;
free(nodep);
return head;
}
else if(n=1)
{
tmpp=head->next;
free(head);
return tmpp;
}
}
result:
After Initialization:
Node num is 0
 
Creat and echo List:
Node num is 0
Node num is 1
Node num is 2
Node num is 3
 
Find 2nd elemet:
Find result:
Node num is 1
 
Delt nodes and echo List
Node num is 2
Node num is 3
请按任意键继续. . .

单链表练习,布布扣,bubuko.com

单链表练习

标签:des   io   for   ar   div   size   type   res   

原文地址:http://www.cnblogs.com/johvoo/p/3879116.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!