线性表 —— 链式存储
单链表存储结构的定义:
1 #include <stdio.h> 2 #include <stdlib.h> 3 typedef int ElemType; 4 typedef struct Node { /*结点类型定义*/ 5 ElemType data; 6 struct Node* next; 7 }Node, * LinkList; /*Node*与LinkList为结构指针类型,两种类型等价*/
单链表的初始化:
1 /*初始化单链表*/ 2 LinkList InitList() { 3 LinkList L = (Node*)malloc(sizeof(Node)); 4 L->next = NULL; 5 return L; 6 }
单链表的头插法建表:
1 /*单链表的头插法*/ 2 void CreateFromHead(LinkList L, ElemType e) { 3 Node* s; 4 s = (Node*)malloc(sizeof(Node)); 5 s->data = e; 6 s->next = L->next; /*表为空L->next=null,将*/ 7 L->next = s; 8 } 9 int main() { 10 LinkList L = InitList(); 11 CreateFromHead(L, 10); 12 CreateFromHead(L, 20); 13 CreateFromHead(L, 30); 14 CreateFromHead(L, 40); 15 16 printf("单链表:"); 17 printfList(L); 18 return 0; 19 }
采用头插法得到的单链表的逻辑顺序与输入元素顺序相反,亦称头插法建表为逆序建表法。
单链表的尾插法建表:
1 /*单链表的尾插法*/ 2 void CreateFromTail(LinkList L, ElemType e) { 3 Node* s; 4 Node* r; 5 s = (Node*)malloc(sizeof(Node)); 6 r = L; 7 while (r->next != NULL) { 8 r = r->next; 9 } 10 s->data = e; 11 s->next = NULL; 12 r->next = s; 13 }
14
15 int main() { 16 LinkList L = InitList(); 17 CreateFromTail(L, 10); 18 CreateFromTail(L, 20); 19 CreateFromTail(L, 30); 20 CreateFromTail(L, 40); 21 22 printf("单链表:"); 23 printfList(L); 24 return 0; 25 }
单链表的长度:
1 int ListLength(LinkList L) { 2 Node* p; 3 p = L->next; 4 int j = 0; 5 while (p != NULL) { 6 p = p->next; 7 j++; 8 } 9 return j; 10 } 11 int main() { 12 LinkList L = InitList(); 13 CreateFromTail(L, 10); 14 CreateFromTail(L, 20); 15 CreateFromTail(L, 30); 16 CreateFromTail(L, 40); 17 18 printf("单链表:"); 19 printfList(L); 20 21 printf("单链表长度:%d\n", ListLength(L)); 22 return 0; 23 }
单链表中查找值为key的结点:
1 ElemType n = 0; /*全局变量用于查找结点的位置*/ 2 Node* Locate(LinkList L, ElemType key) { 3 Node* p; 4 p = L->next; 5 while (p != NULL) { 6 if (p->data != key) { 7 p = p->next; 8 n++; 9 } 10 else 11 { 12 break; 13 } 14 } 15 return p; 16 } 17 int main() { 18 LinkList L = InitList(); 19 CreateFromTail(L, 10); 20 CreateFromTail(L, 20); 21 CreateFromTail(L, 30); 22 CreateFromTail(L, 40); 23 24 printf("单链表:"); 25 printfList(L); 26 27 printf("单链表长度:%d\n", ListLength(L)); 28 29 Node* p = Locate(L,30); 30 if (p == NULL) { 31 printf("没有找到"); 32 } 33 else { 34 printf("您要找的值:%d 所在的位置:%d", p->data,n); 35 } 36 return 0; 37 }
单链表中按元素大小顺序插入一个元素:
1 void InsOrderLinkList(LinkList L, ElemType e) { 2 Node* s, * p; 3 s = (Node*)malloc(sizeof(Node)); 4 p = L; 5 while (p->next != NULL) { 6 if (p->next->data < e) { 7 p = p->next; 8 } 9 else { 10 break; 11 } 12 } 13 s->data = e; 14 s->next = p->next; 15 p->next = s; 16 }
单链表位置i处插入元素e:
1 void InsLinkList(LinkList L, int i, ElemType e) { 2 Node* s, * p; 3 int count = 0; 4 s = (Node*)malloc(sizeof(Node)); 5 p = L; 6 while (p->next!= NULL) { 7 if (count == i - 1) { 8 break; 9 } 10 else { 11 p = p->next; 12 count++; 13 } 14 } 15 s->data = e; 16 s->next = p->next; 17 p->next = s; 18 }
单链表中删除某个结点:
1 void DelLinkList(LinkList L, ElemType e) { 2 Node* s, * p; 3 s = (Node*)malloc(sizeof(Node)); 4 p = L; 5 while (p->next!=NULL) 6 { 7 if (p->next->data==e) { 8 s = p->next; 9 p->next = s->next; 10 free(s); 11 return; 12 } 13 else 14 { 15 p = p->next; 16 } 17 } 18 }
单链表操作代码:
1 #include <stdio.h> 2 #include <stdlib.h> 3 typedef int ElemType; 4 typedef struct Node { 5 ElemType data; 6 struct Node* next; 7 }Node, * LinkList; 8 ElemType n=1; 9 /*初始化单链表*/ 10 LinkList InitList() { 11 LinkList L = (Node*)malloc(sizeof(Node)); 12 L->next = NULL; 13 return L; 14 } 15 /*单链表的头插法*/ 16 void CreateFromHead(LinkList L, ElemType e) { 17 Node* s; 18 s = (Node*)malloc(sizeof(Node)); 19 s->data = e; 20 s->next = L->next; 21 L->next = s; 22 } 23 /*单链表的尾插法*/ 24 void CreateFromTail(LinkList L, ElemType e) { 25 Node* s; 26 Node* r; 27 s = (Node*)malloc(sizeof(Node)); 28 r = L; 29 while (r->next != NULL) { 30 r = r->next; 31 } 32 s->data = e; 33 s->next = NULL; 34 r->next = s; 35 r = s; 36 } 37 /*按大小顺序在单链表中插入结点*/ 38 void InsOrderList(LinkList L,ElemType e) { 39 Node* s, * p; 40 s = (Node*)malloc(sizeof(Node)); 41 p = L; 42 while (p->next!=NULL) 43 { /*若e比p->next->data大,结点e向后移动 44 若e比p->next->data小,跳出循环并在该位置插入结点e 45 */ 46 if (p->next->data<e) 47 { 48 p = p->next; 49 } 50 else 51 { 52 break; 53 } 54 } 55 s->data = e; 56 s->next = p->next; 57 p->next = s; 58 } 59 /*在单链表位置i处,插入结点e*/ 60 void InsList(LinkList L,int i,ElemType e) { 61 Node* s, * p; 62 int count = 0; 63 s = (Node*)malloc(sizeof(Node)); 64 p = L; 65 while (p->next!=NULL) 66 { 67 if (count==i-1) 68 { 69 break; 70 } 71 else { 72 p = p->next; 73 count++; 74 } 75 } 76 if (count==i-1) 77 { 78 s->data = e; 79 s->next = p->next; 80 p->next = s; 81 } 82 } 83 /*单链表中删除某个结点*/ 84 void DelList(LinkList L,ElemType e) { 85 Node* s, * p; 86 s = (Node*)malloc(sizeof(Node)); 87 p = L; 88 while (p->next != NULL) { 89 if (p->next->data==e) 90 { 91 s = p->next; 92 p->next = s->next; 93 free(s); 94 return; 95 } 96 else 97 { 98 p = p->next; 99 } 100 } 101 } 102 /*单链表的打印*/ 103 void printfList(LinkList L) { 104 Node* p; 105 p = L->next; 106 while (p != NULL) { 107 printf("%d ", p->data); 108 p = p->next; 109 } 110 printf("\n"); 111 } 112 /*单链表的长度*/ 113 int ListLength(LinkList L) { 114 Node* p; 115 p = L->next; 116 int j = 0; 117 while (p != NULL) { 118 p = p->next; 119 j++; 120 } 121 return j; 122 } 123 /*单链表中查找key的值*/ 124 Node* Locate(LinkList L, ElemType key) { 125 Node* p; 126 p = L->next; 127 while (p != NULL) { 128 if (p->data != key) { 129 p = p->next; 130 n++; 131 } 132 else 133 { 134 break; 135 } 136 } 137 return p; 138 } 139 140 int main() { 141 LinkList L = InitList(); 142 CreateFromTail(L, 10); 143 CreateFromTail(L, 20); 144 CreateFromTail(L, 30); 145 CreateFromTail(L, 40); 146 147 printf("单链表:"); 148 printfList(L); 149 150 printf("按顺序插入某元素:"); 151 InsOrderList(L, 25); 152 printfList(L); 153 154 printf("在固定位置插入某个元素:"); 155 InsList(L, 2, 100); 156 printfList(L); 157 158 printf("删除某个元素:"); 159 DelList(L, 100); 160 printfList(L); 161 162 printf("单链表长度:%d\n", ListLength(L)); 163 164 Node* p = Locate(L,30); 165 166 if (p == NULL) { 167 printf("没有找到"); 168 } 169 else { 170 printf("您要找的值:%d 所在的位置:%d", p->data,n); 171 } 172 173 174 return 0; 175 }