线性表的链式表达(初始化,头尾插法创建,展示,取值查找,删除,排序,有序插入)

#include<iostream>
#include<stdlib.h>
using namespace std;
typedef int status;
typedef int ElemType;
#define OK 1
#define ERROR 0
typedef struct LNode
{
	ElemType data;
	struct LNode* next;
}LNode,*LinkList; 
status InitList(LinkList &L);//初始化 
status CreateListB(LinkList &L,int n);//尾插法 
status CreateListH(LinkList &L,int n);//头插法 
status show(LinkList &L);//展示 
status ListInsert(LinkList &L,int i,ElemType e);//插入 
status quzhi(LinkList &L,int i,ElemType &e);//取值 
LNode* chazhao(LinkList L,ElemType e);//查找 
status shanchu(LinkList &L,int i);//删除 
int Listlen(LinkList L); //长度
status sort(LinkList &L);//排序 
void youxu(LinkList &L,ElemType e);//有序插入 
int main()
{
	LinkList L;
	int choice;
	ElemType e;
	while(1)
	{
	 	cout<<"1.初始化   11.尾插法创建   12.头插法创建  2.插入"<<endl;
		cout<<"3.展示      4.取值          5.查找        6.删除"<<endl;
		cout<<"7.排序      8.有序插入      9.退出"<<endl;  
		cout<<"请输入选项:";
		cin>>choice;
		switch(choice)
		{
			case 1://初始化 
			{
				if(InitList(L)==OK)
				cout<<"初始化成功"<<endl;
				else cout<<"初始化失败"<<endl;
				break;
			}
			case 11://尾插法创建 
				{
				cout<<"Input n:";
				int n; 
				cin>>n;
				if(CreateListB(L,n)==OK)
				cout<<"创建成功"<<endl;
				else
				cout<<"创建失败"<<endl; 
				show(L);
				break;}
			case 12://头插法创建 
				{
				int n;
				cout<<"Input n:";
				cin>>n;
				if(CreateListH(L,n)==OK)
				cout<<"创建成功"<<endl;
				else cout<<"创建失败"<<endl;
				show(L); 
				break;}
			case 2://插入 
				{
				int i;
				cout<<"请输入要插入的位置:";
				cin>>i;
				cout<<"请输入数据";
				cin>>e;
				if(ListInsert(L,i,e)==OK)
				cout<<"插入成功"<<endl;
				else cout<<"插入位置不合法" <<endl;
				show(L); 
				break;}
			case 3://展示 
				show(L);
				break;	
			case 4://取值 
				cout<<"取值位置:";
				int i;
				cin>>i;
				if(quzhi(L,i,e)==OK)
				cout<<e;
				else cout<<"位置有误"<<endl;
				break;
			case 5://查找 
				{
				cout<<"要查找的数据:";
				cin>>e;
				LNode *p;
				p=chazhao(L,e);
				if(p!=NULL)
				{
					cout<<"数据存在"<<p->data<<endl; 
				}
				else cout<<"数据不存在"<<endl;
				}
				break;
			case 6://删除 
				{
					int i;
					cout<<"请输入删除的位置:";
					cin>>i;
					int a;
					a=shanchu(L,i);
					if(a==OK)
					cout<<"删除成功"<<endl;
					else cout<<"删除失败"<<endl; 
					show(L);
					break; 
				}
			case 7://排序 
				{
					if(sort(L)==OK) 
					show(L);
					else cout<<"排序失败"<<endl;
					break; 
				}
			case 8://有序插入 
				{
					cout<<"输入插入数据:";
					cin>>e;
					youxu(L,e);
					show(L);
					break;
				}
			case 9://退出 
				return 0;
				break;
			default :
			cout<<"选项错误"<<endl;		
		}
		system("pause");
		system("cls");
	}
	return 0;
}
status InitList(LinkList &L)
{
	L=new LNode;//申请节点 
	if(!L) return ERROR;//判断是否创建成功 
	L->next=NULL;//指针域为空 
	return OK;
}
status CreateListB(LinkList &L,int n)
{
	int i;
	LNode* p,*r;
	L=new LNode;
	r=L;
	L->next=NULL;
	for(i=0;i<n;i++)
	{
		p=new LNode;
		cout<<"输入第"<<i+1<<"个数据:";
		cin>>p->data;
		r->next=p;
		p->next=NULL;
		r=p;
	}
	return OK;
}
status CreateListH(LinkList &L,int n)
{
	LNode *p;
	int i;
	L=new LNode;
	L->next=NULL;
	for(i=0;i<n;i++)
	{
		p=new LNode;
		cout<<"输入第"<<i<<"个数据:";
		cin>>p->data;
		p->next=L->next;
		L=p;
	}
	return OK; 
}
status show(LinkList &L)
{
	LNode *p;
	p=L->next;
	while(p)
	{
		cout<<p->data<<" ";
		p=p->next;
	}
	cout<<endl;
}
status ListInsert(LinkList &L,int i,ElemType e)
{
	LNode *p=L;
	int j=0; 
	while(p&&j<i-1)
	{
		p=p->next;
		j++;
	}
	if(!p||j>i-1) return ERROR;
	LNode *s=new LNode;
	s->data=e;
	s->next=p->next;
	p->next=s;
	return OK;
}
status quzhi(LinkList &L,int i,ElemType &e)
{
	LNode* p=L->next;
	int j=0;
	while(p&&j<i-1)	
	{
		p=p->next;
		j++;
	}
	if(!p||j>i) return ERROR;
	e=p->data;
	return OK;
}
LNode* chazhao(LinkList L,ElemType e)
{
	LNode *p=L->next;
	while(p&&p->data!=e)
	{
		p=p->next;
	}
	return p;
}
status shanchu(LinkList &L,int i)
{
	int j=0;	
	LNode* p=L,*q;
	while(p->next&&j<i-1)
	{
		p=p->next;
		j++;
	}
	if((!p->next)||j>i)
	return ERROR;
	q=p->next;
	p->next=q->next;
	delete q;
	return OK;
}
int Listlen(LinkList L)
{
	int len=0;
	LNode *s=L->next;
	while(s)
	{
		s=s->next;
		len++;
	}
	return len;
}
status sort(LinkList &L)
{
	int len=Listlen(L);
	if(len==0)  return ERROR;
	LNode *p,*q;
	ElemType t;
	for(int i=0;i<len-1;i++)
	{
		p=L->next;
		q=p->next;
		for(int j=0;j<len-i-1;j++)
		{
			if(p->data>q->data)
		{
			t=p->data;
			p->data=q->data;
			q->data=t;
		}
		p=p->next;
		q=q->next;
		}
	}
	return OK;  
}
void youxu(LinkList &L,ElemType e)
{
	LNode *p,*q,*s;
	p=L;
	q=p->next;
	while(q!=NULL&&q->data<e)
	{
		p=q;
		q=q->next;
	 } 
	 s=new LNode;
	 s->data=e;
	 p->next=s;
	 s->next=q;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值