链表的增删改查

头文件:linklist.h

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 
 4 struct LinkNode
 5 {
 6     int data;// 数据域
 7     struct LinkNode *pNext;// 指针域
 8 };
 9 
10 typedef struct LinkNode node;// 类型简化
11 
12 void init(node *phead);// 初始化
13 
14 node *addback(node *phead,int data);// 从尾部添加节点
15 
16 void addhead(node **pphead, int data);// 头部添加节点
17 
18 void showall(node *phead);// 显示
19 
20 void revshowall(node *phead);// 反转显示
21 
22 node *searchfirst(node *phead,int finddata);// 查找数据
23 
24 node *changefirst(node *phead, int finddata,int newdata);// 修改查找 
25 
26 node *deletefirst(node *phead,int finddata);// 删除
27 
28 node *insertfirst(node*phead, int finddata, int newdata);//插入

方法实现:linknode.c

  1 #include"linklist.h"
  2 
  3 void init(node *phead)// 初始化
  4 {
  5     phead->data = 0;
  6     phead->pNext = NULL;
  7 }
  8 
  9 //尾插,改变一个指针,需要指针的地址,用返回值给指针赋值
 10 node *addback(node *phead, int data)// 从尾部添加节点
 11 {
 12     node *pnew = malloc(sizeof(node));// 给新节点分配内存
 13     
 14     pnew->data = data;// 初始化新节点
 15     pnew->pNext = NULL;
 16 
 17     if (phead==NULL)
 18     {
 19         phead = pnew;
 20     }
 21     else
 22     {
 23         node *ptemp = phead;// 备份头节点
 24         while (ptemp->pNext!=NULL)// 找到最后一个节点
 25         {
 26             ptemp = ptemp->pNext;// 循环
 27         }
 28         ptemp->pNext = pnew;// 链接
 29     }
 30 
 31     return phead;
 32 }
 33 
 34 // 头插
 35 void addhead(node **pphead, int data)// 头部添加节点
 36 {
 37     node *pnew = malloc(sizeof(node));
 38     pnew->data = data;
 39     pnew->pNext = NULL;
 40 
 41     if (*pphead==NULL)
 42     {
 43         *pphead = pnew;// 直接链接
 44     }
 45 
 46     else
 47     {
 48         pnew->pNext = *pphead;
 49         *pphead = pnew;// pnew  成为第一个节点
 50     }
 51 
 52 }
 53 
 54 void showall(node *phead)// 显示
 55 {
 56     if (phead==NULL)
 57     {
 58         return;
 59     }
 60     else
 61     {
 62         printf("%d  %p  %p\n",phead->data,phead,phead->pNext);
 63         showall(phead->pNext);//跳到下一个节点
 64     }
 65 
 66 
 67 
 68     /*
 69     for (node *newphead = phead; newphead != NULL; newphead = newphead->pNext)
 70     {
 71         printf("%d  %p  %p\n", newphead->data,newphead,newphead->pNext);
 72     }
 73     */
 74 
 75 }
 76 
 77 void revshowall(node *phead)// 反转显示
 78 {
 79 
 80     if (phead == NULL)
 81     {
 82         return;
 83     }
 84     else
 85     {
 86         revshowall(phead->pNext);//跳到下一个节点
 87         printf("%d  %p  %p\n", phead->data, phead, phead->pNext);
 88     }
 89 }
 90 
 91 node *searchfirst(node *phead, int finddata)// 查找数据
 92 {
 93     for (node *p = phead; p != NULL;p= p->pNext)
 94     {
 95         if (p->data==finddata)
 96         {
 97             return p;// 返回找到的地址
 98         }
 99     }
100     return NULL;
101 }
102 
103 node *changefirst(node *phead, int finddata, int newdata)// 修改查找 
104 {
105     for (node *p = phead; p != NULL;p=p->pNext)
106     {
107         if (p->data==finddata)
108         {
109             p->data = newdata;
110             return p;
111         }
112     }
113     return NULL;
114 }
115 
116 node *deletefirst(node *phead, int finddata)// 删除 中间任意节点  头节点   尾节点
117 {
118     node *p1 = NULL, *p2 = NULL;// 定义两个指针
119     p1 = phead;// 保存头节点
120     while (p1!=NULL)
121     {
122         if (p1->data!=finddata)
123         {
124             p2 = p1;// p2保存p1上一个位置
125             p1 = p1->pNext;
126         }
127         else
128         {
129             break;
130         }
131     }
132     
133     if (p1!=phead)
134     {
135         p2->pNext = p1->pNext;// 跳过p1
136         free(p1);// 删除p1
137     }
138     else
139     {
140         phead = phead->pNext;
141         free(p1);
142     }
143     return phead;
144 }
145 
146 node *insertfirst(node*phead, int finddata, int newdata)//插入
147 {
148     node *p1 = NULL, *p2 = NULL;
149     p1 = phead;//保存头结点
150     while (p1!=NULL)
151     {
152         if (p1->data != finddata)
153         {
154             p2 = p1;//p2是保存p1上一个位置
155             p1 = p1->pNext;
156         }
157         else
158         {
159             break;
160         }
161     }
162 
163     node *pnew = malloc(sizeof(node));//新的节点
164     pnew->data = newdata;
165     pnew->pNext = NULL;//赋值
166 
167     if (phead==p1)
168     {
169         pnew->pNext = phead;//保存头结点
170         phead = pnew;//头部插入
171     }
172 
173     else
174     {
175         pnew->pNext = p1;
176         p2->pNext = pnew;//插入
177     }
178 
179     return phead;
180 }

主方法实现测试:maintest.c

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include"linklist.h"
 4 
 5 void main()
 6 {
 7     node *phead = NULL;
 8     //init(phead);//初始化一个节点,不可以初始化头结点
 9     phead = addback(phead, 12);// 尾插
10     phead = addback(phead, 13);
11     phead = addback(phead, 14);
12     phead = addback(phead, 15);
13     phead = addback(phead, 16);
14     phead = addback(phead, 17);
15 
16     phead = insertfirst(phead, 12, 22);// 指定位置插入
17     phead = insertfirst(phead, 16, 66);
18     phead = insertfirst(phead, 17, 77);
19 
20     /*
21     phead = deletefirst(phead,12);// 删除节点
22     phead = deletefirst(phead, 14);
23     phead = deletefirst(phead, 17);
24     */
25 
26     /*
27     addhead(&phead, 11);// 头插
28     addhead(&phead, 10);
29     addhead(&phead, 9);
30     */
31 
32     /*
33     node *pfind = searchfirst(phead, 14);// 查找数据
34     pfind->data = 99;
35     node *changepfind = changefirst(phead,17,77);// 修改查找
36     */
37 
38     showall(phead);
39 
40 
41     /*printf("\n\n");
42     
43     revshowall(phead);*/
44 
45 
46     system("pause");
47 }

 

posted on 2015-05-22 21:03  Dragon-wuxl  阅读(195)  评论(0)    收藏  举报

导航