linkedlist(c语言_简单实现)

 1 #include<stdlib.h>
 2 typedef char Ele ;
 3 
 4 typedef struct node{
 5     Ele e;
 6     struct node *next;
 7 }lnode,*list;
 8 
 9 void get(Ele);
10 
11 //构造链表
12 list createlist(int n){
13     list l = NULL;
14     lnode *p,*r=NULL;
15     Ele e;
16     int i;
17     for(i=0;i<n;i++){
18         get(e);
19         p = (lnode *)malloc(sizeof(lnode));
20         p->e=e;
21         //构造首元素
22         if(!list){
23             l->e=p;
24         }else{
25             r->next=p;
26         }
27         r=p;
28     }
29     return l;
30 }
31 
32 //插入节点
33 void insertList(list list,lnode *q,Ele e){
34         lnode *p = (lnode *)malloc(sizeof(lnode));
35         p->e=e;
36         if(!list){
37             list->next=p;
38             p->next=NULL;
39         }else{
40             //将后一个节点赋值给p.next,前一个节点的下一个节点赋值给p
41             p->next=q->next;
42             q->next=p;
43         }
44 }
45 
46 //删除节点
47 void delNode(list l,lnode *q){
48     if(l==q){
49         l=q->next;
50     }else{
51         list temp = list;
52         while((temp=temp->next)!=q && temp!=NULL){
53         }
54         temp->next=q->next;
55         free(q);
56     }
57 }
58 
59 //销毁链表
60 void destorylist(list list){
61     lnode *p,*q;
62     p=list;
63     while(p){
64         q=p->next;
65         free(p);
66         p=q;
67     }
68     list=NULL;
69 }
原文地址:https://www.cnblogs.com/mozhuhao/p/4484875.html