大一数据结构顺序表、链表、栈、队列实验代码举例应用

顺序表

#include<iostream>
#define MAXSIZE 20
#define OK 1
#define FALSE -1
#define ERROR -2
using namespace std;
typedef int EleType;
typedef int Status;
typedef struct
{
	EleType *element;
	int length;
 }SqList;
 //函数声明 
Status InitiSqList(SqList& L);//初始化顺序表函数 
Status GivenSqList(SqList& L);//键盘输入给定顺序表中元素函数
Status InsertLine(SqList& L,int i,EleType e);//插入顺序表函数
Status OrderInsert(SqList& L,EleType e);//有序插入顺序表函数
Status DeleteLine(SqList& L,int i);//元素删除函数
Status SortList(SqList& L);//元素排序函数
 void OutputList(SqList& L);//顺序表输出函数
 
 int main()//main函数 
 {
 	SqList L;
 	InitiSqList(L);
 	GivenSqList(L);
 	EleType Number;
 	int Position;
 	cout <<"please input the element 3、21、15、99and their position 1、8、4、12 to insert"<<endl;
 	//将元素插入其对应位置 
 	for(int i=0;i<4;i++) 
 	{
 		cin>>Number;
		cin>>Position;
 		InsertLine(L,Position,Number);
 		OutputList(L);
	 }

	 cout <<"please input the position 1、9、12 to delete"<<endl;
	 //删除对应位置的元素 
	 for(int j = 0;j<3;j++)
	 {
	 	cin>>Position;
	 	DeleteLine(L,Position);
	 	OutputList(L);
	 }
	 
	 cout<<"顺序表排序:"<<endl; 
	 SortList(L);
	 cout <<"insert the number 20 to the list in order"<<endl;
	 OrderInsert(L,20);//对20进行有序插入 
	 OutputList(L);
	 cout <<"insert the number 50 to the list in order"<<endl;
	 OrderInsert(L,50);
	 OutputList(L);
	 delete &L;
 	return 0;
 }
 
Status InitiSqList(SqList& L)//初始化顺序表 
 {
 	L.element=new EleType[MAXSIZE];
 	if(!L.element) return FALSE;
 	L.length=0;
 	return OK;
 }
 
Status GivenSqList(SqList& L)//键盘输入给定顺序表中元素 
 {
 	cout<<"intput L=(12,25,7,42,19,38) in order"<<endl;
 	for(int i=0;i<=5;i++)
 	{
 		cin>>L.element[i];
 		L.length++;
	 }
	 return OK;
 }
 
 void OutputList(SqList& L)//输出 
 {
 	if(L.length==0)
 	cout<<"empty list"<<endl; 
 	for(int i= 0;i<L.length;i++)
 	cout <<L.element[i]<<" ";
 	cout<<endl;
 }
 
Status InsertLine(SqList& L,int i,EleType e)//插入顺序表 
 {
 	if(i<1||i>L.length+1)
 	{
	 cout<<"insert failed,beyond the position scope"<<endl;
	 return ERROR;
	 }
	 if(L.length==MAXSIZE)
	 {
	 cout<<"insert failed,the list is full"<<endl;
	 return ERROR;
	 }
 	for(int j=L.length-1;j>=i-1;j--)
 	L.element[j+1]=L.element[j];
 	L.element[i-1]=e;
 	L.length++;
 	return OK;
 }
 
Status OrderInsert(SqList& L,EleType e)//有序插入
 {
 	int i=0;
 	while(e>=L.element[i]&&i<L.length)
	 i++;
	 for(int j=L.length;j>i-1;j--)
 	L.element[j]=L.element[j-1];
 	L.element[i]=e;
 	L.length++;
 	return OK;	 
 }
 
Status DeleteLine(SqList& L,int i)//删除 
 {
 	if(i<1||i>L.length)
 	{cout<<"delete failed,beyond the position scope"<<endl;
	 return ERROR;
	 }
 	for(int j=i-1;j<L.length-1;j++)
     L.element[j]=L.element[j+1];
     L.length--;
 	return OK;
 }
 
Status SortList(SqList& L)//排序 
 {
 	int t;
 	for(int i=0;i<L.length-1;i++)
 	  for(int j=0;j<L.length-i-1;j++)
 	 	if(L.element[j]>L.element[j+1])
 	 	{
 	 	  t=L.element[j];
 	 	  L.element[j]=L.element[j+1];
 	 	  L.element[j+1]=t;
		}
	   OutputList(L);
	  return OK;
 }

运行结果:

intput L=(12,25,7,42,19,38) in order
12 25 7 42 19 38
please input the element 3、21、15、99and their position 1、8、4、12 to insert
3 1
3 12 25 7 42 19 38
21 8
3 12 25 7 42 19 38 21
15 4
3 12 25 15 7 42 19 38 21
99 12
insert failed,beyond the position scope
3 12 25 15 7 42 19 38 21
please input the position 1、9、12 to delete
1
12 25 15 7 42 19 38 21
9
delete failed,beyond the position scope
12 25 15 7 42 19 38 21
12
delete failed,beyond the position scope
12 25 15 7 42 19 38 21
顺序表排序:
7 12 15 19 21 25 38 42
insert the number 20 to the list in order
7 12 15 19 20 21 25 38 42
insert the number 50 to the list in order
7 12 15 19 20 21 25 38 42 50

链表

#include<iostream>
#include<cstdlib>
#define MAXSIZE 20
#define OK 1
#define FALSE -1
#define ERROR -2
using namespace std;
typedef int EleType;
typedef int Status;
typedef struct Node
{
	EleType data;
	Node* pnext;
}LNode, * LinkList;
Status InitiLinkList(LinkList& L);//初始化链表 
Status InsertLinkList(LinkList& L, int i, EleType e);//插入结点
Status OrderInsert(LinkList& L, EleType e);//结点有序插入
Status DeleteLinkList(LinkList& L, int i);//删除结点 
Status SortList(LinkList& L);//结点排序
Status OutputList(LinkList& L);//链表输出 

int main()//main函数 
{
	LinkList L;
	InitiLinkList(L);  //初始化 
	EleType Number; //传值元素 
	int Position;   //位置参数 
	cout << "intput L=(12,25,7,42,19,38) in order" << endl;
	for (int i = 1; i <= 6; i++)
	{
		Position = i;
		cin >> Number;
		InsertLinkList(L, Position, Number);
	}
	OutputList(L);
	cout << "insert the element 3、21、15、99to their position 1、8、4、12 " << endl;
	InsertLinkList(L, 1, 3);
	OutputList(L);
	InsertLinkList(L, 8, 21);
	OutputList(L);
	InsertLinkList(L, 4, 15);
	OutputList(L);
	InsertLinkList(L, 12, 99);
	OutputList(L);
	cout << "delete the position 1、9、12" << endl;
	DeleteLinkList(L, 1);
	OutputList(L);
	DeleteLinkList(L, 9);
	OutputList(L);
	DeleteLinkList(L, 12);
	OutputList(L);
	cout << "顺序表排序:" << endl;
	SortList(L);
	OutputList(L);
	cout << "insert the number 20 to the list in order" << endl;
	OrderInsert(L, 20);
	OutputList(L);
	cout << "insert the number 50 to the list in order" << endl;
	OrderInsert(L, 50);
	OutputList(L);
	delete& L;
	return 0;
}

Status InitiLinkList(LinkList& L)//初始化顺序表 
{
	L = new LNode;
	if (!L) exit(FALSE);//申请失败,退出程序 
	L->pnext = NULL;
	L->data = 0; //头结点数据域储存链表有效元素的长度 
	return OK;
}

Status OutputList(LinkList& L)//输出 
{
	LNode* p = L;
	if (!p) {
		cout << "empty list!" << endl;
		return ERROR;
	}
	while ((p = p->pnext) != NULL)
		cout << p->data << " ";
	cout << endl;
	return OK;
}

Status InsertLinkList(LinkList& L, int i, EleType e)//插入顺序表 
{
	LNode* p = L, * s = new LNode;
	if (!s) exit(FALSE); //申请储存失败则退出程序 
	int j = 0;
	if (i > L->data + 1)
	{
		cout << "insert failed,beyond the position scope" << endl;
		return ERROR;
	}
	while (j < i - 1)
	{
		p = p->pnext;
		j++;
	}
	s->data = e; //结点插入 
	s->pnext = p->pnext;
	p->pnext = s;
	L->data++;
	return OK;
}

Status OrderInsert(LinkList &L,EleType e)//有序插入
 {
 	LNode *p=L;
 	LNode *s=new LNode;
 	s->data=e;
	 while(p->pnext)
	 {
	 	if(s->data<p->pnext->data)
	 	{
	 	 s->pnext=p->pnext;
	     p->pnext=s;
	     return OK;
		 }
		 p=p->pnext;
	 }
	    p->pnext=s;
	 	s->pnext=NULL;
	 return OK;
 }
 
Status DeleteLinkList(LinkList& L, int i)//删除结点 
{
	int j = 0; LNode* p = L, * del;
	if (i > L->data || i < 1)
	{
		cout << "delete failed,beyond the position scope" << endl;
		return ERROR;
	}
	while (j < i - 1)//找到要删除结点的前一个结点 
	{
		p = p->pnext;
		j++;
	}
	del = p->pnext;
	p->pnext = p->pnext->pnext;
	delete del;
	L->data--;
	return OK;
}

Status SortList(LinkList& L)//排序 
{
	LNode* Head = L, * Pre, * Cur, * Next, * End = NULL, * Temp;
	if (!Head) {
		cout << "empty list!" << endl;
		return ERROR;
	}
	while (Head->pnext != End)
	{
		for (Pre = Head, Cur = Pre->pnext, Next = Cur->pnext; Next != End; Pre = Pre->pnext, Cur = Cur->pnext, Next = Next->pnext)
		{
			if (Cur->data > Next->data)
			 {
				Cur->pnext = Next->pnext;
				Pre->pnext = Next;
				Next->pnext = Cur;
				Temp = Cur;
				Cur = Next;
				Next = Temp;
			}
		}
		End = Cur;
	}
}
//运行结果与上方顺序表相同

栈的基本函数

typedef int ElemType;
typedef int Status;
#define OK 1
#define FALSE -1
#define ERROR -2
#define MAXSIZE 100
typedef struct
{
	ElemType top;//栈顶
	ElemType* base;
}Sqstack;
bool StackEmpty(Sqstack& S);//判断栈空
Status InitiStack(Sqstack& S);//初始化栈
Status EnterElem(Sqstack& S,ElemType e);//元素入栈 
Status DeleteElem(Sqstack& S,ElemType e);//元素出栈 
Status GetTop(Sqstack& S);//取栈顶元素
Status ErgoStack(Sqstack& S);//遍历栈表

bool StackEmpty(Sqstack& S)//判断栈空
{
  if(S.top==0) return true;
  else return 0;
} 

Status InitiStack(Sqstack& S)//初始化栈
{
    S.base=new ElemType[MAXSIZE];//申请储存
    if(!S.base) {//基地址申请失败返回FALSE 
    	cout<<"fail to apply reserve"<<endl;
    	return FALSE;
	}
	S.top=0;//坐标初始化为0 
	return OK;
} 

Status EnterElem(Sqstack& S,ElemType e)//元素入栈 
{
   if(S.top==MAXSIZE) {//判断栈满,栈满返回FALSE 
		cout<<"stack fulled!"<<endl;
		return FALSE;
	} 
   S.base[S.top]=e;//元素 e入栈
   S.top++;//入栈成功栈头角标自增 
   return S.base[S.top-1];//用于检查进栈操作是否正确
}

Status DeleteElem(Sqstack& S,ElemType e)//元素出栈 
{
if(StackEmpty(S)) {//判断是否栈空,若空则返回FALSE 
cout<<"stack empty!"<<endl;
   	return FALSE;
   }
   e=S.base[S.top-1];//将栈头赋给元素e进行储存
   S.top--;//栈头删除成功,栈头坐标自减 
   return e;//用于检查出栈操作是否正确 
}  

Status GetTop(Sqstack& S)//取栈顶元素
{
   if(StackEmpty(S)) {//检查栈空 
   	cout<<"stack empty!"<<endl;
   	return FALSE;
   }
   return S.base[S.top-1];//返回栈头元素 
} 

Status ErgoStack(Sqstack& S)//遍历栈表
{
	if(StackEmpty(S)){//检查栈空 
   	cout<<"stack empty!"<<endl;
   	return FALSE;
    }
   for(int j=1;j<=S.top;j++)
   {cout<<S.base[S.top-j]<<" ";}
   cout<<endl;
	return OK;
}

队列应用——医院就诊系统

1、头文件

#ifndef _QUEUE_H
#define _QUEUE_H

#define OK 1
#define ERROR -1 
#define MAXSIZE 60
typedef int Status;
typedef int ElemType;
typedef char DataType;
struct Patient//病人个体信息 
{
	ElemType Medical_Num;//病历号 
	DataType Patient_Name[20];//病人名字 
 };
 struct WQueue//候诊队列 
 {
 	Patient* Queue;
	ElemType front;
	ElemType rear;
 };
    void PrintMenu();//打印菜单 
   Status WorkTime(WQueue& Q);//上班 
  Status Register(WQueue& Q,Patient p);//排队候诊
  Status QueueEmpty(WQueue& Q);//判断队空 
 Status Consultation(WQueue& Q,Patient p);//出队就诊
 Status QueueLength(WQueue& Q);//查看看诊 
 Status CheckQueue(WQueue& Q);//查看排队
 
#endif

2、函数实现文件

#include<iostream>
#include"Queue.h"
using namespace std;
void PrintMenu()//打印菜单 
{
	printf(" ┏-----------------┓\n");
	printf(" ┃      MENU       ┃\n");
	printf(" ┠-----------------┨\n");
	printf(" ┠ 1、候诊  2、就诊┃\n");
	printf(" ┠ 3、查看排队     ┃\n");           
	printf(" ┠ 4、查看看诊     ┃\n");
	printf(" ┠ 5、下班 6、上班 ┃\n");
	printf(" ┗-----------------┛\n");
}
Status WorkTime(WQueue& Q)//上班 
{//(6)上班——初始化排队队列
	Q.Queue=new Patient[MAXSIZE];//为基地址申请内存 
	if(!Q.Queue) {//判断是否申请成功,失败则程序退出
	cout<<"ERROR:apply false"<<endl;
	return ERROR; 
        }
	Q.front=Q.rear=0;//下标初始化为0 
	return OK;
}
Status Register(WQueue& Q,Patient p)//排队候诊 
{//(1)挂号候诊——输入病人信息,病人入队候诊 
	if((Q.rear+1)%MAXSIZE==Q.front) {//判断是否表满
	    cout<<"queue full"<<endl;
	          return -1;
	        }
	  Q.Queue[Q.rear]=p;//将新病人e的数据赋给队尾,即入队 
	    Q.rear=(Q.rear+1)%MAXSIZE;//队尾+1 
	return OK;
}
Status QueueEmpty(WQueue& Q)//判断队空
{
	if(Q.rear==Q.front) return OK;
	  else return 0;
 } 
Status Consultation(WQueue& Q,Patient p)//出队就诊 
{//(2)就诊——队首病人就诊,队列删除元素。
	if(QueueEmpty(Q)) {//判断是否队空 
	 cout<<"nobody waiting in the queue"<<endl;
	  return -1;
      }
	    p=Q.Queue[Q.front];//将队首赋给p进行存储,即出队就诊 
	  Q.front=(Q.front+1)%MAXSIZE;//队首+1 
	return p.Medical_Num;//返回就诊病人病历号 
}
Status QueueLength(WQueue& Q)//求队列长度(返回候诊人数) 
{//(4)查看看诊——判断是否还有人候诊,如有显示候诊人数。
	return (Q.rear-Q.front+MAXSIZE)%MAXSIZE;
}
Status CheckQueue(WQueue& Q)//队列全输出 
{//(3)查看排队——输入查看人的病历号,显示其之前排队病人的人数以及其病历号和姓名。
	if(QueueEmpty(Q)) 
	{//判断是否队空,队空返回-1 
	  cout<<"nobody waiting treatment"<<endl;
	return -1;
      }
     else
     { cout<<"patiences in the waiting queue:"<<endl;
	   
	   for(int i=Q.front;i<Q.rear;i=(i+1)%MAXSIZE)//输出全队列的数据 
	   {
	      cout<<"medical number:"<<Q.Queue[i].Medical_Num<<" ";
			cout<<"patient name:"<<Q.Queue[i].Patient_Name<<endl;	
	   }
	 	   cout<<endl;
	 }
	 return OK; 
}

3、main函数文件

#include <iostream>
using namespace std;
#include"Queue.h"
int main() 
{// MENU: 1、候诊  2、就诊  3、查看排队  4、查看看诊  5、下班  6、上班
	  while(1)
    {
        WQueue queue;//定义候诊队列 
	  Patient transdata;//病人挂号/数据传递参数 
	    int Option,Count,i=0;//Option 选项参数,Count,i计数参数 
	 PrintMenu();//打印菜单选项
	cout<<"please input the option:"<<endl;
	 cin>>Option;//输入选项 
      switch(Option)
       {//1、候诊
	   case 1:if(!queue.Queue)
	   { //医院是否上班
         cout<<"the hosptial is off duty"<<endl;
    	return 0;	
	      }   
	   cout<<"input the number of patient:";
    	 cin>>Count;           //输入候诊人数 
    	 //判断是否大于队列容纳最大量,若超过则限制入列最大量 
    	 if(Count>MAXSIZE) {cout<<"hospital has full of patients"<<endl;
	      Count=60; }
		 cout<<"input the patients' data to register"<<endl<<endl;
		 while(i<Count)
		 {cout<<"intput the "<<i+1<<"st patien's data"<<endl;i++;
		       cin>> transdata.Medical_Num;//采集病人病历号 
                 cin>>transdata.Patient_Name;//采集病人姓名 
    	  Register(queue,transdata);//病历登记 
		 }cout<<"successful register"<<endl;break; 
		 //2、就诊
		 case 2:Consultation(queue,transdata);break;//队首出队就诊
		 //3、查看排队
		 case 3:CheckQueue(queue);break;
		 //4、查看看诊
		 case 4:if(QueueEmpty(queue)) {//判断是否队空 
	          cout<<"nobody waiting treatment"<<endl;
                 }
		 cout<<"number of patients waiting in the queue:"<<QueueLength(queue)<<endl;break;
		 //5、下班
    	 case 5: cout<<"the hospital is off duty"<<endl;return 0;break; 
    	 //6、上班
    	 case 6:if(WorkTime(queue)) cout<<"the hospital is on duty"<<endl;break;
    			default: 
				{cout<<"ERROR: beyond the scope"<<endl;
					return ERROR;
				}
       }
             system("pause");
	      system("cls");
	  }
	return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值