单链表的相关操作

单链表的创建及相关的操作.(单链表的创建,删除,插入,排序,逆置,测长)
第一步:创建单链表的节点

1 typedef struct Node
2 {
3     int     data;
4     struct  node* next;
5 }node,*pnode;//node = struct Node,pnode = struct node*

第二步:创建链表

 1 //创建一个新的链表,并返回头指针
 2 pnode create_list()
 3 {
 4     int data;
 5     pnode pHead = (pnode)malloc(sizeof(node));
 6     pnode pCurrent = pHead;
 7     pCurrent->next = NULL;
 8     if(NULL == pHead)
 9     {
10         printf("pHead malloc failed !\n");
11         exit(-1);
12     }
13     printf("Input data (q to quit):\n");
14     while(scanf("%d",&data)==1)
15     {
16         pnode pnew = (pnode)malloc(sizeof(node));
17         if(NULL == pnew)
18         {
19             printf("malloc failed!\n");
20             exit(-1);
21         }
22         pnew->data = data;
23         pCurrent->next = pnew;
24         pnew->next = NULL;
25         pCurrent = pnew;
26     }
27     return pHead;
28 }

第三步:遍历这个链表并打印

 1 //遍历链表
 2 void travel_list(pnode pHead)
 3 {
 4     pnode pCurrent = pHead->next;
 5     printf("the list are :\n");
 6     while(pCurrent != NULL)
 7     {
 8      printf("%d ",pCurrent->data);
 9      pCurrent = pCurrent->next;
10     }
11     printf("\n");
12 }

第四步:单链表的插入
在链表中插入你个节点,可能有四种情况
①在链表的中间位置插入
②在链表的起始位置插入
③在链表的尾部插入
④原链表为空,即插入到起始位置,有时结束位置

 1 //在链表的第i个位置插入元素value
 2 pNode insert(pNode pHead, int value, int i)
 3 {
 4     pNode pre = pHead;
 5     for (int k = 1; k < i;k++)
 6     {
 7         pre = pre->link;
 8     }
 9     pNode pnew = (pNode)malloc(sizeof(pNode));
10     pnew->data = value;
11     /*
12        pnew->link = pre->link;
13        pre->link  是当前节点,即让新建的节点指向当前节点
14        再让前驱结点指向新建的节点
15        等同于
16        pNode pcurrent;//当前节点
17        pnew->link = pcurrent;
18        pre->link = pnew;
19        */
20     pnew->link = pre->link;
21     pre->link = pnew;
22     return pHead;
23 }

第五步:删除
在单链表中删除值为value的节点

 1 pNode delete_list(pNode pHead, int value)
 2 {
 3     pNode pre = NULL;//pcurrent为目标结点
 4     pNode pcurrent = pHead->link;//pre前驱结点
 5     if (pHead==NULL)
 6     {
 7         return NULL;
 8     }
 9     while (pcurrent->data != value&&pcurrent->link != NULL)
10     {
11         pre = pcurrent;
12         pcurrent = pcurrent->link;
13     }
14     pre->link = pcurrent->link;//删除操作
15     free(pcurrent);
16     return pHead;
17 }

第六步:单链表的测长

 1 int length_list(pNode pHead)
 2 {
 3     pNode pcurrent = pHead->next;
 4     while(pcurrent != NULL)
 5     {
 6         count++;
 7         pcurrent = pcurrent->next;
 8     }
 9     return count;
10 }

第七步:对链表进行冒泡排序

 1 void bubblesort_list(pNode pHead)
 2 {
 3     pNode i,k;//声明两个临时变量,用于for循环
 4     int temp;
 5     for(i = pHead->next;i != NULL;i = i->next)
 6         for(k = i->next;k != NULL;k = k->next)
 7         {
 8             if(i->data > k->data)
 9             {
10                 temp = i->data;
11                 i->data = k->data;
12                 k->data = temp;
13             }
14         }
15     return ;
16 }

第八步:单链表的逆置(有很多种方法,递归,循环)

 1 pNode reverse2(pNode head)
 2 {
 3     pNode F,S,T;
 4     F = head;
 5     S = F->link;
 6     T = S->link;
 7     head->link = NULL;
 8     while(T!=NULL)
 9     {
10         S->link = F;
11         F = S;
12         S = T;
13         T = T->link;
14     }
15     S->link = F;
16     return S;
17 }

 

 

posted @ 2014-04-10 13:21  韦轩  阅读(258)  评论(0)    收藏  举报