数据结构》严奶奶版本---线性表(3)双循环链表 完整源码

数据结构—线性表(3)双循环链表 完整源码

/*
1)在双向循环链表上实现插入、删除操作;
*/
#include 
#include 

using namespace std;

typedef int ElemType;

typedef struct Dlinklist{
	
	ElemType data;
	struct Dlinklist *prior;
	struct Dlinklist *next;

}DNode,*DLinkList;

bool create_list(DLinkList &dl);//创建链表
bool trave_dl(DLinkList &dl);//顺序遍历显示
bool traved_dl(DLinkList &dl);//倒序遍历显示
bool insert_data(DLinkList &dl,ElemType data,int addr);//插入数据
bool delete_data(DLinkList &dl,ElemType data);//根据元素值删除特定元素
bool delete_addr(DLinkList &dl,int addr);//根据元素位置删除特定元素
bool get_addr(DLinkList &dl,ElemType data);//得到特定元素的位置
bool get_data(DLinkList &dl,int addr);//得到特定位置的元素


void show();
void switch_channel(int channel);

DLinkList dl;

int main()
{
	int channel;
	int temp;
	ElemType data;
	/*do{
		show();
		cin>>channel;
		system("cls");
		switch_channel(channel);
		system("pause");
		
	}while(1);*/
	cout<<"双循环链表"<<endl;
	create_list(dl);
	trave_dl(dl);
	traved_dl(dl);
	cout<<"请输入查询元素的位置:";
	cin>>temp;
	get_data(dl,temp);
	cout<<"请输入插入的元素:";
	cin>>data;
	cout<<"请输入插入元素的位置:";
	cin>>temp;
	insert_data(dl,data,temp);
	trave_dl(dl);
	cout<<"请输入要删去的元素:";
	cin>>data;
	delete_data(dl,data);//根据元素值删除特定元素
	trave_dl(dl);
	cout<<"请输入要删去的元素的位置:";
	cin>>temp;
	delete_addr(dl,temp);//根据元素位置删除特定元素
	trave_dl(dl);
	cout<<"查询元素的位置:";
	cin>>temp;
	get_addr(dl,temp);//得到特定元素的位置
	cout<<"查询的元素:";
	cin>>data;
	get_data(dl,data);//得到特定位置的元素

	return 0;
}

void show()
{
	cout<<endl<<endl<<"\t循环双链表基础操作\n";
	cout<<"\t1--创建链表\n";
	cout<<"\t2--顺序遍历显示\n";
	cout<<"\t3--倒序遍历显示\n";
	cout<<"\t4--在特定的位置上插入数据\n";
	cout<<"\t5--根据元素值删除特定元素\n";
	cout<<"\t6--根据元素位置删除特定元素\n";
	cout<<"\t7--得到特定元素的位置\n";
	cout<<"\t8--得到特定位置的元素\n";

}

void switch_channel(int channel)
{
	switch(channel)
	{
	case 1:
		{
			create_list(dl);
			
		}break;
	case 2:
		{
			trave_dl(dl);

		}break;
	case 3:
		{
			traved_dl(dl);
		}break;
	case 4:
		{
			int addr;
			ElemType data;
			cout<<"请输入插入的元素:";
			cin>>data;
			cout<<"请输入插入元素的位置:";
			cin>>addr;
			insert_data(dl,data,addr);
			trave_dl(dl);
		}break;	
	case 5:
		{
			ElemType data;
			cout<<"请输入删除的元素值:";
			cin>>data;
			delete_data(dl,data);//根据元素值删除特定元素
			trave_dl(dl);	
		}break;
	case 6:
		{
			int addr;
			cout<<"请输入删除的元素位置:";
			cin>>addr;
			delete_addr(dl,addr);//根据元素位置删除特定元素
			trave_dl(dl);
		}break;
	case 7:
		{	
			ElemType data;
			cout<<"请输入查找的元素值:";
			cin>>data;
			get_addr(dl,data);//得到特定元素的位置

		}break;
	case 8:
		{	
			int addr;
			cout<<"请输入查找的元素位置:";
			cin>>addr;
			get_data(dl,addr);//得到特定位置的元素*/
		}break;
	
	}


}

//创建链表
bool create_list(DLinkList &dl)
{
	int count;
	cout<<"请输入要初始化的元素个数:";
	cin>>count;
	if(count <= 0)
	{
		cout<<"初始化个数不合法"<<endl;
		return false;
	}


	dl = new DNode;
	dl->next = dl;
	dl->prior = dl;
	cout<<"请输入第1个元素:";
	cin>>dl->data;
	
	DLinkList p = dl;
	int i;
	for(i = 2; i <= count; i++)
	{
		DLinkList q = new DNode;
		
		cout<<"请输入第"<<i<<"个元素:";
		cin>>q->data;	
		
		q->prior = p;
		p->next = q;
		p = q;
	}

	dl->prior = p;
	p->next = dl;

	return true;
}
//顺序遍历显示
bool trave_dl(DLinkList &dl)
{
	DLinkList p = dl;
	cout<<"顺序遍历显示:";
	do{
		cout<<p->data<<" ";
		p = p->next;
	
	}while(p != dl);
	cout<<endl;
	return true;
}
//倒序遍历显示
bool traved_dl(DLinkList &dl)
{
	DLinkList p = dl->prior;
	cout<<"倒序遍历显示:";

	do
	{
		cout<<p->data<<" ";
		p = p->prior;
	
	}while(p != dl->prior);

	cout<<endl;
	return true;
}
//插入数据
bool insert_data(DLinkList &dl,ElemType data,int addr)
{
	if(addr <= 0)
	{
		cout<<"插入位置不合法"<<endl;
		return false;
	}
	
	DLinkList p = dl;
	
	if(addr == 1)
	{
		DLinkList q = new DNode;

		q->data = data;
		q->prior = p->prior;
		p->prior->next = q;
		p->prior = q;
		q->next = p;

		return true;
	}

	int i = 1;

	do{
		p = p->next;
		i++;
		
	}while(i < addr && p != dl);
	
	
	if(p == dl)
	{
		cout<<"位置超出,放于链表最后"<<endl;
		DLinkList q = new DNode;
		q->data = data;
		q->prior = p->prior;
		p->prior->next = q;
		q->next = p;
		p->prior = q;
		return true;
	}

	DLinkList q = new DNode;
	q->data = data;
	q->prior = p->prior;
	p->prior->next = q;
	q->next = p;
	p->prior = q;

	return true;
}
//根据元素值删除特定元素
bool delete_data(DLinkList &dl,ElemType data)
{
	DLinkList p = dl;
	DLinkList q;

	if(p->data == data)
	{
		q = p;
		p = p->next;
		p->prior = q->prior;
		q->prior->next = p;
		dl = p; 
		delete q;

		return true;
	}

	do{

		p = p->next;
		
	}while(p != dl && p->data != data);

	if(p == dl)
	{
		cout<<"元素不存在"<<endl;
		return false;	
	}

	q = p;
	p = p->next;
	p->prior = q->prior;
	q->prior->next = p;

	delete q;

	return true;
}
//根据元素位置删除特定元素
bool delete_addr(DLinkList &dl,int addr)
{
	if(addr <= 0)
	{
		cout<<"位置不合法"<<endl;
		return false;
	}
	
	DLinkList p = dl;
	
	if(addr == 1)
	{
		p->prior->next=p->next;
  		p->next->prior=p->prior;
		dl = p->next;
		delete p;

		return true;
	}

	int i = 1;

	do{
		p = p->next;
		i++;
		
	}while(i < addr && p != dl);
	
	
	if(p == dl)
	{
		cout<<"位置不合法"<<endl;

		return true;
	}

	p->prior->next=p->next;
  	p->next->prior=p->prior;
	delete p;

	return true;
}
//得到特定元素的位置
bool get_addr(DLinkList &dl,ElemType data)
{
	DLinkList p = dl;

	if(p->data == data)
	{
		cout<<"此元素的位置为:1"<<endl;
		return true;
	}

	int i = 1;

	do{

		p = p->next;
		i++;

	}while(p != dl && p->data != data);

	if(p == dl)
	{
		cout<<"元素不存在"<<endl;
		return false;	
	}

	cout<<"此元素的位置为:"<<i<<endl;

	return true;
}
//得到特定位置的元素
bool get_data(DLinkList &dl,int addr)
{
	if(addr <= 0)
	{
		cout<<"位置不合法"<<endl;
		return false;
	}

	DLinkList p = dl;
	int i = 1;
	
	if(addr == 1)
	{
		cout<<"此位置上的元素为:"<<p->data<<endl;
		return true;
	}

	//for(i = 1; i < addr && p->next != dl; i++,p = p->next);

	do{
		p = p->next;
		i++;
	}while(i < addr && p != dl);

	if(p == dl)
	{
		cout<<"查找位置不合法或者元素不存在"<<endl;
		return true;
	}

	cout<<"此位置上的元素为:"<<p->data<<endl;

	return true;
}

你可能感兴趣的:(数据结构源码实现#严奶奶版本)