鱼C论坛

 找回密码
 立即注册
查看: 2520|回复: 0

[学习笔记] 顺序表

[复制链接]
发表于 2019-11-17 15:10:02 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. #include<iostream>
  2. #include<time.h>
  3. #include<stdlib.h>
  4. using namespace std;

  5. #define ElemType int
  6. #define MaxSize 100    //定义顺序表的最大储存个数是100

  7. struct List
  8. {
  9.         ElemType data[MaxSize];
  10.         int length;
  11. };


  12. //建立顺序表(定义一个函数建立顺序表,将数组a的元素给顺序表)
  13. void creatList(struct List *&L, ElemType a[], int n)
  14. {
  15.         int length = 0;
  16.         L = new struct List;
  17.         for (int i = 0; i < n; i++)
  18.         {
  19.                 L->data[i] = a[i];
  20.                 length += 1;
  21.         }
  22.         L->length = length;
  23. }

  24. //产生随机数
  25. ElemType* randomInterger()
  26. {
  27.         ElemType *Interger = new ElemType[10];
  28.         srand((unsigned)time(NULL));
  29.         for (int i = 0; i < 10; i++)
  30.         {
  31.                 Interger[i] = 100 + rand() % 899;
  32.         }
  33.         return Interger;
  34. }

  35. //输出链表的内容
  36. void display(struct List L)
  37. {
  38.         if (L.length == 0)
  39.         {
  40.                 cout << "顺序表为空!" << endl;
  41.                 exit(0);
  42.         }
  43.         for (int i = 0; i < L.length; i++)
  44.         {
  45.                 cout << L.data[i] << "\t";
  46.         }
  47.         cout << endl;
  48. }

  49. //读入一个整数,查看该整数是否在表中,若在,输出其位置(首位置为1)
  50. int findLocation(struct List *&L, ElemType a)
  51. {
  52.         int index = 1;
  53.         for (int i = 0; i < L->length; i++)
  54.         {
  55.                 if (a == L->data[i])
  56.                 {
  57.                         cout << "该数的位置在" << index + i << endl;
  58.                         return index + i;
  59.                 }
  60.                 else
  61.                 {
  62.                         cout << "该数不在表中!" << endl;
  63.                         return 0;
  64.                 }
  65.         }
  66. }


  67. //读入一个整数,以及要插入的位置,把该整数插入到链表中,输出链表的内容(要求判断输入的位置是否合理);
  68. void Insert(struct List *&L, ElemType a, int index)
  69. {
  70.         int len = L->length;
  71.         //判断插入位置
  72.         if (index<0 || index>L->length || L->length == MaxSize)
  73.         {
  74.                 cout << "插入失败!" << endl;
  75.                 exit(0);
  76.         }
  77.         else
  78.         {
  79.                 for (int i = len; i >= index; i--)
  80.                 {
  81.                         L->data[i] = L->data[i - 1];
  82.                 }
  83.                 L->data[index] = a;
  84.                 L->length++;
  85.         }
  86. }


  87. //读入一个整数,若该整数在链表里,删除该整数,输出链表的内容;
  88. void Delete(struct List *&L, ElemType deleteInteger)
  89. {
  90.         int Location = 0;
  91.         if (!findLocation(L, deleteInteger))
  92.         {
  93.                 exit(0);
  94.         }
  95.         else
  96.         {
  97.                 Location = findLocation(L, deleteInteger);
  98.                 for (int i = Location - 1; i < L->length; i++)
  99.                 {
  100.                         L->data[i] = L->data[i + 1];
  101.                 }
  102.                 L->length--;
  103.         }
  104. }

  105. void converse_and_display(struct List *&L)
  106. {
  107.         ElemType *data = new ElemType[L->length];
  108.         for (int i = 0; i < L->length; i++)
  109.         {
  110.                 data[L->length - i - 1] = L->data[i];
  111.         }
  112.         for (int i = 0; i < L->length; i++)
  113.         {
  114.                 L->data[i] = data[i];
  115.         }
  116.         display(*L);
  117. }

  118. //创建一个操作菜单
  119. void menu()
  120. {
  121.         cout << "*****************顺序表的操作*****************" << endl;
  122.         cout << "**********************************************" << endl;
  123.         cout << "*        1.判断一个数是否存在并输出位置      *" << endl;
  124.         cout << "*        2.插入数据到顺序表中                *" << endl;
  125.         cout << "*        3.给定删除元素删除顺序表中的元素    *" << endl;
  126.         cout << "*        4.翻转并显示                        *" << endl;
  127.         cout << "*        5.输出顺序表                        *" << endl;
  128.         cout << "*        6.退出                              *" << endl;
  129.         cout << "**********************************************" << endl;

  130.         int choice = 0;
  131.         ElemType *Interger = randomInterger();
  132.         ElemType find;
  133.         ElemType insertInterger;
  134.         ElemType deleteInteger;
  135.         int index;

  136.         struct List *L;
  137.         creatList(L, Interger, 10);

  138.         while (1)
  139.         {
  140.                 cout << "请输入你的操作:";
  141.                 cin >> choice;

  142.                 if (choice == 6)
  143.                         break;
  144.                 switch (choice)
  145.                 {
  146.                 case 1:
  147.                         cout << "请输入需要判断是否存在的一个数:";
  148.                         cin >> find;
  149.                         findLocation(L, find);
  150.                         break;
  151.                 case 2:
  152.                         cout << "请你输入一个整数和需要插入的位置:";
  153.                         cin >> insertInterger;
  154.                         cin >> index;
  155.                         Insert(L, insertInterger, index);
  156.                         break;
  157.                 case 3:
  158.                         cout << "请输入要删除的整数:" << endl;
  159.                         cin >> deleteInteger;
  160.                         Delete(L, deleteInteger);
  161.                         break;
  162.                 case 4:
  163.                         converse_and_display(L);
  164.                         break;
  165.                 case 5:
  166.                         display(*L);
  167.                         break;
  168.                 default:cout << "输入有误!" << endl;
  169.                 }
  170.         }
  171. }
  172. int main()
  173. {
  174.         menu();
  175.         system("pause");
  176.         return 0;
  177. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-13 14:22

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表