单链表的基本操作c++

头节点:第一个节点,一般不储存数据。

头指针:指向第一个节点。

首元节点:第一个储存数据的节点。

可以通过头节点或头指针指向整个链表。

链表的初始化:::::

前插法

//前插法	
node *initlink()
{
	node *p=(node*)malloc(sizeof(node));//创建头节点
	p->next=NULL;
	
	for(int i=0;i<n;i++)
	{
		node *a=(node*)malloc(sizeof(node));//创建一个新节点用来储存数据
		cin>>a->date;
		a->next=p->next;//将新节点与链表建立联系
		p->next=a;//将新节点插入链表最前端
	}
	return p;
}

尾插法

//尾插法
node *initlink()
{
	node *p=(node*)malloc(sizeof(node));//创建头节点
	node *temp=p;//创建一个指针指向头节点
	
	for(int i=0;i<n;i++)
	{
		node *a=(node*)malloc(sizeof(node));//创建一个新的节点
		cin>>a->date;
		a->next=NULL;
		temp->next=a;//将新节点与链表建立联系,同时新节点插入到链表的最后
		temp=a;//指针指向新节点,方便下次插入新节点
	}
	return p;//返回整个链表
}

通过前插法创建完整链表

#include<iostream>
using namespace std;
typedef struct node 
{
	int date;
	struct node *next;
}node;
node *initlist(int n)
{
	node *p=(node*)malloc(sizeof(node));
	p->next=NULL;
	for(int i=1;i<=n;i++)
	{
		node *a=(node*)malloc(sizeof(node));
		cin>>a->date;
		a->next=p->next;
		p->next=a;
	}
	return p;
}
int main()
{
	int n;cin>>n;
	node *L=initlist(n);//创建一个链表L并引用函数向链表里添加元素
	node *temp=L->next;//创建一个临时指针,因为链表有头节点所以指向链表第二个节点
	while(temp)//遍历链表并输出所有数据
	{
		cout<<temp->date<<" ";
		temp=temp->next;
	}
	return 0;
}

运行结果

通过尾插法创建完整链表

#include<iostream>
using namespace std;
typedef struct node
{
	int date;
	struct node *next;
}node;
node *initlist(int n)
{
	node *p=(node*)malloc(sizeof(node));
	node *temp=p;
	for(int i=1;i<=n;i++)
	{
		node *a=(node*)malloc(sizeof(node));
		cin>>a->date;
		a->next=NULL;
		temp->next=a;
		temp=a;
		
	}
	return p;
}
int main()
{
	int n;cin>>n;
	node *L=initlist(n);//创建一个链表L并引用函数向链表里添加元素
	node *temp=L->next;//因为链表里有头节点所以指向第二个节点
	while(temp)//遍历链表输出所以数据
	{
		cout<<temp->date<<" ";
		temp=temp->next;
	}
	return 0;
}

运行结果:

在链表中插入数据:

//在链表中插入数据(add处插入m这个数据)
node *insertlink(node *L,int m,int add)
{
	node *temp=L;//创建一个临时指针
	for(int i=1;i<add;i++)//查找插入位置的节点的前一个节点
	{
		temp=temp->next;//指向插入位置的前一个节点
		if(temp==NULL)//如果该节点为空
		{
			cout<<"输入的位置无效"<<endl;
			return L;//返回原链表
		}
	}
	node *a=(node*)malloc(sizeof(node));//创建一个临时节点
	a->date=m;//储存数据
	a->next=temp->next;//类似前插法,先于链表建立联系
	temp->next=a;
	return L;//返回处理后的链表
}

 

在链表中删除某个数据:

//在链表中删除某个数据(删除add处元素并用n记录)
node *dellink(node *L,int *n,int add)
{
	node *temp=L;//创建一个临时指针指向链表
	for(int i=1;i<add;i++)//查找链表中该节点前一个节点的位置
	{
		temp=temp->next;
		if(temp->next==NULL)//当add处节点为空时
		{
			cout<<"该数据不存在"<<endl
			return L;//返回原链表
		}
	}
	node *a=temp->next;//建立一个临时节点
	temp->next=temp->next->next;//删除该节点的方法是改变前一个节点的指针域
	*n=a->date;//用n来记录删除节点的数据
	free(a);//释放a节点的内存
	return L;//返回处理后的链表
}

在链表中查找某个元素:

//在链表中查找某个元素的位置
int selecdate(node *L,int num)
{
	node *temp=L;//建立一个临时指针指向链表
	int i=1;
	while(temp->next)
	{
		temp=temp->next;//因为有头节点的存在所以指向下一个节点
		if(temp->date==num)//找到该元素就返回该元素的位置
			return i;
		i++;
	}
	return -1;//当遍历了整个元素后都没有找到该元素的时候返回-1
}

 

 在链表中更新某个元素:

//将链表中add处的数据该为n
node *amenddate(node *L,int add,int n)
{
	node *temp=L;//建立一个临时指针
	temp=temp->date;//让指针指向首元节点
	for(int i=1;i<add;i++)//遍历到add处的前一个节点
	{
		temp=temp->next;//让temp指向add处的节点
	}
	temp->date=n;//让该节点的储存数据改为n
	return L;//返回处理后的链表
}

 

 

对链表进行整表删除:

初始条件:链表L已经存在。操作结果:将L重置为空表。

int clearlist(node *L)
{
	node *p,*q;
	p=L->next;
	while(p)
	{
		q=p->next;
		free(p);
		p=q;
	}
	L->next=NULL;
	return 1;
	
}

 

 

对链表中的数据元素进行增加,插入,删除,查找,改变的操作的完整代码

#include<bits/stdc++.h>
using namespace std;
typedef struct node
{
	int date;
	struct node *next;
}node;
void display(node *L)//输出整个链表所以元素的函数 
{
	node *temp=L->next;
	while(temp)
	{
		cout<<temp->date<<" ";
		temp=temp->next;
	}
	cout<<endl;
}
node *initlist()//用尾插法创建完整链表 
{
	node *p=(node*)malloc(sizeof(node));
	node *temp=p;
	for(int i=1;i<=4;i++)
	{
		node *a=(node*)malloc(sizeof(node));
		a->date=i;
		a->next=NULL;
		temp->next=a;
		temp=a;
	 } 
	 return p;
}
void insertl(node *L,int add,int n)// 在某个位置插入一个元素 
{
	node *temp=L;
	for(int i=1;i<add;i++)
	{
		temp=temp->next;
	}
	node *a=(node*)malloc(sizeof(node));
	a->date=n;
	a->next=temp->next;
	temp->next=a;
}
void dell(node *L,int n,int *m)//删除某个元素 
{
	node *temp=(node*)malloc(sizeof(node));
	temp=L->next;
	int i=1;
	while(temp)
	{
		if(temp->next->date==n)
		{
			*m=i+1;//函数中改变参数的值要用指针 
			break;
		}
		temp=temp->next;
		i++;
	}
	temp->next=temp->next->next;

}
int selectl(node *L,int n)// 查找某个元素的位置 
{
	node *temp=(node*)malloc(sizeof(node));
	temp=L->next;
	int i=1;
	while(temp)
	{
		if(temp->date==n)
			return i;
		temp=temp->next;
		i++;
	}
	return 0;
}
void amendl(node *L,int n,int m)//改变某个位置的元素 
{
	node *temp=(node*)malloc(sizeof(node));
	temp=L;
	for(int i=1;i<n;i++)
	{
		temp=temp->next;
	}
	node *a=(node*)malloc(sizeof(node));
	a->date=m;
	a->next=temp->next->next;
	temp->next=a;
	
}
int main()
{
	node *L=initlist();
	cout<<"初始化链表为:"<<endl; 
	display(L);
	
	int add,n;
	printf("输入插入元素的位置和元素:\n"); 
	cin>>add>>n;
	printf("在第%d的位置插入元素%d:\n",add,n);
	insertl(L,add,n);
	display(L);
	
	int n1,m1;
	printf("输入删除的元素:\n"); 
	cin>>n1;
	dell(L,n1,&m1);//因为函数中要该表m1的值,所以此处调用的是m1的地址 
	printf("删除元素%d,它的位置是%d:\n",n1,m1);	
	display(L);
	
	int n2,m2;
	printf("输入查找的元素:\n");
	cin>>n2;
	m2=selectl(L,n2);	
	printf("查找元素%d的位置: ",n2);
	cout<<m2<<endl;
	
	int n3,m3;
	printf("输入改变元素的位置和新的元素:\n");
	cin>>n3>>m3;
	amendl(L,n3,m3);
	printf("更改第%d的位置上的数据为%d:\n",n3,m3);
	display(L);
	return 0;
}

运行结果

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值