链表的基本操作

1.创建链表

#include <iostream>
#include <stdlib.h>//用于malloc, free 内存申请及释放 

using namespace std;

struct List
{
	int data;// 数据
	struct List* next;//存入下一个结点的地址 
};

/*链表的创建*/
void chreat(struct List *L)
{
	int m;
	cin>>m;//存要插入的值
	while(m) //输入0停止 
	{
		struct List* p = (struct List *)malloc(sizeof(struct List));  //申请动态空间 
		p->data = m;
		p->next = NULL;
		
		L->next = p;//与链表连接 
		L = L->next; //此时L指向p 
		cin>>m;
	} 
} 

/*遍历链表*/
void show(struct List* L)
{
	cout<<"输出结果为:";
	while(L->next)
	{
		cout<<L->next->data<<' ';
		L = L->next;
	} 
	cout<<endl;
}

int main()
{
	struct List* head_list = (struct List *)malloc(sizeof(struct List)); //头节点
	head_list->next = NULL;
	cout<<"输入的值为:"; 
	chreat(head_list); //初始化链表,将地址传入函数 
	show(head_list);
	return 0;
} 

在这里插入图片描述

2.插入链表

#include <iostream>
#include <stdlib.h>//用于malloc, free 内存申请及释放 

using namespace std;

struct List
{
	int data;// 数据
	struct List* next;//存入下一个结点的地址 
};

/*链表的创建*/
void chreat(struct List *L)
{
	int m;
	cin>>m;//存要插入的值
	while(m) //输入0停止 
	{
		struct List* p = (struct List *)malloc(sizeof(struct List));  //申请动态空间 
		p->data = m;
		p->next = NULL;
		
		L->next = p;//与链表连接 
		L = L->next; //此时L指向p 
		cin>>m;
	} 
} 

/*遍历链表*/
void show(struct List* L)
{
	cout<<"输出结果为:";
	while(L->next)
	{
		cout<<L->next->data<<' ';
		L = L->next;
	} 
	cout<<endl;
}

/*插入链表*/
void add(struct List* L,int k,int m)  //在链表第k个元素前插入元素m; 
{
	struct List* q = NULL;  //存储第k-1个元素的值
	struct List* p = (struct List *)malloc(sizeof(struct List)); //申请动态空间
	p->data = m;
	p->next = NULL;
	
	while(k--&&L->next) //查找第k-1个结点的位置,让q储存一下,908且此时L->next不为空 ,是从头节点开始循环的 
	{
		q = L;
		L = L->next;
	} 
	if(k>0)//说明此时链表已经循环完毕了,直接在最后插入就行了
	{
		L->next = p;// 此时L指的是最后一个元素
	} 
	else
	{
		q->next = p;
		p->next = L;
	}
} 

int main()
{
	int k,m;
	struct List* head_list = (struct List *)malloc(sizeof(struct List)); //头节点
	head_list->next = NULL;
	cout<<"输入的值为:"; 
	chreat(head_list); //初始化链表,将地址传入函数 
	cout<<"请输入第几位前插入元素几:"<<endl;
	cin>>k>>m;
	add(head_list,k,m); 
	show(head_list);
	return 0;
} 

在这里插入图片描述

1.后插入链表

#include <iostream>

using namespace std;

/*创建结构体*/
struct List
{
	int data; // 数据 
	struct List* next;  //存入地址 
};

/*链表的建造*/
void ba_add(struct List* &head)
{
	cout<<"请输入数据:"; 
	struct List* q = head;
	int m;
	cin>>m;
	/*后插入法*/
	while(m) //当输入0时停止存储
	{
		struct List* p = (struct List *)malloc(sizeof(struct List)); //创建新结点,分配动态空间
		p->data = m; 
		p->next = NULL;
		
		q->next = p;
		q = p;
		cin>>m;
	} 
	
} 

/*输出链表*/
void show(struct List* &head)
{
	cout<<"输出结果为:";
	while(head->next)
	{
		cout<<head->next->data<<' ';
		head = head->next;
	}
	cout<<endl;
}

int main()
{
	//创造头节点 
	struct List* head = (struct List *)malloc(sizeof(struct List)); 
	head->next = NULL;
	ba_add(head);
	show(head);
	return 0;
} 

在这里插入图片描述

2.前插入链表

#include <iostream>

using namespace std;

/*创建结构体*/
struct List
{
	int data; // 数据 
	struct List* next;  //存入地址 
};

/*链表的建造*/
void ba_add(struct List* &head)
{
	cout<<"请输入数据:"; 
	struct List* L = head;
	int m;
	cin>>m;
	/*前插入法*/
	while(m) //当输入0时停止存储
	{
		struct List* p = (struct List *)malloc(sizeof(struct List)); //创建新结点,分配动态空间
		p->data = m; 
		p->next = NULL;
		
		p->next = L->next;//L指向q的指向的节点 
		L->next = p;//L指向p,即将p插入到头节点后; 
		cin>>m;
	} 
	
} 

/*输出链表*/
void show(struct List* &head)
{
	cout<<"输出结果为:";
	while(head->next)
	{
		cout<<head->next->data<<' ';
		head = head->next;
	}
	cout<<endl;
}

int main()
{
	//创造头节点 
	struct List* head = (struct List *)malloc(sizeof(struct List)); 
	head->next = NULL;
	ba_add(head);
	show(head);
	return 0;
} 

在这里插入图片描述

3.删除链表

#include <iostream>
#include <stdlib.h>//用于malloc, free 内存申请及释放 

using namespace std;

struct List
{
	int data;// 数据
	struct List* next;//存入下一个结点的地址 
};

/*链表的创建*/
void chreat(struct List *L)
{
	int m;
	cin>>m;//存要插入的值
	while(m) //输入0停止 
	{
		struct List* p = (struct List *)malloc(sizeof(struct List));  //申请动态空间 
		p->data = m;
		p->next = NULL;
		
		L->next = p;//与链表连接 
		L = L->next; //此时L指向p 
		cin>>m;
	} 
} 

/*遍历链表*/
void show(struct List* L)
{
	cout<<"输出结果为:";
	while(L->next)
	{
		cout<<L->next->data<<' ';
		L = L->next;
	} 
	cout<<endl;
}

void del(struct List* &head_list, int i)
{
	int j=0;
	struct List *q; 
	struct List *p = head_list;
	while((p->next)&&(j<i-1)) // 查找第i-1个结点 
	{
		p = p->next;
		++j;
	}
	if(!(p->next)||j>i-1)  cout<<"此处删除位置不合理";  //当i>n或i<1时,删除位置不合理 
	q = p->next; //临时保存被删结点的位置以备释放 
	p->next = q->next;
	free(q);//释放q节点 
}

int main()
{
	int i;
	struct List* head_list = (struct List *)malloc(sizeof(struct List)); //头节点
	head_list->next = NULL;
	cout<<"输入的值为:"; 
	chreat(head_list); //初始化链表,将地址传入函数 
	cout<<"请输入要删除第几位:"<<endl;
	cin>>i;
	del(head_list,i);
	show(head_list);
	return 0;
} 

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值