c语言-单链表的基本操作(删除、增加、顺序、逆序)

下面包括
1、链表的建立(顺序+逆序)
2、删除结点(头、中、尾
3、增加结点(头、中、尾

(这里没有考虑到空链表的情况,并且初链表只有5个结点,之后运用的时候要修改控制输入的语句)

删除结点

void delete_(struct p*head) //删除结点 
{
	struct p *a,*b;
	int num,c=0;
	cin>>num;
	if(num==0)//删除头部 
		head=head->next ;
	else
	{
		for(a=head;a!=NULL;a=a->next )
		{//删除在中部或尾部的
			if(c==num)
				b->next =a->next ;
			else 
				b=a;
			c++;
		}
	}

增加结点

void add(struct p*head)//增加结点 
{
	struct p *a,*b,*cha;
	cha=(struct p *)malloc(sizeof(struct p));
	int num,temp;//num是插入的位置(下标从0开始),temp是要插入的值
	cin>>num>>temp; 
	cha->n=temp;
	cha->next =NULL; 
	if(num==0)//增加在头部前 
	{
		cha->next =head;
		head=cha;
	}
	else
	{
		for(a=head;a!=NULL;a=a->next,c++)
		{	
			if(a->next ==NULL)//在尾部插入(插在后面 
			{
				a->next =cha;
				cha->next =NULL;
				break;
			}
			if(c==num)//中间插入 (写的时候卡住了) 
			{
				b=(struct p *)malloc(sizeof(struct p));
				b=a->next ;
				a->next=cha;
				cha->next=b ;
				break;
			}
	}

头插(逆序)

for(int i=0;i<5;i++)//逆序建立 (头插) 
{
	a=(struct p *)malloc(sizeof(struct p));//每次使用都要开辟新的空间 
	cin>>a->n;//对 a 开辟出的结构体进行输入 
	a->next =head;
	head=a;
}

尾插(顺序)

for(int i=0;i<5;i++)//顺序建立 (尾插) 
		{
			a=(struct p *)malloc(sizeof(struct p));//每次使用都要开辟新的空间 
			cin>>a->n;//对 a 开辟出的结构体进行输入 
			a->next=NULL;//a 的结点指向空指针 
			if(head==NULL)//head只起记录链表头作用,之后不再动 head 
				head=a;//将第一个结点地址给 head 
			else 
				ptr->next=a;//之后 就是 ptr 与 a 之间轮换 
			ptr=a;
		}

总代码:

#include<iostream>
#include<stdlib.h>
#include<cstdio>
using namespace std;
struct p{
	int n;
	struct p *next;
} *head=NULL,*ptr,*a;
void delete_(struct p*head) //删除结点 
{
	struct p *a,*b;
	cout<<"(删除下标从0开始)"<<endl<<"你要删除的结点位置:"; 
	int num,cout=0;
	cin>>num;
	if(num==0)//删除头部 
		head=head->next ;
	else
	{
		for(a=head;a!=NULL;a=a->next )
		{
			if(cout==num)
				b->next =a->next ;
			else 
				b=a;
			cout++;
		}
	}
	for(ptr=head;ptr !=NULL;ptr=ptr->next ) //输出链表 
		printf("%d ",ptr->n );;
}
void add(struct p*head)//增加结点 
{
	struct p *a,*b,*cha;
	cha=(struct p *)malloc(sizeof(struct p));
	cout<<"下标从0开始;"<<endl; 
	cout<<"若输入0 增加在头部之前"<<endl<<"若输入4 增加在尾部之后"<<endl<<"若输入1-3 "<<"增加在该结点之前"<<endl<<endl; 
	cout<<"输入你的选择:";
	int num,c=1;
	cin>>num;
	printf("\n输入要插入的数据 ");
	int temp;
	cin>>temp; 
	cha->n=temp;
	cha->next =NULL; 
	printf("\n");
	if(num==0)//增加在头部前 
	{
		cha->next =head;
		head=cha;
	}
	else
	{
		for(a=head;a!=NULL;a=a->next,c++)
		{	
			if(a->next ==NULL)//在尾部插入(插在后面 
			{
				a->next =cha;
				cha->next =NULL;
				break;
			}
			if(c==num)//中间插入 (写的时候卡住了) 
			{
				b=(struct p *)malloc(sizeof(struct p));
				b=a->next ;
				a->next=cha;
				cha->next=b ;
				break;
			}
//			cout<<"*"<<c<<endl;
		}
	}
	for(ptr=head;ptr!=NULL;ptr=ptr->next ) //输出链表 
		printf("%d ",ptr->n);
}

int main()
{
	int option;
	cout<<"本链表仅建立5个结点"<<endl<<endl;
	cout<<"选择创建方法:"<<endl;
	cout<<"1 : 尾插_顺序建立"<<endl<<"2 : 头插_逆序建立"<<endl<<endl; 
	cout<<"你选择的是方法:" ;
	cin>>option;
	cout<<endl<<"输入你的数据:"<<endl;
	if(option==1)
	{
		for(int i=0;i<5;i++)//顺序建立 (尾插) 
		{
			a=(struct p *)malloc(sizeof(struct p));//每次使用都要开辟新的空间 
			cin>>a->n;//对 a 开辟出的结构体进行输入 
			a->next=NULL;//a 的结点指向空指针 
			if(head==NULL)//head只起记录链表头作用,之后不再动 head 
				head=a;//将第一个结点地址给 head 
			else 
				ptr->next=a;//之后 就是 ptr 与 a 之间轮换 
			ptr=a;
		}
		cout<<endl; 
		cout<<"输出链表:"<<endl;
		for(ptr=head;ptr !=NULL;ptr=ptr->next ) //输出链表 
			cout<<ptr->n <<" ";
		cout<<endl;
	}
	if(option==2)//逆序建立 
	{
		for(int i=0;i<5;i++)//逆序建立 (头插) 
		{
			a=(struct p *)malloc(sizeof(struct p));//每次使用都要开辟新的空间 
			cin>>a->n;//对 a 开辟出的结构体进行输入 
			a->next =head;
			head=a;
		}
		cout<<endl; 
		cout<<"输出链表:"<<endl;
		for(ptr=head;ptr !=NULL;ptr=ptr->next ) //输出链表 
			cout<<ptr->n <<" "; 
	}
	int op;
	cout<<endl;
	cout<<"选择接下来的操作:"<<endl<<"1、删除结点"<<endl<<"2、增加结点"<<endl<<"3、结束操作"<<endl<<endl; 
	cout<<"输入你的选择:"; 
	cin>>op;
	cout<<endl;
	if(op==1) delete_(head); 
	if(op==2) add(head);
	if(op==3) return 0;
	return 0;
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值