简单的单向链表
ElemType READ(); int LENGTH(LinkList list); int ISEMPTY(LinkList list); LinkList FIND(LinkList list,ElemType item); int INSERTLINK(LinkList list,int i,ElemType item); void DELETELIST(LinkList list);
1 1 #include<stdio.h> 2 2 #include<stdlib.h> 3 3 #include <string.h> 4 4 typedef int ElemType; 5 5 //链结点类型 6 6 typedef struct node{ 7 7 ElemType data; 8 8 struct node *link; 9 9 }LNode,*LinkList; 10 10 ElemType READ(); 11 11 int LENGTH(LinkList list); 12 12 int ISEMPTY(LinkList list); 13 13 LinkList FIND(LinkList list,ElemType item); 14 14 //在非空线性链表的第i个链结点后插入一个数据信息为item的链节点 15 15 int INSERTLINK(LinkList list,int i,ElemType item); 16 16 void DELETELIST(LinkList list); 17 17 ElemType READ() 18 18 { 19 19 ElemType a; 20 20 printf("Please input a number "); 21 21 scanf("%d",&a); 22 22 return a; 23 23 } 24 24 //建立一个线性链表 25 25 LinkList CREATE(int n) 26 26 { 27 27 //p:新结点指针 r:尾部指针 list:头指针 28 28 LinkList p,r,list = NULL; 29 29 ElemType a; 30 30 int i = 0; 31 31 for(i = 0 ;i<n;i++) 32 32 { 33 33 a = READ(); 34 34 p = (LinkList)malloc(sizeof(LNode)); 35 35 p->data = a; 36 36 p->link = NULL; 37 37 if(list == NULL) 38 38 list = p; 39 39 else 40 40 r->link = p; 41 41 r = p; 42 42 } 43 43 return (list); 44 44 } 45 45 void OUTPUT(LinkList list) 46 46 { 47 47 LinkList p; 48 48 p = list; 49 49 if(p == NULL){printf("linklist is empty ");return;} 50 50 while(p!=NULL) 51 51 { 52 52 printf("%d ",(p->data)); 53 53 p = p->link; 54 54 } 55 55 printf(" "); 56 56 } 57 57 int main() 58 58 { 59 59 int n,op; 60 60 printf("Please input the size of LinkList "); 61 61 scanf("%d",&n); 62 62 LinkList list = CREATE(n); 63 63 OUTPUT(list); 64 64 while(1) 65 65 { 66 66 printf("option: 1:insert 2:delete 3:build "); 67 67 scanf("%d",&op); 68 68 switch(op) 69 69 { 70 70 case 1: 71 71 printf("input position and value "); 72 72 int pos,val; 73 73 scanf("%d%d",&pos,&val); 74 74 INSERTLINK(list,pos,val); 75 75 OUTPUT(list); 76 76 break; 77 77 case 2:DELETELIST(list); 78 78 //list = NULL; 79 79 OUTPUT(list); 80 80 break; 81 81 case 3: 82 82 if(ISEMPTY(list)) 83 83 { 84 84 printf("input the size of linklist "); 85 85 scanf("%d",&n); 86 86 list = CREATE(n); 87 87 OUTPUT(list); 88 88 } 89 89 else {printf("delete list at first ");} 90 90 break; 91 91 92 92 } 93 93 } 94 94 } 95 95 int LENGTH(LinkList list) 96 96 { 97 97 int num=0; 98 98 LinkList p = list; 99 99 while(p!=NULL) 100 100 { 101 101 num++; 102 102 p = p->link; 103 103 } 104 104 return num; 105 105 } 106 106 int ISEMPTY(LinkList list) 107 107 { 108 108 return list == NULL; 109 109 } 110 110 //返回被查找节点的地址,否则返回NULL 111 111 LinkList FIND(LinkList list ,ElemType item) 112 112 { 113 113 LinkList p = list; 114 114 while(p!=NULL&&p->data!=item) 115 115 { 116 116 p = p->link; 117 117 } 118 118 return p; 119 119 } 120 120 int INSERTLINK(LinkList list ,int i,ElemType item) 121 121 { 122 122 //查找第i个结点 123 123 LinkList p = list,newNode; 124 124 int j = 1; 125 125 while(j<i&& p != NULL) 126 126 { 127 127 j++; 128 128 p = p->link; 129 129 } 130 130 if(j!=i||p==NULL) 131 131 { 132 132 printf("链表中不存在第i个链结点 "); 133 133 return -1; 134 134 } 135 135 newNode = (LinkList)malloc(sizeof(LNode));//申请一个新的链结点 136 136 newNode -> data = item; 137 137 newNode -> link = p->link; 138 138 p->link = newNode; 139 139 return 1; 140 140 141 141 } 142 142 void DELETELIST(LinkList list) 143 143 { 144 144 LinkList p = list; 145 145 while(p!= NULL) 146 146 { 147 147 list = p->link; 148 148 free(p); 149 149 p = list; 150 150 } 151 151 list = NULL;//把整个链表删除后 让头指针为NULL list是指针 为什么 最后list!=NULL 152 152 }