简单的数字顺序表游戏

来源:互联网 发布:nginx 快速配置 编辑:程序博客网 时间:2024/06/10 08:00
#include<iostream>
#include<stdlib.h>
using namespace std;
typedef int ElemType;
const int Maxsize=100;


struct List 
{
int size;
ElemType list[Maxsize];
};
void InitList(List &L)//初始化
{   
L.size=0;
}
void ClearList(List &L)//清空
{
L.size=0;
}
int LenthList(List &L)//得到线性表的长度
{
return L.size;
}
bool EmptyList(List &L)//判断线性表是否为空
{
return L.size==0;
}
ElemType GetList(List &L,int pos)//找出给定序号pos的元素
{
if(pos<1 || pos>L.size)
{
cout << " pos is out range! " << endl;
exit(1);
}
return L.list[pos-1];
}
void TraverseList(List &L)//遍历线性表
{
for(int i=0; i<L.size; i++)

cout << L.list[i] << ' ';


cout << endl;
}
bool FindList(List &L, ElemType& item)//找出给定值
{
for(int j=0; j<L.size;j++)
if(L.list[j] == item)
{
item = L.list[j];
return true;
}
return false;
}
bool UpdateList(List &L, const ElemType& item)//更新线性表的值
{
for(int k=0; k<L.size; k++)
if(L.list[k] == item)
{
L.list[k] = item;
return true;
}
return false;
}
bool InsertList(List &L, ElemType item, int pos)//按指定条件插入元素
{
if(pos<-1 || pos>L.size+1)
{
cout << "pos值无效!" << endl;
return false;
}
 if(pos==0)
 {
for(int l=0; l<L.size; l++)
if(item<L.list[l]) break;
pos=l+1;
 }

  else if(pos==-1) pos=L.size+1;


 for(int p=L.size-1; p>=pos-1;p--)

L.list[p+1]=L.list[p];


         L.list[pos-1]=item;
 L.size++;
return true;
}
bool DeleteList(List &L, ElemType& item, int pos)//按指定条件删除元素
{
if(L.size==0)
{
cout << "线性表为空,删除无效!" << endl;
return false;

}


if(pos<-1 || pos>L.size)
{
cout << "pos值无效!" << endl;
return false;

}


for(int n=0; n<L.size; n++)
{

if(item==L.list[n]) break;


if(n==L.size) return false;


pos=n+1;

}


if(pos==-1) pos=L.size;

item = L.list[pos-1];


for(int r=pos; r<L.size; r++)
{
L.list[r-1]=L.list[r];
}
L.size--;
return true;
}
void SortList(List &L)//排序
{
ElemType x;
for(int p=1; p<L.size; p++)
{

x=L.list[p];


for(int q=p-1; q>=0; q--)

if(x<L.list[p]) L.list[q+1]=L.list[q];


else break;
L.list[q+1]=x;
}
}


int main()
{
int a[12]={3,4,7,9,13,16,17,34,23,12,2,49};
List t;

InitList(t);


for(int i=0; i<12; i++) InsertList(t, a[i],i+1);
    TraverseList(t);
cout << " 欢迎来到×××的数字顺序表游戏! " << endl;
cout << " 请选择:" << endl;
cout << " 1.新建数字顺序表 " << endl;
cout << " 2.查找某数是否在表中 " << endl;
cout << " 3.插入一个数字到表中 " << endl;
cout << " 4.删除表中某数" << endl;
cout << " 5.给数字顺序表排序 " << endl;
cout << " 6.屏幕输出整个表 " << endl;
cout << " 7.不想玩了,拜拜! " << endl;

cout << "请输入您的选择:" ;


int b=0, d[100];
int e=0, g=0;
int z=0, h=0 ;
int f=0, y=0;
cin >> b;


for(int b1=0; ;b++)
{
switch(b)
{
case 1:
cout << "请输入线性表的长度n(n>0且n<100):"  << endl; 

List t1;
InitList(t1);

cin >> b;


cout << "请输入表中元素的值:" << endl;
for(; e<b; e++)
cin >> d[e];
        for(; f<b; f++)
InsertList(t1, d[f],f);
                      TraverseList(t1);

break;


case 2:

   cout << "输入待查找的元素值:";
       cin >> g;
   if(FindList(t,g)) 
cout << "查找成功!" << endl;
    else cout << "查找失败!" << endl;

break;


case 3:
 
                  cout << "按值插入,输入待插入元素的值:";
     cin >> h;
     if(InsertList(t,h,0)) cout << "插入成功!" << endl;
     else cout << "插入失败!" << endl;
          TraverseList(t);

 break;


case 4:
  
  cout << "输入待删除元素的值:" << endl;
      cin >> y;
      if(DeleteList(t,y,0)) cout << "删除成功!" << endl;
      else cout << "删除失败!" << endl;
  TraverseList(t);

  break;


case 5:
cout << "下面将给数字表排序:";
SortList(t);
TraverseList(t);

break;


case 6:
cout << "下面将输出整个表:" << endl;
TraverseList(t);

break;


case 7:
cout << " 不想玩了,拜拜! " << endl;

break;


default: 
cout << " Error " << endl; 
break;
}
if(b==7) exit(1);
    cout << " 欢迎来到×××的数字顺序表游戏! " << endl;
cout << " 请选择:" << endl;
cout << " 1.新建数字顺序表 " << endl;
cout << " 2.查找某数是否在表中 " << endl;
cout << " 3.插入一个数字到表中 " << endl;
cout << " 4.删除表中某数" << endl;
cout << " 5.给数字顺序表排序 " << endl;
cout << " 6.屏幕输出整个表 " << endl;
cout << " 7.不想玩了,拜拜! " << endl;
cout << "请输入您的选择:" ;
cin >> b;
}
    
return 0;
}
0 0
原创粉丝点击