鱼C论坛

 找回密码
 立即注册
楼主: 小甲鱼

[技术交流] 静态顺序表的各种操作(线性表)

  [复制链接]
发表于 2015-5-10 12:05:01 | 显示全部楼层
给力,嘻嘻
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-5-10 13:18:47 | 显示全部楼层
强烈支持楼主ing……
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-5-10 15:25:14 | 显示全部楼层
强烈支持楼主ing……
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-5-10 19:16:40 | 显示全部楼层
辛苦了, 小甲鱼:handshake
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-5-10 21:23:31 | 显示全部楼层
真是难得给力的帖子啊。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-5-12 10:27:21 | 显示全部楼层
真是难得给力的帖子啊。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-5-19 12:39:32 | 显示全部楼层
谢谢小甲鱼了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-5-31 00:02:34 | 显示全部楼层
强烈支持楼主ing……
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-6-3 15:58:49 | 显示全部楼层
强烈支持楼主ing……
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-6-6 20:41:38 | 显示全部楼层
我只是路过打酱油的。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-6-9 17:17:31 | 显示全部楼层
小甲鱼棒棒哒
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-6-12 10:17:17 | 显示全部楼层
我会加油学会的
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-6-15 14:00:01 | 显示全部楼层
不错,学习了,顶楼主!!!!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-6-17 10:30:42 | 显示全部楼层
谢谢小甲鱼
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-6-21 15:19:16 | 显示全部楼层
谢谢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2015-6-22 10:22:47 | 显示全部楼层
谢谢楼主
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-6-22 15:32:41 | 显示全部楼层
本帖最后由 seii 于 2015-6-22 15:36 编辑
  1. /*****************************/
  2. /** By Seii  **/
  3. /*****************************/
  4. #ifndef _LIST_H_
  5. #define _LIST_H_
  6. #include <stdio.h>
  7. #include <stdlib.h>

  8. typedef enum{
  9.         false, true
  10. }bool;

  11. typedef enum{
  12.         ERROR, OK
  13. }status;

  14. typedef int ElemType;
  15. typedef int CursorType;
  16. #define MAXSIZE 10

  17. typedef struct _list{
  18.         ElemType data;
  19.         CursorType cur;
  20. }Component, SLList[MAXSIZE];

  21. #define _IsEmpty(SSLptr) ((SSLptr)[MAXSIZE-1].cur==0)
  22. #define _IsFull(SSLptr) ((SSLptr[0].cur==0))

  23. status InitSLL(SLList list);//初始化
  24. void PrintSLL(const SLList list);//打印
  25. status InsertBackSLL(SLList list, ElemType data);//尾部插入数据
  26. void MoveToLast(SLList list, CursorType *lastCur);//移动到最后一个元素
  27. status InsertFrontSLL(SLList list, ElemType data);//头部插入数据
  28. size_t GetRestSpace(const SLList list);//获取剩余的存储空间
  29. status MoveToPrevPos(const SLList list, int pos, CursorType *target);//移动到pos处的前一个元素
  30. status InsertByPos(SLList list, int pos, ElemType data);//根据位置插入数据
  31. status RemoveByPos(SLList list, int pos);//根据位置删除元素

  32. #endif //_LIST_H_
复制代码

  1. #include "list.h"

  2. status InitSLL(SLList list){//初始化
  3.         if (list == NULL)return ERROR;
  4.         for (int i = 0; i < MAXSIZE - 1; i++){
  5.                 list[i].cur = i + 1;
  6.                 list[i].data = 0;
  7.         }
  8.         list[MAXSIZE - 1].data = 0;
  9.         list[MAXSIZE - 1].cur = 0;
  10.         list[MAXSIZE - 2].cur = 0;
  11.         return OK;
  12. }

  13. void PrintSLL(const SLList list){//打印
  14.         if (list == NULL)
  15.                 puts("***********************该链表无效********************\n");
  16.         else if (_IsEmpty(list))
  17.                 puts("***********************该链表为空********************\n");
  18.         else{
  19.                 puts("***********************链表数据START********************");
  20.                 for (CursorType i = list[MAXSIZE - 1].cur; i != 0; i = list[i].cur){
  21.                         printf("%d  ", list[i].data);
  22.                 }
  23.                 putchar('\n');
  24.                 puts("***********************链表数据E N D********************\n");
  25.         }
  26. }

  27. status InsertBackSLL(SLList list, ElemType data){//尾部插入数据
  28.         if (list == NULL)return ERROR;
  29.         CursorType last;
  30.         if (_IsEmpty(list))
  31.                 list[MAXSIZE - 1].cur = 1;
  32.         MoveToLast(list, &last);
  33.         list[last].cur = list[0].cur;
  34.         list[0].cur = list[list[last].cur].cur;
  35.         list[list[last].cur].data = data;
  36.         list[list[last].cur].cur = 0;
  37.         return OK;
  38. }

  39. void MoveToLast(SLList list, CursorType *lastCur){//移动到最后一个元素
  40.         if (list == NULL || _IsEmpty(list) || lastCur == NULL)
  41.                 *lastCur = 0;
  42.         else{
  43.                 *lastCur = list[MAXSIZE - 1].cur;
  44.                 while (list[*lastCur].cur != 0)*lastCur = list[*lastCur].cur;
  45.         }
  46. }

  47. status InsertFrontSLL(SLList list, ElemType data){//头部插入数据
  48.         if (list == NULL || _IsFull(list))return ERROR;
  49.         CursorType newnode = list[0].cur;
  50.         list[newnode].data = data;
  51.         list[0].cur = list[newnode].cur;
  52.         list[newnode].cur = list[MAXSIZE - 1].cur;
  53.         list[MAXSIZE - 1].cur = newnode;
  54.         return OK;
  55. }

  56. size_t GetRestSpace(const SLList list){//获取剩余的存储空间
  57.         if (list == NULL)return -1;
  58.         size_t count = 0;
  59.         for (CursorType cur = list[0].cur; cur != 0; cur = list[cur].cur, count++);
  60.         return count;
  61. }

  62. status MoveToPrevPos(const SLList list, int pos, CursorType *target){//移动到pos处的前一个元素
  63.         if (list == NULL)return ERROR;
  64.         *target = MAXSIZE - 1;
  65.         pos--;
  66.         while (pos && *target != 0){
  67.                 *target = list[*target].cur;
  68.                 pos--;
  69.         }
  70.         if (pos == 0){
  71.                 return OK;
  72.         }
  73.         else{
  74.                 *target = -1;

  75.                 //puts("您的插入超出了范围");

  76.                 return ERROR;
  77.         }
  78. }

  79. status InsertByPos(SLList list, int pos, ElemType data){//根据位置插入数据
  80.         if (list == NULL || _IsFull(list))return ERROR;
  81.         CursorType target;
  82.         if (MoveToPrevPos(list, pos, &target)){
  83.                 CursorType next = list[target].cur;
  84.                 list[target].cur = list[0].cur;
  85.                 list[0].cur = list[list[target].cur].cur;
  86.                 list[list[target].cur].data = data;
  87.                 list[list[target].cur].cur = next;
  88.                 return OK;
  89.         }
  90.         return ERROR;
  91. }

  92. status RemoveByPos(SLList list, int pos){//根据位置删除元素
  93.         if (list == NULL || _IsEmpty(list) || pos < 1)return ERROR;
  94.         CursorType target;
  95.         if (MoveToPrevPos(list, pos, &target)){
  96.                 CursorType curTarget = list[target].cur;
  97.                 list[target].cur = list[curTarget].cur;
  98.                 list[curTarget].cur = list[0].cur;
  99.                 list[0].cur = target;
  100.                 return OK;
  101.         }
  102.         return ERROR;
  103. }
复制代码

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "list.h"

  4. int main(){
  5.         SLList list;
  6.         InitSLL(list);

  7.         for (int i = 0; i < 5; i++){
  8.                 InsertFrontSLL(list, i + 1);
  9.         }

  10.         PrintSLL(list);
  11.         printf("rest=%d\n", GetRestSpace(list));

  12.         RemoveByPos(list, 3);


  13.         PrintSLL(list);
  14.         printf("rest=%d\n", GetRestSpace(list));

  15.         system("pause");
  16.         return 0;
  17. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-6-23 22:19:02 | 显示全部楼层
...
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2015-6-24 21:15:06 | 显示全部楼层
好牛呀
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-7-2 08:04:33 | 显示全部楼层
看看
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-28 05:01

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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