单向链表
主函数
#include "./link.h"
int main(int argc, const char *argv[])
{
linkList *list=create_linkList();
/*delete_head_linkList(list);
delete_tail_linkList(list);
insert_head_linkList(list,50);
printf("%d\n",list->text.len);
insert_head_linkList(list,30);
insert_head_linkList(list,40);
insert_head_linkList(list,60);
insert_head_linkList(list,10);
show_linkList(list);
insert_tail_linkList(list,12);
insert_tail_linkList(list,15);
show_linkList(list);
delete_head_linkList(list);
show_linkList(list);
delete_tail_linkList(list);
show_linkList(list);
add_linkList(list,6,99);
show_linkList(list);
delete_linkList(list,1);
show_linkList(list);*/
insert_sort(list,15);
insert_sort(list,19);
insert_sort(list,10);
insert_sort(list,21);
insert_sort(list,18);
insert_sort(list,1);
show_linkList(list);
return 0;
}
头文件
#ifndef __LINK_H__
#define __LINK_H__
#include <stdio.h>
#include <stdlib.h>
typedef int dataType;
union msg{
dataType data;
int len;
};
typedef struct node{
union msg text;
struct node *next;
}linkList;
linkList* create_linkList(void);
void insert_head_linkList(linkList* list,dataType num);
void show_linkList(linkList* head);
void insert_tail_linkList(linkList* list,dataType num);
void delete_head_linkList(linkList* head);
void delete_tail_linkList(linkList* tail);
void add_linkList(linkList* head,int place,dataType num);
void delete_linkList(linkList* head,int place);
void insert_sort(linkList* head,dataType num);
#endif
功能文件
#include "./link.h"
/*
* function: 创建链表
* @param [ in]
* @param [out]
* @return
*/
linkList* create_linkList(void){
linkList* head = (linkList*)malloc(sizeof(linkList));
if(NULL==head){
printf("头结点申请失败,创建单链表失败\n");
return NULL;
}
head->text.len=0;
head->next=NULL;
return head;
}
/*
* function: 从头插入数据
* @param [ in]
* @param [out]
* @return
*/
void insert_head_linkList(linkList* list,dataType num){
linkList *temp=(linkList*)malloc(sizeof(linkList));
if(NULL==temp){
printf("创建节点失败\n");
return;
}
temp->next=list->next;
temp->text.data=num;
list->next=temp;
list->text.len++;
printf("插入成功\n");
return;
}
/*
* function: 遍历链表
* @param [ in]
* @param [out]
* @return
*/
void show_linkList(linkList* head){
linkList *p=head;
while(p->next!=NULL){
p=p->next;
printf("%d ",p->text.data);
}
printf("\n");
return ;
}
/*
* function: 尾插法
* @param [ in]
* @param [out]
* @return
*/
void insert_tail_linkList(linkList* list,dataType num){
linkList *p=list;
linkList* temp=(linkList*)malloc(sizeof(linkList));
if(NULL==temp){
printf("节点申请失败\n");
return;
}
while(p->next!=NULL){
p=p->next;
}
p->next=temp;
temp->text.data=num;
temp->next=NULL;
list->text.len++;
printf("尾插成功\n");
return ;
}
/*
* function: 判断链表是否为空
* @param [ in]
* @param [out]
* @return 空则返回0
*/
int isEmpty_linkList(linkList* head){
return head->next==NULL?0:1;
}
/*
* function: 头部删除
* @param [ in]
* @param [out]
* @return
*/
void delete_head_linkList(linkList* head){
if(isEmpty_linkList(head)==0){
printf("头删除失败\n");
return;
}
linkList* temp=NULL;
temp=head->next;
head->next=temp->next;
free(temp);
temp=NULL;
head->text.len--;
return ;
}
/*
* function: 尾部删除
* @param [ in]
* @param [out]
* @return
*/
void delete_tail_linkList(linkList* tail){
if(isEmpty_linkList(tail)==0){
printf("尾删除失败\n");
return;
}
linkList* p=tail;
linkList* temp=NULL;
while(p->next!=NULL){
temp=p;
p=p->next;
}
temp->next=NULL;
free(p);
p=NULL;
tail->text.len--;
return;
}
/*
* function: 按位置操作——插入
* @param [ in]
* @param [out]
* @return
*/
void add_linkList(linkList* head,int place,dataType num){
if(place>0&&place<=head->text.len+1){
linkList* p=head;
linkList* pt=NULL;
for(int i=0;i<place;i++){
pt=p;
p=p->next;
}
linkList* temp=(linkList*)malloc(sizeof(linkList));
if(temp==NULL){
printf("节点申请失败\n");
return ;
}
pt->next=temp;
temp->text.data=num;
temp->next=p;
head->text.len++;
}
else{
printf("操作位置错误\n");
}
return;
}
/*
* function: 按位置操作——删除
* @param [ in]
* @param [out]
* @return
*/
void delete_linkList(linkList* head,int place){
if(place>0&&place<=head->text.len){
linkList* p=head;
linkList* pt=NULL;
if(isEmpty_linkList(head)==0){
printf("链表为空,无法删除\n");
return;
}
for(int i=0;i<place;i++){
pt=p;
p=p->next;
}
pt->next=p->next;
free(p);
p=NULL;
head->text.len--;
}
else{
printf("删除位置错误\n");
}
return ;
}
/*
* function: 链表翻转
* @param [ in]
* @param [out]
* @return
*/
void flip_linkList(linkList* head){
if(isEmpty_linkList(head)==0){
printf("链表为空,无法翻转");
return ;
}
linkList* p=head;
while(p->next->next!=NULL){
p=p->next;
}
linkList* temp=p;
for(int i=1;i<head->text.len;i++){
}
}
/*
* function: 直接插入排序
* @param [ in]
* @param [out]
* @return
*/
void insert_sort(linkList* head,dataType num){
linkList* temp=(linkList*)malloc((sizeof(linkList)));
if(temp==NULL){
printf("节点申请失败\n");
return ;
}
temp->text.data=num;
temp->next=NULL;
linkList* p=head;
if(head->next==NULL){
head->next=temp;
head->text.len++;
}
while(p->next!=NULL){
if(num<p->next->text.data){
break;
}else
p=p->next;
}
temp->next=p->next;
p->next=temp;
head->text.len++;
return;
}
单向循环链表
头文件
#ifndef __LOOP_H__
#define __LOOP_H__
#include <stdio.h>
#include <stdlib.h>
typedef int dataType;
union msg{
dataType data;
int len;
};
typedef struct node{
union msg text;
struct node *next;
}LooplinkList;
LooplinkList* create_looplinkList(void);
void insertetail_loopLinkList(LooplinkList* head,dataType num);
void show_loopLinkList(LooplinkList* head);
void insertehead_loopLinkList(LooplinkList* head,dataType num);
void deletetail_looplinkList(LooplinkList* head);
void deletehead_looplinkList(LooplinkList* head);
void inserte_LooplinkList(LooplinkList* head,int loca,dataType num);
#endif
主函数
#include "./loopLinkList.h"
/*
* function: 创建循环链表
* @param [ in]
* @param [out]
* @return
*/
int main(int argc, const char *argv[])
{
LooplinkList* list=create_looplinkList();
insertetail_loopLinkList(list,11);
insertehead_loopLinkList(list,12);
insertehead_loopLinkList(list,13);
insertehead_loopLinkList(list,14);
insertetail_loopLinkList(list,15);
insertetail_loopLinkList(list,16);
show_loopLinkList(list);
deletehead_looplinkList(list);
show_loopLinkList(list);
deletetail_looplinkList(list);
show_loopLinkList(list);
inserte_LooplinkList(list,5,99);
show_loopLinkList(list);
return 0;
}
功能函数
1 #include "./loopLinkList.h"
2
3 LooplinkList* create_looplinkList(void){
4 LooplinkList* head=(LooplinkList*)malloc(sizeof(LooplinkList));
5 if(head==NULL){
6 printf("创建循环链表失败!!\n");
7 return NULL;
8 }
9 head->next=head;
0 return head;
1 }
2 /*
3 * function: 头插法
4 * @param [ in]
5 * @param [out]
6 * @return
7 */
8
9 void insertehead_loopLinkList(LooplinkList* head,dataType num){
0 LooplinkList* temp=(LooplinkList* )malloc(sizeof(LooplinkList));
1 if(temp == NULL){
2 printf("申请节点失败\n");
3 return;
4 }
5 temp->next=head->next;
6 temp->text.data=num;
7 head->next=temp;
8 head->text.len++;
9 return;
0 }
1
2 /*
3 * function: 尾插法
4 * @param [ in]
5 * @param [out]
6 * @return
7 */
8
9 void insertetail_loopLinkList(LooplinkList* head,dataType num){
0 LooplinkList* temp=(LooplinkList*)malloc(sizeof(LooplinkList));
1 if(temp == NULL){
2 printf("申请节点失败\n");
3 return;
4 }
5 LooplinkList* p=NULL;
6 p=head->next;
7 while(p->next!=head){
8 p=p->next;
9 }
0 temp->next=head;
1 temp->text.data=num;
2 p->next=temp;
3 head->text.len++;
4 return;
5 }
6
7 /*
8 * function: 遍历
9 * @param [ in]
0 * @param [out]
1 * @return
2 */
3
4 void show_loopLinkList(LooplinkList* head){
5 LooplinkList* p=NULL;
6 p=head;
7 while(p->next!=head){
8 p=p->next;
9 printf("%d ",p->text.data);
0 }
1 printf("\n");
2 return;
3 }
4
5 /*
6 * function: 头删除法
7 * @param [ in]
8 * @param [out]
9 * @return
0 */
1
2 void deletehead_looplinkList(LooplinkList* head){
3 if(head->next==head){
4 printf("链表为空,无法删除\n");
5 return ;
6
7 LooplinkList* p;
8 p=head->next;
9 head->next=p->next;
0 head->text.len--;
1 free(p);
2 p=NULL;
3 return;
4 }
5
6
7 /*
8 * function: 尾删除法
9 * @param [ in]
0 * @param [out]
1 * @return
2 */
3
4 void deletetail_looplinkList(LooplinkList* head){
5 if(head->next==head){
6 printf("链表为空,无法删除\n");
7 return ;
8 }
9 LooplinkList* p=head;
0
1 while(p->next->next!=head){
2 p=p->next;
3 }
4 free(p->next);
5 p->next=head;
6 return;
7 }
8
9
0 /*
1 * function: 按位置插入
2 * @param [ in]
3 * @param [out]
4 * @return
5 */
6
7 void inserte_LooplinkList(LooplinkList* head,int loca,dataType num)
8 {
9 LooplinkList* temp=(LooplinkList*)malloc(sizeof(LooplinkList));
0 if(temp==NULL){
1 printf("申请节点失败\n");
2 return;
3 }
4 LooplinkList* p=head;
5 for(int i=0;i<loca-1;i++){
6 if(p->next==head){
7 p=p->next->next;
8 }
9 p=p->next;
0 }
1 temp->next=p->next;
2 temp->text.data=num;
3 p->next=temp;
4 head->text.len++;
5
6 return;
7 }
双向链表
头文件
#ifndef __LINK_H__
#define __LINK_H__
#include <stdio.h>
#include <stdlib.h>
typedef int dataType;
union msg{
dataType data;
int len;
};
typedef struct node{
union msg text;
struct node *next;
struct node *prve;
}doubleLink;
void insert_Bypos(doubleLink* head,int pos,dataType num);
void delete_Head(doubleLink* head);
doubleLink* create_doublelink(void);
void instert_Head(doubleLink* head,dataType num);
void show_doublelink(doubleLink* head);
void instert_Tail(doubleLink* head,dataType num);
void delete_Tail(doubleLink* head);
void delete_Bypos(doubleLink* head,int pos);
#endif
主函数
#include "./dooubleLink.h"
int main(int argc, const char *argv[])
{
doubleLink* list = create_doublelink();
instert_Head(list,12);
instert_Head(list,13);
instert_Head(list,14);
show_doublelink(list);
instert_Tail(list,11);
show_doublelink(list);
insert_Bypos(list,2,66);
insert_Bypos(list,99,1000);
show_doublelink(list);
delete_Head(list);
show_doublelink(list);
delete_Tail(list);
show_doublelink(list);
delete_Bypos(list,1);
show_doublelink(list);
delete_Bypos(list,2);
show_doublelink(list);
return 0;
}
功能函数
1 #include "./dooubleLink.h"
2
3 /*
4 * function: 创建双向链表
5 * @param [ in]
6 * @param [out]
7 * @return
8 */
9
10 doubleLink* create_doublelink(void){
11 doubleLink* head=(doubleLink*)malloc(sizeof(doubleLink));
12 if(head==NULL){
13 printf("创建双向链表失败\n");
14 return NULL;
15 }
16 head->next=NULL;
17 head->prve=NULL;
18 head->text.len=0;
19 return head;
20 }
21
22
23 /*
24 * function: 遍历
25 * @param [ in]
26 * @param [out]
27 * @return
28 */
29 void show_doublelink(doubleLink* head){
30 doubleLink* p=head;
31 while(p->next!=NULL){
32 p=p->next;
33 printf("%d ",p->text.data);
34 }
35 printf("\n");
36 return;
37
38 }
39
40 /*
41 * function: 头插法
42 * @param [ in]
43 * @param [out]
44 * @return
45 */
46 void instert_Head(doubleLink* head,dataType num){
47 doubleLink* temp=(doubleLink*)malloc(sizeof(doubleLink));
48 if(temp==NULL){
49 printf("申请节点失败\n");
50 return;
51 }
52
53 temp->next=NULL;
54 temp->prve=NULL;
55 temp->text.data=num;
56
57 if(head->next==NULL){
58 temp->next=head->next;
59 head->next=temp;
60 temp->prve=head;
61 }
62 else{
63 temp->next=head->next;
64 head->next=temp;
65 temp->next->prve=temp;
66 temp->prve=head;
67 }
68 head->text.len++;
69 return ;
70 }
71
72
73 /*
74 * function: 尾插法
75 * @param [ in]
76 * @param [out]
77 * @return
78 */
79
80 void instert_Tail(doubleLink* head,dataType num){
81 doubleLink* temp=(doubleLink*)malloc(sizeof(doubleLink));
82 if(temp==NULL){
83 printf("申请节点失败\n");
84 return;
85 }
86 temp->next=NULL;
87 temp->prve=NULL;
88 temp->text.data=num;
89 doubleLink* p=head;
90 while(p->next!=NULL){
91 p=p->next;
92 }
93 temp->next=p->next;
94 p->next=temp;
95 temp->prve=p;
96 head->text.len++;
97 return;
98 }
99 /*
100 * function: 按位置插入
101 * @param [ in]
102 * @param [out]
103 * @return
104 */
105
106 void insert_Bypos(doubleLink* head,int pos,dataType num){
107 doubleLink* temp=(doubleLink*)malloc(sizeof(doubleLink));
108 if(temp==NULL){
109 printf("申请节点失败\n");
110 return;
111 }
112 temp->next=NULL;
113 temp->prve=NULL;
114 temp->text.data=num;
115 if(pos<1||pos>head->text.len){
116 printf("位置不合法\n");
117 return;
118 }
119 doubleLink* p=head;
120 for(int i=0;i<pos-1;i++){
121 p=p->next;
122 }
123 if(p->next!=NULL){
124 temp->next=p->next;
125 p->next=temp;
126 temp->next->prve=temp;
127 temp->prve=p;
128 }
129 else{
130 temp->next=p->next;
131 p->next=temp;
132 temp->prve=p;
133 }
134 head->text.len++;
135 return;
136 }
137
138
139 /*
140 * function: 头删除法
141 * @param [ in]
142 * @param [out]
143 * @return
144 */
145 void delete_Head(doubleLink* head){
146 if(head->next==NULL){
147 printf("链表为空,不能删除\n");
148 return;
149 }
150 doubleLink* p=head->next;
151 if(p->next==NULL){
152 p->next=NULL;
153 }
154 head->next=p->next;
155 p->next->prve=head;
156
157 free(p);
158 p=NULL;
159 head->text.len--;
160 return;
161 }
162
163 /*
164 * function: 尾删除
165 * @param [ in]
166 * @param [out]
167 * @return
168 */
169
170 void delete_Tail(doubleLink* head){
171 if(head->next==NULL){
172 printf("链表为空,不能删除\n");
173 return;
174 }
175 doubleLink* p=head->next;
176 if(p->next==NULL){
177 head->next=NULL;
178 }
179 while(p->next!=NULL){
180 p=p->next;
181 }
182 p->prve->next=NULL;
183 head->text.len--;
184 return;
185 }
186 /*
187 * function: 按位置删除
188 * @param [ in]
189 * @param [out]
190 * @return
191 */
192 void delete_Bypos(doubleLink* head,int pos){
193 if(head->next==NULL){
194 printf("链表为空,不能删除\n");
195 return;
196 }
197 doubleLink* p=head->next;
198
199 if(pos<1||pos>head->text.len){
200 printf("位置不合法\n");
201 return;
202 }
203 for(int i=1;i<pos;i++){
204 p=p->next;
205 }
206 if(p->next==NULL){
207 p->prve->next=p->next;
208 }
209 else{
210 p->prve->next=p->next;
211 p->next->prve=p->prve;
212 }
213 head->text.len--;
214 return;
215 }
216
~
~
~
~
~
~
~