0
点赞
收藏
分享

微信扫一扫

不带头节点的单链表

冶炼厂小练 2022-03-19 阅读 49
c++

(1)编写函数删除不带头结点单链表head中的第一个值为x 的结点,并构造测试用例进行测试。

(2)假设线性表(a1,a2,a3,…an)采用不带头结点的单链表存储,请设计算法将不带头结点的单链表head就地倒置,使表变成(an,an-1,…a3.a2,a1)。并构造测试用例进行测试。

(3)假设不带头结点的单链表head是升序排列的,设计算法将值为x的结点插入到链表head中,并保持链表有序性。分别构造插入到表头、表中和表尾三种情况的测试用例进行测试。

(4)编写算法函数删除不带头结点单链表head中所有值为x的结点。

代码如下:

#include<iostream>
using namespace std;
struct tree
{
	int val;
	struct tree* next;
};
typedef struct tree tRee;
typedef tRee* Tree;
void createTree(Tree& head,Tree& tail,int n)
{
	Tree p;
	p=(Tree)malloc(sizeof(tRee));
	p->val=n;
	p->next=NULL;
	if(head==NULL)
	{
		head=tail=p;
	}
	else
	{
		tail->next=p;
		tail=p;
	}
	tail->next=NULL;
	return ;
}
void Delete(Tree& head,int n)
{
	Tree pre,p;
	pre=(Tree)malloc(sizeof(tRee));
	p=(Tree)malloc(sizeof(tRee));
	pre=head;
	p=head;
	while(p!=NULL)
	{
		if(p->val==n)
		{
			if(p==head)
			{
				head=head->next;
				free(p);
				break;
			}
			else
			{
				pre->next=p->next;
				free(p);
				break;
			}
		}
		if(pre==p)
		{
			p=p->next;
		}
		else
		{
			p=p->next;
			pre=pre->next;
		}
	}
	return ;
}
void print(Tree& head)
{
	Tree p=head;
	while(p!=NULL)
	{
		printf("%d ",p->val);
		p=p->next;
	}
	cout<<endl;
	return ;
}
void Reverse(Tree& head,Tree& tail)
{
	if(head==tail)return ;
	else if(head->next==tail)
	{
		tail->next=head;
		head=tail;
		tail=head->next;
	}
	else
	{
		Tree p,end;
		p=(Tree)malloc(sizeof(tRee));
		end=(Tree)malloc(sizeof(tRee));
		end=head->next;
		p->next=head->next->next;
		head->next->next=head;
		head->next=p;
		while(p->next!=NULL)
		{
			Tree l=(Tree)malloc(sizeof(tRee));
			l=p->next->next;
			p->next->next=end;
			end=p->next;
			p->next=l;
		}
		head->next=NULL;
		tail=head;
		head=end;
	}
	return ;
}
void disease(Tree& head,Tree& tail,int n)
{
	Tree p=(Tree)malloc(sizeof(tRee)),l=(Tree)malloc(sizeof(tRee));
	p->val=n;
	l=head;
	if(head->val>=n)
	{
		p->next=head;
		head=p;
	}
	else
	{
		while(l!=NULL)
		{
			if(l->val<=n&&l->next==NULL)
			{
				l->next=p;
				p->next=NULL;
				tail=p;
				break;
			}
			else if(l->val<n&&l->next->val>=n)
			{
				p->next=l->next;
				l->next=p;
				break;
			}
			else
			{
				l=l->next;
			}
		}
	}
	return ;
}
void allDelete(Tree& head,int n)
{
	if(head->val==n)
	{
		while(head->val==n)
		{
			head=head->next;
		}
	}
	else
	{
		Tree pre,p;
		p=(Tree)malloc(sizeof(tRee));
		pre=(Tree)malloc(sizeof(tRee));
		pre=head;
		p=head->next;
		while(p->val!=n)
		{
			pre=pre->next;
			p=p->next;
		}
		while(p->val==n)
		{
			p=p->next;
		}
		pre->next=p;
	}
	return ;
}
int main()
{
	Tree head,tail;
	head=(Tree)malloc(sizeof(tRee));
	tail=(Tree)malloc(sizeof(tRee));
	tail=NULL;
	head=NULL;
	for(int n=1;n<20;n+=2)
	{
		createTree(head,tail,n);
		if(n==5)createTree(head,tail,n);
		if(n==9){
			createTree(head,tail,n);
			createTree(head,tail,n);
		}
	}
	print(head);
	printf("删除第一个5后:\n");
	Delete(head,5);
	print(head);
	printf("倒置后的单链表:\n");
	Reverse(head,tail);
	print(head);
	printf("倒置回原来的样子:\n");
	Reverse(head,tail);
	print(head);
	printf("插入1,7,19(分别为前中后)\n");
	disease(head,tail,1);
	disease(head,tail,7);
	disease(head,tail,19);
	print(head);
	printf("删除所有为9的位置:\n");
	allDelete(head,9);
	print(head);
	return 0;
}
举报

相关推荐

0 条评论