【数据结构】链式存储结构的基本操作

版权声明:无意呢。 https://blog.csdn.net/qq_41900081/article/details/86558068

一、实验目的

掌握单链表,链式堆栈,链式队列的定义及基本操作

二、使用仪器、器材

笔记本一台
操作系统:Win7
编程软件:DevC++

三、实验内容及原理

(一)单链表的定义及基本操作

  1. 用带表头的链表存放输入的数据,每读入一个数,按升序顺序插入到链表中,链 表中允许两个结点有相同值。链表的头结点存放链表后面的结点个数,初始化时 就生成头结点(初值为 0)。
  2. 在上述带表头的链表中删除第 i 个结点或删除数值为 item 的结点。
  3. 链表翻转是把数据逆序(变成降序),注意,头结点不动。翻转后要再翻转一次, 恢复升序后才能插入新元素,否则会出错。
  4. 设 A 与 B 分别为两个带有头结点的有序循环链表(所谓有序是指链接点按数据域 值大小链接,本题不妨设按数据域值从小到大排列),list1 和 list2 分别为指向两个链表的指针。请写出并在计算机上实现将这两个链表合并为一个带头结点 的有序循环链表的算法。

C++版本实现

#include<iostream>
using namespace std;
 
typedef struct LNode
{
	int  data;
	struct LNode *next;
}LNode, *Linklist;
int sort_L(Linklist &L,int n)
{   Linklist r;	
	r=L;
	Linklist p = new LNode;
	for(int i=1;i<n;i++)
	    {
	    	r=L->next;		
		for(int j=1;j<=n-i;j++)
		{   
			if(r->data>r->next->data)
				{   
					p->data=r->data;
					r->data=r->next->data;
					r->next->data=p->data;	
				}
				r=r->next;
		  } 
        }
}
Linklist Create_L(Linklist &L, int n)//创建单链表
{   Linklist r;	
	r=L;	
	for (int i = n; i > 0; --i)
	{   Linklist p = new LNode;
		cin >> p->data;
		p->next =NULL;
	    r->next = p;
		r=p;				
	}
	sort_L(L,n);
	return L;
}
Linklist Create_XHL(Linklist &L, int m)//创建单循环链表
{   Linklist r;	
    L->next = L;
	r=L;	
	for (int i = m; i > 0; --i)
	{   Linklist p = new LNode;
		cin >> p->data;
		p->next =L;
	    r->next = p;
		r=p;				
	}
	r->next=L;
	sort_L(L,m);
	return L; 
}

Linklist connet_XHL (Linklist LA,Linklist LB)
{
	Linklist p1,p2,s1,s2;
	p1=LA->next;
	p2=LB->next;
	while(p1!=LA)
	{
		if(p1->next==LA)
		{
			s1=p1;
			break;
		 } 
		 p1=p1->next;
	}
	while(p2!=LB)
	{
		if(p2->next==LB)
		{
			s2=p2;
			break;
		 } 
		 p2=p2->next;
	}
	s1->next=LB->next;
	s2->next=LA; 
}
Linklist Init_L(Linklist &L,int n)//初始化 单链表
{
	L = new LNode;
	L->next = NULL;
	L->data = n;
	return L;
	
}
Linklist Init_XHL(Linklist &L,int n)//初始化 单循环链表
{
	L = new LNode;
	L->next = L;
	L->data = n;
	return L;
	
}

Linklist insert_L(Linklist &L,int i,int e,int n)//插入单链表
{   if(i>n || i<1) 
	{
		cout<<"插入的位置不存在!!!"	; 
		return L; 
	}
	Linklist q = L;
	int j = 0;
	while (q&&j < i - 1)
	{
		q = q->next;
		++j;
	}
	if (!q || j > i - 1) exit(0);
	Linklist s = new LNode;
	s->data=e;
	s->next = q->next;
	q->next = s;
	//sort_L(L,n);
	return L;
}
int dele_L(Linklist &L, int i,int n)//删除节点
{   if(i>n || i<1) {
	cout<<"删除的位置不存在!!!"; 
	return 0;
	} 
	Linklist p = L;
	int e,j=0;
	while(p->next&&j<i-1)
	{
		p = p->next; ++j;
	}
	if (!(p->next || j > i - 1)) exit(0);
	Linklist q= new LNode;
	q = p->next;
	p->next = q->next;
	e = q->data;
	delete q;
	--n;
	
	return n;

}
int cout_L(Linklist &L)
{   Linklist q;
	q=L->next;
	while(q!=NULL)
	{
		cout<<q->data<<"     ";
		q=q->next;
	} 	
	cout<<endl;	  	
}
int cout_XHL(Linklist &L)
{   Linklist q;
	q=L->next;
	while(q!=L)
	{
		cout<<q->data<<"     ";
		q=q->next;
	} 	cout<<endl;	  	
}
Linklist turn_L(Linklist &L)
{
	Linklist p,q,pr;
	p=L->next;
	q=NULL;
	L->next=NULL;
	while(p)
	{
		pr=p->next;
		p->next=q;
	    q=p;
	    p=pr;
	}
	L->next=q;
	return L;
 } 
int main()
{
	Linklist L,LA,LB;
	int n,i,e,m;
    cout<<"输入链表长度n:";
    cin>>n;
	cout<<"初始化单链表:"<<endl;Init_L(L,n);//初始化单链表 
	Create_L(L,n);cout<<"创建的有序单链表:" ;
	cout_L(L); 
	cout<<endl<<"输入需要删除的位置:";
	cin>>i;
	
	dele_L(L,i,n);//删除第i个节点 
	cout<<endl<<"删除节点后的单链表:"; 
	cout_L(L);
	turn_L(L); //第一次反转 
	cout<<endl<<"反转后的单链表:";
	cout_L(L);
	turn_L(L); //反转回来 
	cout<< endl<<"输入需要插入的位置:";
	cin>>i; 
	cout<< endl<<"输入需要插入的值:";
	cin>>e; 
	insert_L(L,i,e,n);
	cout<<endl<<"插入后的单链表:";
	cout_L(L);
	cout<<endl<< "输入循环链表的长度:";cin>>m; 
	Init_XHL(LA,m);//初始化循环单链表LA 
	Init_XHL(LB,m);//初始化循环单链表LB 
	
	cout<<endl<<"初始化单循环链表A:"<<endl;Create_XHL(LA,m);
	cout<<endl<<"创建有序单循环链表A:";
	cout_XHL(LA);
	cout<<endl<<"初始化单循环链表B:"<<endl;Create_XHL(LB,m);
	cout<<endl<<"创建有序单循环链表B:";
	cout_XHL(LB);
	connet_XHL(LA,LB);
	sort_L(LA,2*m);
	cout<<endl<<"输出合并后的有序单循环链表:";
	cout_XHL(LA);
    delete L,LA,LB;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41900081/article/details/86558068
今日推荐