链表的基础操作

#include<iostream>
#include<iomanip>
#include<cmath>
#include<cstdio>
using namespace std;
#define Maxsize 100
#define ElemType int 
#define Status int 
typedef long long ll;
typedef struct Node
{
	int data;//数据存储
	struct Node *next;//指针域
}Node,*List;

List Init_List()
{
	Node *ls;
	ls=(Node*)malloc(sizeof(Node));//申请结点空间
	if(ls==NULL) exit(0);//如果失败则退出程序
	ls->next=NULL;
	return ls;
}
//头插法创建链表
List Creat_ls_head()
{
	Node *ls;
	ls=(Node*)malloc(sizeof(Node));
	ls->next=NULL;
	int x;
	while(scanf("%d",&x))
	{
		if(x==-1) break;
		Node *p;
		p=(Node*)malloc(sizeof(Node));
		p->data=x;//将该节点赋值为x
		//ls-NULL--L-p--NULL
		p->next=ls->next;
		ls->next=p;//更新一下L的指针永远指向头部
		cout<<"结点创建完成!"<<endl;
	}
	return ls; 
}

//尾插法创建链表
List Creat_ls_tail()
{
	Node *ls;
	ls=(Node*)malloc(sizeof(Node));
	ls->next=NULL;
	int x;
	//设置一个尾节点r
	Node *r;
	r=ls;
	while(scanf("%d",&x))
	{
		if(x==-1) break;
		Node *p;
		p=(Node*)malloc(sizeof(Node));
		p->data=x;//将该节点赋值为x
		r->next=p;
		//r要永远代表最后一个节点
		r=p;
		cout<<"结点创建完成!"<<endl;
	}
	r->next=NULL;
	//cout<<"OK"<<endl;
	return ls; 
}
//打印链表
void ls_display(List L)
{
	Node *p=L->next;
	int cnt=0;
	while(p)
	{
		cnt++;
		cout<<"第"<<cnt<<"个元素的值为:"<<p->data<<endl;
		//p指针下移一个
		p=p->next;
	}
}
//对于元素进行修改
List ls_replace(List ls,int x,int y)
{
	Node *p=ls->next;
	int cnt=0;
	while(p)
	{
		if(p->data==x) p->data=y;//将所有的x替换成y;
		p=p->next;
	}
	return ls;
}
List ls_insert(List ls,int i,int x)
{
	Node *p;
	p=ls;
	//将指针指第i个位置的前驱	
	for(int ti=0;ti<i;ti++)  p=p->next;
	Node *q;
	q=(Node*)malloc(sizeof(Node));//插入的结点为q
	q->data=x;
	q->next=p->next;
	p->next=q;
	return ls;
}
//删除值为x的元素
List ls_delete(List ls,int x)
{
	Node *p,*q;
	p=ls->next;
	while(p->data!=x)
	{
		q=p;
		p=p->next;
	}
	//此时已经找到的话
	q->next=p->next;
	free(p);
	return ls;
}
int main()
{
	//①利用尾插法进行创建链表
	List ls1;
	cout<<"****************************************"<<endl;
	cout<<"请输入单链表的数据:以-1结尾"<<endl;
	ls1=Creat_ls_tail();
	ls_display(ls1);
	cout<<"****************************************"<<endl;
	//②利用头插法进行创建链表
	List ls2;
	cout<<"****************************************"<<endl;
	cout<<"请输入单链表的数据:以-1结尾"<<endl;
	ls2=Creat_ls_head();
	ls_display(ls2);
	cout<<"****************************************"<<endl;
	//执行插入操作
	int i;
	int x;
	cout<<"****************************************"<<endl;
	cout<<"请输入插入数据的位置:";
	cin>>i;
	cout<<"请输入插入的数值大小:";
	cin>>x;
	cout<<"原序列的表示如下所示:"<<endl;
	ls_display(ls2);
	cout<<"现序列的表示如下所示:"<<endl;
	ls_insert(ls2,i,x);
	ls_display(ls2);
	cout<<"****************************************"<<endl;
	//执行修改操作
	cout<<"****************************************"<<endl;
	cout<<"请输入原来数据的值:";
	cin>>i;
	cout<<"请输入修改的数值大小:";
	cin>>x;
	cout<<"原序列的表示如下所示:"<<endl;
	ls_display(ls2);
	cout<<"现序列的表示如下所示:"<<endl;
	ls_replace(ls2,i,x);
	ls_display(ls2);
	cout<<"****************************************"<<endl;
	//执行删除操作
	cout<<"****************************************"<<endl;
	cout<<"请输入需要删除的数据的值:";
	cin>>x;
	cout<<"原序列的表示如下所示:"<<endl;
	ls_display(ls2);
	cout<<"现序列的表示如下所示:"<<endl;
	ls_delete(ls2,x);
	ls_display(ls2);
	cout<<"****************************************"<<endl;
	cout<<"完成所有链表的操作!"<<endl;
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

温柔济沧海

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值