链表的创建与基本操作(C++ 实现)

链表即采用链式存储方式的线性表。为了表示每个数据元素 a_i 及其后继元素 a_{i+1} 间的逻辑关系,我们在存储数据元素 a_i 的同时,必须存储一个指示其后继 a_{i+1}的存储位置的信息 b_i 。我们称这样的二元组 (a_i, b_i)为一个结点 ( Node ) 。不多说,直接贴代码吧。

#include <iostream>
#include <string>
using namespace std;
struct Node
{
	int data;
	Node *next;
};

class List
{
	
public:
	 Node *head;
	 List()//构造函数初始化一个空链表
	  {
		  head=NULL;
	  }

	 void Create()//创建一个链表
	 {
		Node *p=new Node;
		p->data = 0;
		p->next = NULL;
		head=p;
		int n;
Start:
		cout<<"请输入链表的长度: "<<endl;
		cin>>n;

		if(n<0)
		{
			cout<<"您的输入有误,请从新输入!"<<endl;
			goto Start;
		}
		else
		{
			cout<<"请依次输入"<<n<<"个值:"<<endl;
		}

		cout<<"请输入第 1 个值:"<<endl;//这里以链表第一个值作为头结点
		cin>>p->data;

		for(int i = 1; i < n; i++)
		{
			Node *q=new Node;
			q->next = NULL;
			cout<<"请输入第 "<<i+1<<" 个值:"<<endl;
			cin>>q->data;
			//q->next=p->next;
			p->next=q;
			p=q;
			
		}
	 }
	 
	 void Insert(int i,int m)//在表的第i个位置插入一个数m
	 {
		 Node *p=head;
		 int j=1;
		 while(p && j<i-1)
		 {
			 p=p->next;
			 j++;
		 }
		 if(!p || j>i-1)
		 {
			  cout<<"插入位置有误"<<endl;
		 }
		 else
		 {
			 Node *q;
			 q=new Node;
			 q->data=m;
			 q->next=p->next;
			 p->next=q;

		 }
	 }

	 void Delete(int i)//删除表的第i个元素,返回其值m
	 {
		 int m;
		 Node *p=head;
		 int j=1;
		 while(p->next && j<i-1)
		 {
			 p=p->next;
			 j++;
		 }
		 if(!(p->next)||j>i-1)
			 cout<<"删除位置错误"<<endl;
		 else
		 {
			 Node *q=new Node;
			 q=p->next;
			 p->next=q->next;
			 m=q->data;
			 delete q;
		 }
		 cout<<"被删除的 "<<i<<" 位置处的值为"<<m<<endl;

	 }
	 void Output()//输出链表的数据域和指针域
	 {
		 if(head==NULL)
		 {
			 cout<<"该表为空表"<<endl;
		 }
		
		Node *current=head;
		while(current!=NULL)
		{
			cout<<"在内存地址 "<<current<<" 上存储的数据是 "<<current->data<<", 存储的指针是 "<<current->next<<"."<<endl;
			current=current->next;
			
		}
	 }
	 int Length()//链表长度
	 {
		 Node *p=head;
		 int j=0;
		 while(p!=NULL)
		 {
			 p=p->next;
			 j++;
		 }
		 cout<<"链表长度为 "<<j<<endl;
		 return j;

	 }

	 void Reverse()//倒序排列
	 {
           if(head==NULL)
		   {
                return;       //链表为空链表
           }
            Node *current = head -> next,*node;     //从头结点开始遍历
            head -> next = NULL;
            while(current != NULL)
			{
                node = current -> next;    //记住当前操作节点的后面节点的地址
                current -> next = head;
                head = current;
                current = node;
            }
	 }


	 void Sort()//利用冒泡排序从大到小排列
	 {
		 Node *s=head;
		 while(s->next!=NULL)
		 {

		 Node *p=s; 
		 Node *q=p->next;
		 while(q!=NULL)
		 {
			 if(p->data<=q->data)
			 {
				 p=q;
				 q=q->next;
			 }
			 else
			 {
				 Node *r=new Node;
				 int m=p->data;
				 r=p;
				 r->data=q->data;
				 p=q;
				 p->data=m;
				 q=p->next;

			 }
		 }
		 int l=s->data;
		 s->data=p->data;
		 p->data=l;
		 s=s->next;
		 }


	 }

	 void Delete_Repeat()//删除链表中重复出现的元素,只保留第一个出现的元素
	 {


	 }

	 void Get()//获取链表指定位置的元素值
	 {
		 cout<<"请输入需要查找的位置:"<<endl;
		 int k;
		 cin>>k;
		 Node *p=head;
		 int l=1;
		 while(p!=NULL && l<k)
		 {
			 p=p->next;
			 l++;
		 }
		 if(p==NULL || l<k)
		 {
			 cout<<"所查找位置不存在"<<endl;
		 }
		 else
		 {
			 int e=p->data;
			 cout<<"第 "<<k<<" 个位置处的值为"<<e<<endl;
		 }
	 }

	 int Search()//在链表中查找某个数是否出现,并打印出现的位置和总次数
	 {
		 int k;
		 cout<<"请输入要查找的值:"<<endl;
		 cin>>k;
		 Node *p=head;
		 int j=0,i=0;
		 while(p!=NULL)
		 {
			 i++;
		 if(p->data==k)
		 {
			j++;
			p=p->next;
			cout<<"值 "<<k<<" 出现在链表的第 "<<i<<" 个位置"<<endl;
		 }
		 else
		 {
			p=p->next;
		 }
		 }
		 cout<<"值 "<<k<<" 在链表中出现的总次数为 "<<j<<endl;
		 return j;
	 }
};


void Merge(List A,List B,List C)//合并两个链表为第三个链表并输出
{
	Node *a=A.head;
	Node *c=C.head=A.head;
	Node *b=B.head;
	while(c->next!=NULL)
	{
		c=c->next;
	}
	c->next=b;
	C.Output();
}

int main()
{
	cout<<"*****链表初始化*****"<<endl;
	List list,A,B;
	list.Length();
	list.Output();

	cout<<"*****创建新表及基本操作*****"<<endl;
	list.Create();
	list.Length();
	list.Output();
	list.Get();
	list.Search();
	
	cout<<"*****从大到小排列*****"<<endl;
	list.Sort();
	list.Output();

	cout<<"*****从小到大排列*****"<<endl;
	list.Reverse();
	list.Output();

	cout<<"*****在指定位置插入指定值*****"<<endl;
	list.Insert(3,78);
	list.Length();
	list.Output();

	cout<<"*****删除指定位置处的值并输出*****"<<endl;
	list.Delete(2);
	list.Length();
	list.Output();

	cout<<"*****链表的合并*****"<<endl;
	A.Create();
	A.Output();
	cout<<endl;
	Merge(list,A,B);

	return 0;
}

猜你喜欢

转载自blog.csdn.net/vegetable__chicken/article/details/81165710
今日推荐