找回密码
 用户注册

QQ登录

只需一步,快速开始

查看: 3184|回复: 0

线性表的顺序表示与实现 |flying0033

[复制链接]
发表于 2011-12-21 11:30:42 | 显示全部楼层 |阅读模式
/*        线性表是有N个元素的非空有限序列        存在惟一的一个被称作“第一个”数据的元素        存在惟一的一个被称作“第后一个”数据的元素        除第一个与最后一个之外,其它元素都存在唯一的一个前驱和唯一的一个后续        复杂的线性表中的元素可以由多个数据项组成        同一个线性表中的元素类型必须相同        线性表的顺序表,是用一组地址连续的存储单元依次存储线性表的数据元素         算法复杂度:取表长与访问表中的的元素为O(1),插入与删除的时间复杂度为O(n)        顺序表适用于频繁访问,只有较少(或不进行)“插入删除操作” */
  1. #ifndef SQLIST_H
  2. #define SQLIST_H
  3. #define MAXSIZE  100 //顺序表的最大存储容量
  4. #define INFINITY 65535 //设置为β
  5. typedef int ElemType;  //顺序表的数据类型,自定义
  6. typedef struct {
  7.         ElemType data[MAXSIZE]; //定义一个顺序表,大小为MAXSIZE
  8.         int len;                                //顺序表的当前长度
  9. }SqList;
  10. void InitList(SqList *q); //初始化顺序表
  11. void GetElem(SqList *q,int i,ElemType *e); //返回元素,将第i个位置的元素存入*e
  12. void GetListLength(SqList *q,int *len); //返回顺序表的长度,将长度存入*len
  13. void InsertList(SqList *q,int i,ElemType e); //在第i个位置插入e,最坏的情况下i为第一个位置,时间复杂度为O(n)
  14. void DeleteList(SqList *q,int i,ElemType *e); //删除第i个位置的元素,将删除的元素存入*e,最坏情况i为第一个位置
  15.                                                                                           //时间复杂度为O(n)
  16. int LocateElem(SqList *q,ElemType e); //在顺序表中查找元素e是否存在,不存在返回-1,否则返回所在的位置
  17. #endif //SQLIST_H
  18. #include "SqList.h"
  19. #include <stdio.h>
  20. #include <string.h>
  21. void InitList(SqList *q) //初始化顺序表
  22. {
  23.         memset(q->data,0,sizeof(ElemType) * MAXSIZE); //初始化顺序表的元素为0
  24.         q->len = 0; //初始化顺序表的当前大小为0
  25. }
  26. void GetElem(SqList *q,int i,ElemType *e) //获取第i个位置的元素(下标值从零开始,i个位置的元素在表中位置为i-1)
  27. {
  28.         if(i > 0 && i <= q->len) //如果i存在于顺序表中
  29.                 *e = q->data[i - 1]; //返回第i个位置的元素
  30.         else
  31.                 *e = INFINITY; //返回β值
  32. }
  33. void InsertList(SqList *q,int i,ElemType e) //在顺序表的第i个位置插入元素e
  34. {
  35.         if(i > MAXSIZE || i <= 0)
  36.         {
  37.                 printf("超出范围,元素插入失败\n");
  38.                 return;
  39.         }
  40.         for(int j = q->len - 1;j >= i - 1;--j)//将i个位置与i之后的所有元素向后移一位(下标值从零开始i - 1)
  41.                 q->data[j + 1] = q->data[j];
  42.         q->data[i - 1] = e; //将元素插入到顺序表中,下标值从零开始
  43.         ++q->len; //增加当前顺序表的长度值
  44. }
  45. void DeleteList(SqList *q,int i,ElemType *e) //将顺序表中第i个位置的元素删除,将删除的元素存入*e
  46. {
  47.         if(i <= 0 && i > q->len)
  48.         {
  49.                 printf("超出范围,元素删除失败\n");
  50.                 *e = INFINITY; //返回β值
  51.                 return;
  52.         }
  53.         *e = q->data[i - 1];
  54.         for(int j = i - 1;j < q->len - 1;++j) //第i个位置之后的所有元素向后移
  55.                 q->data[j] = q->data[j + 1];
  56.         --q->len; //当前的顺序表长度减1
  57. }
  58. int LocateElem(SqList *q,ElemType e) //在顺序表中查找元素e,如果找到返回位置,否则返回-1
  59. {
  60.         for(int i = 0;i < q->len;++i)
  61.         {
  62.                 if(q->data[i] == e)
  63.                         return i + 1; //下标从0开始,所以要加上1
  64.         }
  65.         return -1;
  66. }
  67. #include "SqList.h"
  68. #include <stdio.h>
  69. int main()
  70. {
  71.         SqList q;
  72.         InitList(&q); //初始化顺序表
  73.         int element;
  74.         int i = 1; //插入的位置
  75.         printf("请输入要插入顺序表中的内容:(CTRL + Z)结束\n");
  76.         while((scanf("%d",&element)) != EOF)
  77.         {
  78.                 InsertList(&q,i,element);
  79.                 ++i;
  80.         }
  81.         i = 2; //第顺序表的第2个位置插入元素
  82.         fflush(stdin); //清空缓冲区,确保输入正确
  83.         printf("请输入要在第2个位置插入的元素\n");
  84.         scanf("%d",&element);
  85.         InsertList(&q,i,element);
  86.         printf("顺序表的内容为:\n");
  87.         for(int j = 0;j < q.len;++j)
  88.                 printf("%d ",q.data[j]);
  89.         printf("\n");
  90.         int e; //用于存入被删除的元素
  91.         printf("请输入要删除的元素的位置\n");
  92.         fflush(stdin);
  93.         scanf("%d",&i);
  94.         DeleteList(&q,i,&e);
  95.         printf("被删除的元素为:%d\n",e);
  96.         printf("顺序表的内容为:\n");
  97.         for(int j = 0;j < q.len;++j)
  98.                 printf("%d ",q.data[j]);
  99.         printf("\n");
  100.         printf("请输入要在表中查找的元素:\n");
  101.         fflush(stdin);
  102.         scanf("%d",&element);
  103.         if((i = LocateElem(&q,element)) == -1)
  104.                 printf("顺序表中不存在元素:%d\n",element);
  105.         else
  106.                 printf("在顺序表的第%d个位置找到元素:%d\n",i,element);
  107.         return 0;
  108. }
复制代码


作者:flying0033 发表于2011-12-20 11:06:41 原文链接
您需要登录后才可以回帖 登录 | 用户注册

本版积分规则

Archiver|手机版|小黑屋|ACE Developer ( 京ICP备06055248号 )

GMT+8, 2024-5-7 22:41 , Processed in 0.014009 second(s), 6 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

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