1 //链式链表c语言版
2 typedef int ElemType;
3 typedef struct Node //链式链表定义
4 {
5 struct Node* next;
6 int data;
7 }ListLink;
8
9 ListLink* ListInit()//链式链表初始化
10 {
11 ListLink* head = (Node*)malloc(sizeof(Node));//创建一个头结点
12 ListLink* p = head;//声明一个指针指向头结点,用于遍历链表
13 for(int i = 1; i < 9; ++i)
14 {
15 //创建新结点
16 ListLink* q = (Node*)malloc(sizeof(ListLink));
17 q->data = i;
18 q->next = nullptr;
19 //继续将尾节点作为p
20 p->next = q;
21 p = p->next;
22 }
23 return head;
24 }
25
26 ListLink* ListGet(ListLink* List, ElemType e)//按值查找
27 {
28 ListLink* p = List->next;
29 while (p && (p->data)!=e)
30 {
31 p = p->next;
32 }
33 return p;
34
35 }
36
37 ListLink* ListGetIndex(ListLink* List, int i)//按位置查找
38 {
39 if (i == 0)
40 return List;
41 if (i < 1)
42 return nullptr;
43 int j = 1;
44 ListLink* p = List->next;//用一个指针指向第一个结点
45 while (p && j < i)
46 {
47 p = p->next;
48 j++;
49 }
50 return p;
51 }
52
53 ListLink* ListInsert(ListLink* List, int i, ElemType e)//在第i个位置插入元素e
54 {
55 ListLink* temp = List;//创建临时结点List
56 //首先找到要插入位置的上一个结点
57 int j = 1;
58 for (j; j < i; ++j)
59 {
60 if (!temp)
61 return temp;
62 temp = temp->next; //进行遍历 temp一步的往后移
63 }
64
65 ListLink* s = (ListLink*)malloc(sizeof(ListLink));
66 s->data = e;
67 s->next = temp->next;
68 temp->next = s;
69 return List;
70 }
71
72 ListLink* ListInsertHead(ListLink* List, ElemType e)//在头部插入一个元素
73 {
74 ListLink* p = List;
75 ListLink* q = (ListLink*)malloc(sizeof(ListLink));
76 q->data = e;
77 q->next = p->next;
78 p->next = q;
79 return List;
80 }
81
82 ListLink* ListInsertEnd(ListLink* List, ElemType e)//在尾部插入一个元素
83 {
84 ListLink *p, * q;
85 p = List;
86 while (p->next)
87 {
88 p = p->next;
89 }
90 q = (ListLink*)malloc(sizeof(ListLink));
91 q->data = e;
92 p->next = q;
93 q->next = nullptr;
94 return List;
95 }
96
97 ListLink* ListDeleteIndex(ListLink* List, int i)//按位置删除元素
98 {
99 ListLink* p = List; // 创建临时结点
100 int j = 1;
101 while (j < i && p)
102 {
103 p = p->next;
104 ++j;
105 }
106 if (!p || j > i)//第i个结点不存在
107 {
108 return nullptr;
109 }
110 ListLink* q = p->next;
111 p->next = q->next;
112 free(q);
113 return List;
114 }
115
116 bool ListDeleteElem(ListLink* List, ElemType e)//按值删除元素
117 {
118 ListLink* p = List;
119 while (p->next)
120 {
121 p = p->next;
122 if (p->data == e)
123 return true;
124 }
125 return false;
126 }
127
128 void ListClear(ListLink* List)//删除顺序链表全部元素
129 {
130
131 }
132
133 int ListLength(ListLink* List)//获取顺序链表长度
134 {
135 ListLink* p = List;
136 int j = 0;
137 while (p->next)
138 {
139 p = p->next;
140 ++j;
141 }
142 return j;
143 }
144
145 bool ListEmpty(ListLink* List)//顺序链表是否为空
146 {
147 return List->next == nullptr;
148
149 }
150
151 void ListShow(ListLink* List)//遍历链表的每个元素
152 {
153 ListLink* i = List;
154 while (i->next)
155 {
156 i = i->next;
157 cout << i->data << " ";
158 }
159 cout << endl;
160 }
161
162 int main() {
163
164 ListLink* p = ListInit();
165 ListShow(p);
166 cout << endl;
167 cout << ListLength(p) << endl;
168 ListLink* q = ListGet(p, 6);
169 cout << q->data << endl;
170 q = ListGetIndex(p, 5);
171 cout << q->data << endl;
172 q = ListInsert(p, 5, 90);
173 ListShow(q);
174 q = ListDeleteIndex(p, 5);
175 ListShow(q);
176 cout << ListEmpty(p) << endl;
177 q = ListInsertHead(p, 100);
178 ListShow(q);
179 q = ListInsertEnd(p, 1000);
180 ListShow(q);
181 cout << ListLength(p) << endl;
182 return 0;
183
184 }