作业4双链表

这篇博客介绍了如何实现整数双链表的基本操作,包括创建、输出、去重以及删除指定区间的元素。通过示例代码展示了头插法、尾插法创建链表,以及如何删除重复元素和特定范围内的节点。同时,讨论了单链表中负数、零、正数的排序问题和删除指定区间节点的算法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1:整数双链表的基本运算-3

总时间限制: 10000ms 内存限制: 10000kB
描述
设计整数双链表的基本访问程序,并用相关数据进行测试

输入
顺序输入双链表A的各个元素
输出
第一行:创建双链表A后,按输入顺序输出所有元素
第二行:按输入反顺序输出双链表A的所有元素
样例输入
1 2 3 4 0 9
样例输出
1 2 3 4 0 9
9 0 4 3 2 1

#include<iostream>
#include<cstdlib>
using namespace std;
const int MAXSIZE=100;
#define ElemType int 

typedef struct node{
	ElemType data;
	struct node *prior;
	struct node *next;
}DLinkNode;
DLinkNode *L;
bool InitList(DLinkNode *&L)
{
	L=(DLinkNode *)malloc(sizeof(DLinkNode));
	if(!L)
		return false;
	L->next=NULL;
}
void CreateListF(DLinkNode *&L,ElemType a[],int n)//头插法 
{
	DLinkNode *s;
	int i;
	L=(DLinkNode *)malloc(sizeof(DLinkNode));
	L->next=NULL;
	for(i=0;i<n;i++)
	{
		s=(DLinkNode *)malloc(sizeof(DLinkNode));
		s->data=a[i];
		s->next=L->next;
		s->prior=L;
		if(L->next!=NULL)
		{
			L->next->prior=s; 
		}
		L->next=s;
	}
}
void CreateListR(DLinkNode *&L,ElemType a[],int n)//尾插法 
{
	DLinkNode *s,*tc;
	int i;
	L=(DLinkNode *)malloc(sizeof(DLinkNode));
	tc=L;
	for(i=0;i<n;i++)
	{
		s=(DLinkNode *)malloc(sizeof(DLinkNode));
		s->data=a[i];
		tc->next=s;
		s->prior=tc;
		tc=s;
	}
	tc->next=NULL;
}
void DispList(DLinkNode *&L)
{
	DLinkNode *p=L->next;
	while(p!=NULL)
	{
		cout<<p->data<<" ";
		p=p->next;
	}
	cout<<endl;
}
void DispList1(DLinkNode *&L)
{
	DLinkNode *p=L->next;
	int i=1;
	while(p->next!=NULL)
	{	
		p=p->next;
		i++;
	}
	while(i!=0)
	{
		cout<<p->data<<" ";
		p=p->prior;
		i--;
	}
	cout<<endl;
}
int GetLength(DLinkNode *L)
{
	int i=0;
	DLinkNode *p=L->next;
	while(p!=NULL)
	{
		i++;
		p=p->next;
	}
	return i;
}
bool GetElem(DLinkNode *&L,int i,ElemType &e)
{
	int j=0;
	DLinkNode *p=L;
	if(i<=0)
		return false;
	while(j<i&&p!=NULL)
	{
		j++;
		p=p->next;
	}
	if(p==NULL)
		return false;
	else{
		e=p->data;
		return true;
	}
}
int LocateElem(DLinkNode *&L,ElemType e)
{
	int i=1;
	DLinkNode *p=L->next;
	while(p!=NULL&&p->data!=e)
	{
		p=p->next;
		i++;
	}
	if(p==NULL)
		return (0);
	else
		return (i);
}
bool ListEmpty(DLinkNode *L)
{
	return(L->next==NULL);
}

bool ListInsert(DLinkNode *&L,int i,ElemType e)
{
	int j=0;
	DLinkNode *p=L,*s;
	if(i<=0)
		return false;
	while(j<i-1&&p!=NULL)
	{
		j++;
		p=p->next;
	}
	if(p==NULL)
		return false;
	else{
		s=(DLinkNode *)malloc(sizeof(DLinkNode));
		s->data=e;
		s->next=p->next;
		if(p->next!=NULL)
			p->next->prior=s;
		s->prior=p;
		p->next=s;
		return true;
	}
		
}
bool ListDelete(DLinkNode *&L,int i,ElemType &e)
{
	int j=0;
	DLinkNode *p=L,*pre;
	while(j<i-1&&p!=NULL)
	{
		j++;
		p=p->next;
	}
	if(p==NULL)
		return false;
	else
	{
		pre=p->prior;
		if(p->next!=NULL)
			p->next->prior=pre;
		pre->next=p->next;
		free(p);
		return true; 
	}
}
void DestoryList(DLinkNode *&L)
{
	DLinkNode *pre=L,*p=L->next;
	while(p!=NULL)
	{
		free(pre);
		pre=p;
		p=pre->next;
	}
	free(pre);
}

int main()
{
	DLinkNode *L;
	ElemType e;
	InitList(L);
	int n=0,i=0;
	int a[MAXSIZE];
	do
	{
		cin>>a[i];
		i++;
		n++;
	}while(getchar()!='\n');
	CreateListR(L,a,n);
	DispList(L);
	DispList1(L);
	DestoryList(L);
}

2:整数双链表的基本运算-4

总时间限制: 10000ms 内存限制: 10000kB
描述
设计整数双链表的基本访问程序,删除整数双链表中的重复元素,多个值相同的元素只保留第一个,并用相关数据进行测试

输入
顺序输入双链表A的各个元素
输出
第一行:创建双链表A后,输出所有元素
第二行:删除整数双链表中的重复元素,多个值相同的元素只保留第一个,然后输出去重后的双链表A的所有元素
样例输入
0 1 2 4 3 2 4 4 0 9 9
样例输出
0 1 2 4 3 2 4 4 0 9 9
0 1 2 4 3 9

#include<iostream>
#include<cstdlib>
using namespace std;
const int MAXSIZE=100;
#define ElemType int 

typedef struct node{
	ElemType data;
	struct node *prior;
	struct node *next;
}DLinkNode;
DLinkNode *L;
bool InitList(DLinkNode *&L)
{
	L=(DLinkNode *)malloc(sizeof(DLinkNode));
	if(!L)
		return false;
	L->next=NULL;
}
void CreateListF(DLinkNode *&L,ElemType a[],int n)//头插法 
{
	DLinkNode *s;
	int i;
	L=(DLinkNode *)malloc(sizeof(DLinkNode));
	L->next=NULL;
	for(i=0;i<n;i++)
	{
		s=(DLinkNode *)malloc(sizeof(DLinkNode));
		s->data=a[i];
		s->next=L->next;
		s->prior=L;
		if(L->next!=NULL)
		{
			L->next->prior=s; 
		}
		L->next=s;
	}
}
void CreateListR(DLinkNode *&L,ElemType a[],int n)//尾插法 
{
	DLinkNode *s,*tc;
	int i;
	L=(DLinkNode *)malloc(sizeof(DLinkNode));
	tc=L;
	for(i=0;i<n;i++)
	{
		s=(DLinkNode *)malloc(sizeof(DLinkNode));
		s->data=a[i];
		tc->next=s;
		s->prior=tc;
		tc=s;
	}
	tc->next=NULL;
}
void DispList(DLinkNode *&L)
{
	DLinkNode *p=L->next;
	while(p!=NULL)
	{
		cout<<p->data<<" ";
		p=p->next;
	}
	cout<<endl;
}
int GetLength(DLinkNode *L)
{
	int i=0;
	DLinkNode *p=L->next;
	while(p!=NULL)
	{
		i++;
		p=p->next;
	}
	return i;
}
bool GetElem(DLinkNode *&L,int i,ElemType &e)
{
	int j=0;
	DLinkNode *p=L;
	if(i<=0)
		return false;
	while(j<i&&p!=NULL)
	{
		j++;
		p=p->next;
	}
	if(p==NULL)
		return false;
	else{
		e=p->data;
		return true;
	}
}
int LocateElem(DLinkNode *&L,ElemType e)
{
	int i=1;
	DLinkNode *p=L->next;
	while(p!=NULL&&p->data!=e)
	{
		p=p->next;
		i++;
	}
	if(p==NULL)
		return (0);
	else
		return (i);
}
bool ListEmpty(DLinkNode *L)
{
	return(L->next==NULL);
}

bool ListInsert(DLinkNode *&L,int i,ElemType e)
{
	int j=0;
	DLinkNode *p=L,*s;
	if(i<=0)
		return false;
	while(j<i-1&&p!=NULL)
	{
		j++;
		p=p->next;
	}
	if(p==NULL)
		return false;
	else{
		s=(DLinkNode *)malloc(sizeof(DLinkNode));
		s->data=e;
		s->next=p->next;
		if(p->next!=NULL)
			p->next->prior=s;
		s->prior=p;
		p->next=s;
		return true;
	}
		
}
void delsame(DLinkNode *&L)
{
	DLinkNode *p,*q,*s;
	p = L->next;

	for(p;p!=NULL;p=p->next)
	{
		s=p;                   
		for(q=p->next;q!=NULL; )
		{
			if(q->data==p->data)
			{
				s->next=q->next;
				free(q);
				q=s->next;
			}
			else
			{
				s=q;
				q=q->next;
			}
		}
	}
	
}
bool ListDelete(DLinkNode *&L,int i,ElemType &e)
{
	int j=0;
	DLinkNode *p=L,*pre;
	while(j<i-1&&p!=NULL)
	{
		j++;
		p=p->next;
	}
	if(p==NULL)
		return false;
	else
	{
		pre=p->prior;
		if(p->next!=NULL)
			p->next->prior=pre;
		pre->next=p->next;
		free(p);
		return true; 
	}
}
void DestoryList(DLinkNode *&L)
{
	DLinkNode *pre=L,*p=L->next;
	while(p!=NULL)
	{
		free(pre);
		pre=p;
		p=pre->next;
	}
	free(pre);
}

int main()
{
	DLinkNode *L;
	ElemType e;
	InitList(L);
	int n=0,i=0;
	int a[MAXSIZE];
	do
	{
		cin>>a[i];
		i++;
		n++;
	}while(getchar()!='\n');
	CreateListR(L,a,n);
	DispList(L);
	delsame(L);
	DispList(L);
	DestoryList(L);
}

6:2.14 单链表-负数零正数顺序链表
查看提交统计提问
总时间限制: 10000ms 内存限制: 10000kB
描述
有一个带头节点的整数单链表L,设计一个算法,将所有的负数节点移动到前面(如果存在这样的节点),中间是为0的节点(如果存在这样的节点),最后是为正数的节点(如果存在这样的节点)。

输入
13
6 4 -6 -4 0 1 0 -2 2 0 3 -3 -5
输出
-6 -4 -2 -3 -5 0 0 0 6 4 1 2 3
样例输入
13
6 4 -6 -4 0 1 0 -2 2 0 3 -3 -5
样例输出
-6 -4 -2 -3 -5 0 0 0 6 4 1 2 3

#include <malloc.h>
#include <stdio.h>
#include<iostream>
using namespace std;
const int MAXSIZE=100;
typedef int ElemType;
typedef struct node
{	ElemType data;					//数据域
	struct node *next;				//指针域
} SLinkNode;						//单链表结点类型
void InitList(SLinkNode *&L)					//初始化单链表L
{	L=(SLinkNode *)malloc(sizeof(SLinkNode));	//创建头结点L
	L->next=NULL;
}
void DestroyList(SLinkNode *&L)		//销毁单链表L
{	SLinkNode *pre=L,*p=pre->next;
	while (p!=NULL)
	{	free(pre);
		pre=p; p=p->next;			//pre、p同步后移
	}
	free(pre);
}
int GetLength(SLinkNode *L)			//求长度
{	int i=0;
	SLinkNode *p=L->next;
	while (p!=NULL)
	{	i++;
		p=p->next;
	}
	return i;
}
int GetElem(SLinkNode *L,int i,ElemType &e)	//求第i个结点值
{	int j=0;
	SLinkNode *p=L;					//p指向头结点,计数器j置为0
	if (i<=0) return 0;				//参数i错误返回0
	while (p!=NULL && j<i)
	{	j++;
		p=p->next;
	}
	if (p==NULL)
		return 0;					//未找到返回0
	else
	{	e=p->data;
		return 1;					//找到后返回1
	}
}
int Locate(SLinkNode *L,ElemType e)	//求第一个值为e的结点位置
{	SLinkNode *p=L->next;
	int j=1;						//p指向第一个数据结点,j置为其序号1
	while (p!=NULL && p->data!=e)
	{	p=p->next;
		j++;
	}
	if (p==NULL) return(0);			//未找到返回0
	else return(j);					//找到后返回其序号
}
int InsElem(SLinkNode *&L,ElemType x,int i)	//插入结点值为x的结点
{	int j=0;
	SLinkNode *p=L,*s;
	if (i<=0) return 0;				//参数i错误返回0
	while (p!=NULL && j<i-1)		//查找第i-1个结点p
	{	j++;
		p=p->next;
	}
	if (p==NULL)
		return 0;					//未找到第i-1个结点时返回0
	else							//找到第i-1个结点p
	{	s=(SLinkNode *)malloc(sizeof(SLinkNode));
		s->data=x;					//创建存放元素x的新结点s
		s->next=p->next;			//将s结点插入到p结点之后
		p->next=s;
		return 1;					//插入运算成功,返回1
	}
}
int DelElem(SLinkNode *&L,int i)	//删除结点
{	int j=0;
	SLinkNode *p=L,*q;
	if (i<=0) return 0;				//参数i错误返回0
	while (p!=NULL && j<i-1)		//查找第i-1个结点
	{	j++;
		p=p->next;
	}
	if (p==NULL)
		return 0;					//未找到第i-1个结点时返回0
	else							//找到第i-1个结点p
	{	q=p->next;					//q指向被删结点
		if (q==NULL)
			return 0;				//没有第i个结点时返回0
		else
		{	p->next=q->next;		//从单链表中删除q结点
			free(q);				//释放其空间
			return 1;
		}
	}
}
void DispList(SLinkNode *L)			//输出单链表
{	SLinkNode *p=L->next;
	while (p!=NULL)
	{	printf("%d ",p->data);
		p=p->next;
	}
	printf("\n");
}

void CreateListF(SLinkNode *&L,ElemType a[],int n)	//头插法建表
{	SLinkNode *s;  int i;
	L=(SLinkNode *)malloc(sizeof(SLinkNode));		//创建头结点
	L->next=NULL;									//头结点的next域置空
	for (i=0;i<n;i++)								//遍历a数组所有元素
	{	s=(SLinkNode *)malloc(sizeof(SLinkNode));
		s->data=a[i];								//创建存放a[i]元素的新结点s
		s->next=L->next;							//将s插在头结点之后
		L->next=s;
	}
}

void CreateListR(SLinkNode *&L,ElemType a[],int n)	//尾插法建表
{	SLinkNode *s,*tc;  int i;
	L=(SLinkNode *)malloc(sizeof(SLinkNode));		//创建头结点
	tc=L;											//tc为L的尾结点指针
	for (i=0;i<n;i++)
	{	s=(SLinkNode *)malloc(sizeof(SLinkNode));
		s->data=a[i];								//创建存放a[i]元素的新结点s
		tc->next=s;									//将s结点插入tc结点之后
		tc=s;
	}
	tc->next=NULL;									//尾结点next域置为NULL
}

//补充代码
void Move(SLinkNode *&L)
{
	SLinkNode *p=L->next,*q;
	q=p;
	int i=GetLength(L),j=1;
	int k=i;
	while(i!=0&&p!=NULL)
	{
//		cout<<p->data<<endl;
//		cout<<"q"<<q->data<<endl;
//		DispList(L);
		if(p->data<0)
		{
			InsElem(L,p->data,j);
			j++;
			p=p->next;
			DelElem(L,GetLength(L)-i+1);
		}
		else if(p->data==0){
			InsElem(L,0,j);
			p=p->next;
			DelElem(L,GetLength(L)-i+1);
			
		}
		else
		{
			p=p->next;
		}
		i--;
	}
}
int main()
{
	SLinkNode *L;
	int n;
	ElemType a[MAXSIZE];
	cin>>n;
	for(int i=0;i<n;i++)
	   cin>>a[i];
	CreateListR(L,a,n);				//尾插法建表
	Move(L);
    DispList(L);
	DestroyList(L);
	return 0; 
}

7:2.12 单链表-删除某一区间内的节点
查看提交统计提问
总时间限制: 10000ms 内存限制: 10000kB
描述
有一个整数序列采用带头节点的单链表L 。设计一个算法,删除单链表L中data值大于等于min且小于等于max的节点(若表中有这样的节点),同时释放被删节点的空间,这里min和max是两个的参数。

输入
9
5 1 3 6 8 2 4 8 1
3 7
输出
1 8 2 8 1
样例输入
9
5 1 3 6 8 2 4 8 1
3 7
样例输出
1 8 2 8 1

#include <malloc.h>
#include <stdio.h>
#include<iostream>
using namespace std;
const int MAXSIZE=100;
typedef int ElemType;
typedef struct node
{	ElemType data;					//数据域
	struct node *next;				//指针域
} SLinkNode;						//单链表结点类型
void InitList(SLinkNode *&L)					//初始化单链表L
{	L=(SLinkNode *)malloc(sizeof(SLinkNode));	//创建头结点L
	L->next=NULL;
}
void DestroyList(SLinkNode *&L)		//销毁单链表L
{	SLinkNode *pre=L,*p=pre->next;
	while (p!=NULL)
	{	free(pre);
		pre=p; p=p->next;			//pre、p同步后移
	}
	free(pre);
}
int GetLength(SLinkNode *L)			//求长度
{	int i=0;
	SLinkNode *p=L->next;
	while (p!=NULL)
	{	i++;
		p=p->next;
	}
	return i;
}
int GetElem(SLinkNode *L,int i,ElemType &e)	//求第i个结点值
{	int j=0;
	SLinkNode *p=L;					//p指向头结点,计数器j置为0
	if (i<=0) return 0;				//参数i错误返回0
	while (p!=NULL && j<i)
	{	j++;
		p=p->next;
	}
	if (p==NULL)
		return 0;					//未找到返回0
	else
	{	e=p->data;
		return 1;					//找到后返回1
	}
}
int Locate(SLinkNode *L,ElemType e)	//求第一个值为e的结点位置
{	SLinkNode *p=L->next;
	int j=1;						//p指向第一个数据结点,j置为其序号1
	while (p!=NULL && p->data!=e)
	{	p=p->next;
		j++;
	}
	if (p==NULL) return(0);			//未找到返回0
	else return(j);					//找到后返回其序号
}
int InsElem(SLinkNode *&L,ElemType x,int i)	//插入结点值为x的结点
{	int j=0;
	SLinkNode *p=L,*s;
	if (i<=0) return 0;				//参数i错误返回0
	while (p!=NULL && j<i-1)		//查找第i-1个结点p
	{	j++;
		p=p->next;
	}
	if (p==NULL)
		return 0;					//未找到第i-1个结点时返回0
	else							//找到第i-1个结点p
	{	s=(SLinkNode *)malloc(sizeof(SLinkNode));
		s->data=x;					//创建存放元素x的新结点s
		s->next=p->next;			//将s结点插入到p结点之后
		p->next=s;
		return 1;					//插入运算成功,返回1
	}
}
int DelElem(SLinkNode *&L,int i)	//删除结点
{	int j=0;
	SLinkNode *p=L,*q;
	if (i<=0) return 0;				//参数i错误返回0
	while (p!=NULL && j<i-1)		//查找第i-1个结点
	{	j++;
		p=p->next;
	}
	if (p==NULL)
		return 0;					//未找到第i-1个结点时返回0
	else							//找到第i-1个结点p
	{	q=p->next;					//q指向被删结点
		if (q==NULL)
			return 0;				//没有第i个结点时返回0
		else
		{	p->next=q->next;		//从单链表中删除q结点
			free(q);				//释放其空间
			return 1;
		}
	}
}
void DispList(SLinkNode *L)			//输出单链表
{	SLinkNode *p=L->next;
	while (p!=NULL)
	{	printf("%d ",p->data);
		p=p->next;
	}
	printf("\n");
}

void CreateListF(SLinkNode *&L,ElemType a[],int n)	//头插法建表
{	SLinkNode *s;  int i;
	L=(SLinkNode *)malloc(sizeof(SLinkNode));		//创建头结点
	L->next=NULL;									//头结点的next域置空
	for (i=0;i<n;i++)								//遍历a数组所有元素
	{	s=(SLinkNode *)malloc(sizeof(SLinkNode));
		s->data=a[i];								//创建存放a[i]元素的新结点s
		s->next=L->next;							//将s插在头结点之后
		L->next=s;
	}
}

void CreateListR(SLinkNode *&L,ElemType a[],int n)	//尾插法建表
{	SLinkNode *s,*tc;  int i;
	L=(SLinkNode *)malloc(sizeof(SLinkNode));		//创建头结点
	tc=L;											//tc为L的尾结点指针
	for (i=0;i<n;i++)
	{	s=(SLinkNode *)malloc(sizeof(SLinkNode));
		s->data=a[i];								//创建存放a[i]元素的新结点s
		tc->next=s;									//将s结点插入tc结点之后
		tc=s;
	}
	tc->next=NULL;									//尾结点next域置为NULL
}

//补充代码
// 在此处补充你的代码
void del(SLinkNode *&L,int min,int max)
{
	SLinkNode *p=L->next,*q,*s;
	int i=0;
	for(p;p!=NULL;p=p->next)
	{
		s=p;
		for(q=p->next;q!=NULL; )
		{
			if(q->data>=min&&q->data<=max)
			{
//				cout<<"p"<<p->data<<endl;
				s->next=q->next;
				free(q);
				q=s->next;

			} 
			else
			{
				s=q;
				q=q->next;
			}
		}	
	}
	SLinkNode *f=L->next;
	if(f->data>=min&&f->data<=max)
	{
		DelElem(L,1);
	}
}
int main()
{
	SLinkNode *L;
	int n;
	int max,min;
	ElemType a[MAXSIZE];
	cin>>n;
	for(int i=0;i<n;i++)
	   cin>>a[i];
	cin>>min>>max;
	CreateListR(L,a,n);				//尾插法建表
	del(L,min,max);
    DispList(L);
	DestroyList(L);
	return 0; 
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值