|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- #include<iostream>
- #include<time.h>
- #include<stdlib.h>
- using namespace std;
- #define ElemType int
- #define MaxSize 100 //定义顺序表的最大储存个数是100
- struct List
- {
- ElemType data[MaxSize];
- int length;
- };
- //建立顺序表(定义一个函数建立顺序表,将数组a的元素给顺序表)
- void creatList(struct List *&L, ElemType a[], int n)
- {
- int length = 0;
- L = new struct List;
- for (int i = 0; i < n; i++)
- {
- L->data[i] = a[i];
- length += 1;
- }
- L->length = length;
- }
- //产生随机数
- ElemType* randomInterger()
- {
- ElemType *Interger = new ElemType[10];
- srand((unsigned)time(NULL));
- for (int i = 0; i < 10; i++)
- {
- Interger[i] = 100 + rand() % 899;
- }
- return Interger;
- }
- //输出链表的内容
- void display(struct List L)
- {
- if (L.length == 0)
- {
- cout << "顺序表为空!" << endl;
- exit(0);
- }
- for (int i = 0; i < L.length; i++)
- {
- cout << L.data[i] << "\t";
- }
- cout << endl;
- }
- //读入一个整数,查看该整数是否在表中,若在,输出其位置(首位置为1)
- int findLocation(struct List *&L, ElemType a)
- {
- int index = 1;
- for (int i = 0; i < L->length; i++)
- {
- if (a == L->data[i])
- {
- cout << "该数的位置在" << index + i << endl;
- return index + i;
- }
- else
- {
- cout << "该数不在表中!" << endl;
- return 0;
- }
- }
- }
- //读入一个整数,以及要插入的位置,把该整数插入到链表中,输出链表的内容(要求判断输入的位置是否合理);
- void Insert(struct List *&L, ElemType a, int index)
- {
- int len = L->length;
- //判断插入位置
- if (index<0 || index>L->length || L->length == MaxSize)
- {
- cout << "插入失败!" << endl;
- exit(0);
- }
- else
- {
- for (int i = len; i >= index; i--)
- {
- L->data[i] = L->data[i - 1];
- }
- L->data[index] = a;
- L->length++;
- }
- }
- //读入一个整数,若该整数在链表里,删除该整数,输出链表的内容;
- void Delete(struct List *&L, ElemType deleteInteger)
- {
- int Location = 0;
- if (!findLocation(L, deleteInteger))
- {
- exit(0);
- }
- else
- {
- Location = findLocation(L, deleteInteger);
- for (int i = Location - 1; i < L->length; i++)
- {
- L->data[i] = L->data[i + 1];
- }
- L->length--;
- }
- }
- void converse_and_display(struct List *&L)
- {
- ElemType *data = new ElemType[L->length];
- for (int i = 0; i < L->length; i++)
- {
- data[L->length - i - 1] = L->data[i];
- }
- for (int i = 0; i < L->length; i++)
- {
- L->data[i] = data[i];
- }
- display(*L);
- }
- //创建一个操作菜单
- void menu()
- {
- cout << "*****************顺序表的操作*****************" << endl;
- cout << "**********************************************" << endl;
- cout << "* 1.判断一个数是否存在并输出位置 *" << endl;
- cout << "* 2.插入数据到顺序表中 *" << endl;
- cout << "* 3.给定删除元素删除顺序表中的元素 *" << endl;
- cout << "* 4.翻转并显示 *" << endl;
- cout << "* 5.输出顺序表 *" << endl;
- cout << "* 6.退出 *" << endl;
- cout << "**********************************************" << endl;
- int choice = 0;
- ElemType *Interger = randomInterger();
- ElemType find;
- ElemType insertInterger;
- ElemType deleteInteger;
- int index;
- struct List *L;
- creatList(L, Interger, 10);
- while (1)
- {
- cout << "请输入你的操作:";
- cin >> choice;
- if (choice == 6)
- break;
- switch (choice)
- {
- case 1:
- cout << "请输入需要判断是否存在的一个数:";
- cin >> find;
- findLocation(L, find);
- break;
- case 2:
- cout << "请你输入一个整数和需要插入的位置:";
- cin >> insertInterger;
- cin >> index;
- Insert(L, insertInterger, index);
- break;
- case 3:
- cout << "请输入要删除的整数:" << endl;
- cin >> deleteInteger;
- Delete(L, deleteInteger);
- break;
- case 4:
- converse_and_display(L);
- break;
- case 5:
- display(*L);
- break;
- default:cout << "输入有误!" << endl;
- }
- }
- }
- int main()
- {
- menu();
- system("pause");
- return 0;
- }
复制代码 |
|