1. 单链表
linear_list_head.c
cpp
复制代码
#include "stdio.h"
#include "stdlib.h"
#include "stdbool.h"
#include "string.h"
typedef bool status;
//单链表
//申明一个结点
struct Node
{
char name[20];
int age;
struct Node *next;
};
//创建单链表
struct Node *Linear_List_Create()
{
struct Node *p=(struct Node *)malloc(sizeof(struct Node));
if(p==NULL)
{
printf("单链表创建失败!\n");
return NULL;
}
memset(p->name,0,sizeof(p->name));
p->age=0;
p->next=NULL;
return p;
}
//遍历单链表
status Linear_List_Travel(struct Node *p)
{
if(p->next==NULL)
{
printf("空链表,遍历失败!\n");
return false;
}
struct Node *tra=p->next;
while(tra)
{
printf("姓名:%s\n",tra->name);
printf("年龄:%d\n",tra->age);
tra=tra->next;
}
return true;
}
//向单链表中插入数据
//头插法
status Linear_List_Insert_Head(struct Node *p,char name1[],int age1)
{
if(p->next==NULL)
{
struct Node *new_Node=(struct Node *)malloc(sizeof(struct Node));
if(new_Node==NULL)
{
printf("新节点创建失败,插入数据失败!\n");
return false;
}
strcpy(new_Node->name,name1);
new_Node->age=age1;
new_Node->next=NULL;
p->next=new_Node;
return true;
}
struct Node *new_Node=(struct Node *)malloc(sizeof(struct Node));
if(new_Node==NULL)
{
printf("新节点创建失败,插入数据失败!\n");
return false;
}
strcpy(new_Node->name,name1);
new_Node->age=age1;
//将头节点之后的节点地址保存起来
struct Node *m=p->next;
p->next=new_Node;
new_Node->next=m;
return true;
}
//尾插法
status Linear_List_Insert_Back(struct Node *p,char name1[],int age1)
{
if(p->next==NULL)
{
struct Node *new_Node=(struct Node *)malloc(sizeof(struct Node));
if(new_Node==NULL)
{
return false;
}
strcpy(new_Node->name,name1);
new_Node->age=age1;
new_Node->next=NULL;
p->next=new_Node;
return true;
}
struct Node *new_Node =(struct Node *)malloc(sizeof(struct Node));
if(new_Node==NULL)
{
return false;
}
strcpy(new_Node->name,name1);
new_Node->age=age1;
new_Node->next=NULL;
struct Node *tra=p;
while(tra->next)
{
tra=tra->next;
}
tra->next=new_Node;
return true;
}
status Linear_List_Insert_Fixed_Pos_Node(struct Node *p,int pos,char name1[],int age1)
{
if(p->next==NULL)
{
return false;
}
int list_Len=0;//获取链表的长度
struct Node *tra1=p;
while(tra1->next)
{
tra1=tra1->next;
list_Len++;
}
if(pos<1||pos>list_Len)
{
printf("插入的位置,超出了链表的长度,插入失败!\n");
return false;
}
int i=0;//遍历链表,找到插入位置的前一个结点的地址
struct Node *tra=p;
struct Node *temp;//存放tra后面结点的地址
while(i<list_Len)
{
i++;
if(i==pos)
{
temp=tra->next;
struct Node *new_Node=(struct Node *)malloc(sizeof(struct Node));
if(new_Node==NULL)
{
return false;
}
strcpy(new_Node->name,name1);
new_Node->age=age1;
tra->next=new_Node;
new_Node->next=temp;
return true;
}
tra=tra->next;
}
}
int main()
{
struct Node *L=Linear_List_Create();
//向单链表中头部插入结点
Linear_List_Insert_Head(L,"张三",20);
Linear_List_Insert_Head(L,"李四",30);
Linear_List_Insert_Head(L,"王二麻子",25);
//向单链表中的尾部插入结点
Linear_List_Insert_Back(L,"华清远见",30);
//向单链表中指定位置插入结点
Linear_List_Insert_Fixed_Pos_Node(L,2,"清华远见",30);
//遍历单链表
Linear_List_Travel(L);
return 0;
}
2. 循环单链表
my_loop_list.c
cpp
复制代码
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
typedef bool status;
//循环单链表
struct Node{
int num;
struct Node* next;
};
//初始化创建循环单链表
struct Node* loop_creat(){
struct Node* p = (struct Node*)malloc(sizeof(struct Node));
if(p==NULL){
printf("创建失败\n");
return NULL;
}
p->num = 0;
p->next = p;
return p;
}
//头插法
status loop_head_insert(struct Node* p,int data){
if(p->next==p){//链表为空
struct Node* new = (struct Node*)malloc(sizeof(struct Node));
if(new==NULL){
return false;
}
new->num = data;
p->next = new;
new->next = p;
return true;
}
//链表中有数据
struct Node* new = (struct Node*)malloc(sizeof(struct Node));
new->num = data;
struct Node* tmp = p->next;
p->next = new;
new->next = tmp;
return true;
}
//遍历链表
status loop_travel(struct Node* p){
if(p->next == p){//链表为空
printf("链表为空\n");
return false;
}
struct Node* tmp = p;
while(tmp->next!=p){
tmp = tmp->next;
printf("%d ",tmp->num);
}
return true;
}
//删除结点__头删法
status loop_delete_head(struct Node* p){
if(p->next==p){//链表为空
printf("链表为空无法删除\n");
return false;
}
struct Node* temp1 = p->next;
struct Node* temp2 = temp1->next;//后一个节点
p->next = temp2;
temp1->next = NULL;
free(temp1);
return true;
}
//尾插法
status loop_back_insert(struct Node* p,int data){
if(p->next==p){//链表为空
struct Node* new = (struct Node*)malloc(sizeof(struct Node));
if(new==NULL){
return false;
}
new->num = data;
p->next = new;
new->next = p;
return true;
}
struct Node* new = (struct Node*)malloc(sizeof(struct Node));
if(new==NULL){
return false;
}
struct Node* tmp = p;
while (tmp->next!=p)
{
tmp = tmp->next;
}
new->num = data;
tmp->next = new;
new->next = p;
return true;
}
int main()
{
struct Node* L = loop_creat();
loop_head_insert(L,5);
loop_head_insert(L,4);
loop_head_insert(L,3);
loop_head_insert(L,2);
loop_head_insert(L,1);
loop_travel(L);
printf("\n***************\n");
loop_delete_head(L);
loop_delete_head(L);
loop_travel(L);
printf("\n***************\n");
loop_back_insert(L,6);
loop_back_insert(L,7);
loop_back_insert(L,8);
loop_travel(L);
return 0;
}
3. 实现链表的反转
linear_resver.c
cpp
复制代码
#include "stdio.h"
#include "stdlib.h"
#include "stdbool.h"
typedef bool status;
//实现链表的反转
struct Node
{
int a;
struct Node *next;
};
struct Node *Linear_List_Create()
{
struct Node *p=(struct Node *)malloc(sizeof(struct Node));
if(p==NULL)
{
return false;
}
p->a=0;
p->next=NULL;
return p;
}
status Linear_List_Insert_Head(struct Node *p,int data)
{
if(p->next==NULL)
{
struct Node *new=(struct Node *)malloc(sizeof(struct Node));
if(new==NULL)
{
return false;
}
new->a=data;
new->next=NULL;
p->next=new;
return true;
}
struct Node *new=(struct Node *)malloc(sizeof(struct Node));
if(new==NULL)
{
return false;
}
new->a=data;
struct Node *temp=p->next;
p->next=new;
new->next=temp;
return true;
}
status Linear_List_Travel(struct Node*p)
{
if(p->next==NULL)
{
return false;
}
struct Node *tra=p;
while(tra->next)
{
tra=tra->next;
printf("数据:%d\n",tra->a);
}
return true;
}
//实现链表的反转
status Linear_List_Reverse(struct Node *p)
{
//(1)如果只有一个结点
//(2)如果是空链表
struct Node *p1=p->next;
struct Node *q1=p->next->next;
while(q1)
{
p1->next=q1->next;
q1->next=p->next;
p->next=q1;
q1=p1->next;
}
Linear_List_Travel(p);
return true;
}
int main()
{
struct Node *L=Linear_List_Create();
Linear_List_Insert_Head(L,4);
Linear_List_Insert_Head(L,3);
Linear_List_Insert_Head(L,2);
Linear_List_Insert_Head(L,1);
Linear_List_Travel(L);
//单链表的反转
Linear_List_Reverse(L);
return 0;
}