链表程序

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 
  4 typedef int datatype;
  5 typedef struct node/*链表的每一个节点都是结构体*/
  6 {
  7     datatype data;
  8     struct node *next; 
  9 }linklist;/*linklist 是struct node 的别名,以后就可以用linklist来替代struct node*/
 10 
 11 /*函数声明区*/
 12 /*增删改查这几个函数形参一般都要有头节点*/
 13 linklist * list_create();
 14 int head_insert(linklist *H,datatype value);
 15 int head_delete(linklist *H);
 16 void list_show(linklist *H);
 17 //空----1   非空-----0
 18 int list_empty(linklist *H);
 19 int list_insert(linklist *H,datatype value,int pos);
 20 int list_delete(linklist *H,datatype value);
 21 int list_replace(linklist *H,datatype old,datatype new);
 22 int list_search(linklist *H,datatype value);
 23 void list_recorver(linklist *H);
 24 
 25 int main(int argc, const char *argv[])
 26 {
 27     linklist *H = NULL;
 28     if((H = list_create()) == NULL)
 29     {
 30         printf("list_create failed
");
 31         return -1;
 32     }
 33 
 34     head_insert(H,1);
 35     head_insert(H,2);
 36     head_insert(H,3);
 37     head_insert(H,4);
 38     head_insert(H,5);
 39     list_show(H);
 40 
 41     head_delete(H);
 42     list_show(H);
 43 
 44     list_insert(H,10,2);
 45     list_show(H);
 46 
 47     list_delete(H,3);
 48     list_show(H);
 49 
 50     list_replace(H,10,100);
 51     list_show(H);
 52 
 53     printf("search:%d
",list_search(H,1));
 54 
 55     list_recorver(H);
 56     list_show(H);
 57 
 58     return 0;
 59 }
 60 /*h是头结点,p是第首节点,因为头结点和首节点到尾节点之间的节点数据类型都一样,都是struct node类型的*/
 61 linklist * list_create()
 62 {
 63     linklist *H = NULL;//定义任何一个指针都需要初始化,一般初始化为NULL,然后再重新指向,否则成为野指针。
 64     if((H = malloc(sizeof(linklist))) == NULL)
 65     {
 66         printf("malloc failed
");
 67         return NULL;
 68     }
 69 
 70     H->next = NULL;//防止野指针。
 71 
 72     return H;
 73 }
 74 
 75 int head_insert(linklist *H,datatype value)
 76 {
 77     linklist *p = NULL;/*防止野指针*/
 78     if((p = malloc(sizeof(linklist))) == NULL)
 79     {
 80         printf("malloc node failed
");
 81         return -1;
 82     }
 83     /*把p插在h的后面*/
 84     p->data = value;//p的数据域为value
 85     p->next = H->next;//h节点后面的节点的地址为h->next,把h后面的节点的地址 保存在p的指针域(p->next)中。
 86     H->next = p;//把p的内容 给第一个节点的指针域(h->next)
 87 
 88     return 0;
 89 }
 90 
 91 void list_show(linklist *H)
 92 {
 93     while(H->next != NULL)//防止第一个节点后没有节点。
 94     {
 95         printf("%d ",H->next->data);//第二个节点的数据域
 96         H = H->next;//指向下一个节点,H移动。
 97     }
 98     printf("
");
 99 }
100 
101 int head_delete(linklist *H)
102 {
103     linklist *p = H->next;
104     if(list_empty(H))
105     {
106         printf("list is empty
");
107         return -1;
108     }
109     
110     H->next = p->next;
111     free(p);//p节点所指向的内存被回收,p本身还存在。
112     p = NULL;
113 
114     return 0;
115 }
116 
117 int list_empty(linklist *H)//判断是否为空链表 什么是空链表?只有头节点,没有首节点。
118 {
119     if(H->next != NULL)
120         return 0;
121     else
122         return 1;
123 }
124 
125 int list_insert(linklist *H,datatype value,int pos)/*指定位置插入节点,value是值,pos是位置,在pos后面插入*/
126 {
127     int i = 0;
128     linklist *p = H,
linklist *q = NULL; 129 while(i < pos && p != NULL) 130 { 131 p = p->next; 132 i++;//当i>pos时,p便不再移动,i也不再加一 133 } 134 135 if(p == NULL) 136 { 137 printf("pos error "); 138 return -1; 139 } 140 141 if((q = malloc(sizeof(linklist))) == NULL) 142 { 143 printf("malloc node failed "); 144 return -1; 145 } 146 /*和头插法的插入一样*/ 147 q->data = value; 148 q->next = p->next; 149 p->next = q; 150 151 return 0; 152 } 153 154 int list_delete(linklist *H,datatype value)/*指定位置删除节点*/ 155 { 156 linklist *p = NULL; 157 158 while(H->next != NULL) 159 { 160 if(H->next->data == value) 161 { 162 p = H->next; 163 H->next = p->next; 164 free(p); 165 p = NULL; 166 167 return 0; 168 } 169 else 170 H = H->next; 171 } 172 173 printf("no value"); 174 return -1; 175 } 176 177 int list_replace(linklist *H,datatype old,datatype new)/*改动值*/ 178 { 179 while(H->next != NULL) 180 { 181 if(H->next->data == old) 182 { 183 H->next->data = new; 184 return 0; 185 } 186 else 187 { 188 H = H->next; 189 } 190 } 191 192 return -1; 193 } 194 195 int list_search(linklist *H,datatype value) 196 { 197 int pos = 0; 198 while(H->next != NULL) 199 { 200 if(H->next->data == value) 201 return pos; 202 else 203 { 204 H = H->next; 205 pos++; 206 } 207 } 208 209 printf("no found "); 210 return -1; 211 } 212 213 void list_recorver(linklist *H) 214 { 215 linklist *q = NULL,*p = H->next; 216 H->next = NULL; 217 218 while(p != NULL) 219 { 220 q = p; 221 p = p->next; 222 q->next = H->next; 223 H->next = q; 224 } 225 }
原文地址:https://www.cnblogs.com/renchong/p/5586281.html