C单链表实现


  1 /*
  2  * LinkNode.c
  3  *
  4  *  Created on: Jan 14, 2014
  5  *      Author: root
  6  */
  7 #include <stdlib.h>
  8 #include <stdio.h>
  9 #include <malloc.h>
 10 typedef struct node
 11 {
 12     char *data;
 13     struct node *next;
 14 }Node,*pNode;
 15 
 16 pNode create();
 17 void printList(pNode pHead);
 18 int InsertNode(pNode pHead,int front,char *data);
 19 char* DeleteNode(pNode pHead,int pos);
 20 int LengthList(pNode pHead);
 21 void Sort_List(pNode pHead);
 22 pNode Reverse_List(pNode pHead);
 23 int main()
 24 {
 25 
 26     pNode pHead = NULL;
 27     int front;
 28     int choose;
 29     char* return_val=NULL;
 30     pHead=create();
 31     printf("你输入的数据是,\n");
 32     int length;
 33     length = LengthList(pHead);
 34     printf("(长度为%d的单链表):\n",length);
 35     printList(pHead);
 36     while(1)
 37     {
 38     printf("是否还要进行如下操作:\n");
 39     printf("1.插入数据      2.删除数据     3.修改数据     4.链表排序   5.链表逆序   6.退出\n");
 40     scanf("%d",&choose);
 41     if(choose!=1&&choose!=2&&choose!=3&&choose!=4&&choose!=5)
 42     {
 43         printf("error:请输入正确的数字!\n");
 44         break;
 45     }
 46     switch(choose)
 47     {
 48         case 1:
 49         {
 50             printf("请输入要在第几个节点前插入数据:");
 51             scanf("%d",&front);
 52             if(front>length)
 53             {
 54                 front = length;
 55                 printf("Warning:输入结点位置大于链表长度,默认在表尾部添加!\n");
 56             }
 57             printf("请输入要插入的数据:");
 58             char *data = (char*)malloc(sizeof(char*));
 59             scanf("%s",data);
 60             if(InsertNode(pHead,front,data)==1)
 61             {
 62                  printf("插入成功\n插入后的数据是:\n");
 63                  printList(pHead);
 64             }else
 65             {
 66                  printf("插入失败\n");
 67             }
 68                 break;
 69         }
 70         case 2:
 71         {
 72             printf("请输入要删除第几个节点的数据:");
 73             scanf("%d",&front);
 74             if(front>length)
 75             {
 76                 front=length;
 77                 printf("Warning:输入结点位置大于链表长度,默认删除表最后一个结点!\n");
 78             }
 79             return_val = DeleteNode(pHead,front);
 80             if(return_val==NULL)
 81             {
 82                   printf("删除失败。\n");
 83             }else
 84             {
 85                  printf("删除成功。删除的元素是:%s\n",return_val);
 86             }
 87                  printf("操作完成后的数据是:\n");
 88                  printList(pHead);
 89                  break;
 90         }
 91         case 3:
 92         {
 93             printf("暂时没有做这个功能!\n");
 94             break;
 95         }
 96         case 4:
 97         {
 98             Sort_List(pHead);
 99             break;
100         }
101         case 5:
102         {
103             Reverse_List(pHead);
104             break;
105         }
106         case 6:
107         {
108             exit(1);
109         }
110         return 0;
111     }
112 
113     }
114 
115     return 0;
116 }
117 
118 pNode create()
119 {
120     int i;
121     int len;
122     pNode pHead = (pNode)malloc(sizeof(struct node));
123     pNode pTail = pHead;
124     pTail->next = NULL;
125     printf("请输入节点个数:");
126     scanf("%d",&len);
127     for(i=0;i<len;i++)
128     {
129         char *val=(char*)malloc(sizeof(char*));//Warning.malloc
130         printf("第 %d 个节点的数值:",i+1);
131         scanf("%s",val);
132         pNode pNew = (pNode)malloc(sizeof(Node));
133         pNew->data = val;
134         /*
135          *  有序的
136          *  1.将尾节点的next指针指向新节点
137          *  2.然后把新节点next指针设置为空
138          *  3. 最后将新节点作为尾节点
139          */
140         pTail->next=pNew;
141         pNew->next=NULL;
142         pTail = pNew;
143     }
144     printf("create list success!");
145     return pHead;
146 }
147 
148 
149 void printList(pNode pHead)
150 {
151     pNode p = pHead->next;
152     while(p!=NULL)
153     {
154         printf("%s\n",p->data);
155         p=p->next;
156     }
157     printf("\n");
158 }
159 int InsertNode(pNode pHead,int front,char *data)
160 {
161     int i = 0;
162     pNode _node = pHead;
163     pNode pSwap ;
164 
165     if((front<1) || (_node==NULL))
166     {
167         printf("error:List is NULL or front<1");
168         return -1;
169 
170     }
171     while(i<front-1)
172     {
173         _node = _node->next;
174         ++i;
175     }
176     pNode pNew = (pNode)malloc(sizeof(Node));
177 
178     pNew->data=data;
179     pSwap = _node->next;
180     _node->next = pNew;
181     pNew->next = pSwap;
182 
183     return 1;
184 }
185 
186 char* DeleteNode(pNode pHead,int pos)
187 {
188     int i=0;
189     char *data;
190     pNode _node =pHead;
191     pNode pSwap ;
192     if((pos < 1) && (NULL == _node->next))
193         {
194             printf("failed to delete ! \n");
195             return 0;
196         }
197     while(i < pos-1)
198     {
199         _node = _node->next;
200         ++i;
201     }
202     pSwap = _node->next;
203     data = pSwap->data;
204     _node->next = _node->next->next;
205     free(pSwap);
206     return data;
207 }
208 
209 int LengthList(pNode pHead)
210 {
211     int length = 0;
212     pNode temp = pHead->next;
213     while(temp!=NULL)
214     {
215         temp=temp->next;
216         length++;
217     }
218     return length;
219 }
220 
221 void Sort_List(pNode pHead)
222 {
223     int i , j;
224     char* swap ;
225     int len=LengthList(pHead);
226     pNode m,n;
227     for(i=0,m=pHead->next;i<len-1;i++,m=m->next)
228     {
229         for(j=i+1,n=m->next;j<len;j++,n=n->next)
230         {
231             if(m->data > n->data)
232             {
233                 swap = m->data;
234                 m->data = n->data;
235                 n->data = swap;
236             }
237         }
238     }
239     printf("排序完后结果为:\n");
240     printList(pHead);
241 }
242 pNode Reverse_List(pNode pHead)
243 {
244     if(pHead->next==NULL || pHead->next->next ==NULL)
245     {
246         return pHead;
247     }
248     pNode t=NULL;
249     pNode p=pHead->next;// first element
250     pNode q=pHead->next->next;//second element
251 
252     while(q!=NULL)
253     {
254         t = q->next;
255         q->next = p;
256         p=q;
257         q=t;
258     }
259     pHead->next->next = NULL;
260     pHead->next = p;
261     printf("逆序后结果为:\n");
262     printList(pHead);
263     return pHead;
264 
265 }

 


posted on 2014-01-23 14:39  IronMan_  阅读(558)  评论(0编辑  收藏  举报