C 单链表的所有运用(有头结点和无头结点)
觉得有用就点个赞叭!❤
- 单链表基本操作
1.尾插法建立有头结点单链表
2.建立无头结点单链表
3.删除特定结点
4.插入特定结点
5.合并俩结点
6.删除俩链表的重复结点
7.去重
1.尾插法建立有头结点单链表
#include<stdio.h>
#include<stdlib.h>
#define len sizeof(list)
typedef struct List{
int data;
struct List *next;
} list;
int main(){
list *create_Havehead(); //有头结点 单链表创建
list *create_Nohead(); //无头结点 单链表创建
//自己调用
return 0;
}
list *create_Havehead(){
list *head, *p1, *p2;
head = malloc(len);
p2 = head;
head->data = 0;
head->next = NULL;
p1 = malloc(len);
printf("输入data:(输入0结束)\n");
scanf("%d", &p1->data);
while(p1->data){
p1->next = p2->next;
p2->next = p1;
p2 = p1;
p1 = malloc(len);
scanf("%d", &p1->data);
}
return head;
}
2.建立无头结点单链表
#include<stdio.h>
#include<stdlib.h>
#define len sizeof(list)
typedef struct List{
int data;
struct List *next;
} list;
int main(){
list *create_Havehead(); //有头结点 单链表创建
list *create_Nohead(); //无头结点 单链表创建
//自己调用
return 0;
}
list *create_Nohead(){
list *head, *p1, *p2;
int n = 0;
p1 = malloc(len);
printf("输入data:(输入0结束)\n");
scanf("%d", &p1->data);
p2 = p1;
head = NULL;
while(p1->data){
n++;
if(n==1){
head = p1;
}
else{
p2->next = p1;
}
p2 = p1;
p1 = malloc(len);
scanf("%d", &p1->data);
}
p2->next = NULL;
return head;
}
3.删除特定结点
#define len sizeof(list)
typedef struct List{
int data;
struct List *next;
} list;
int main(){
list *create_Havehead(); //有头结点 单链表创建
list *create_Nohead(); //无头结点 单链表创建
list *delete_Havehead (list *); //有头结点 删除特定结点
list *delete_Nohead(list *); //无头结点 删除特定结点
void export(list *);
list *head;
//head = create_Havehead();
export(head);
return 0;
}
list *delete_Nohead (list *head){
//无头结点删除特定结点
list *p1, *p2;
int num;
printf("输入您想删除结点的data:(输0退出)");
scanf("%d", &num);
p1 = p2 = head;
while(p1->data!=num&&p1->next&&num){
p2 = p1;
p1 = p1->next;
}
if(num==p1->data){
if(p1==head){
head = head->next;
}
else{
p2->next = p1->next;