Chinaunix首页 | 论坛 | 博客
  • 博客访问: 556145
  • 博文数量: 127
  • 博客积分: 1169
  • 博客等级: 少尉
  • 技术积分: 1298
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-16 14:29
个人简介

空白

文章分类

全部博文(127)

分类: C/C++

2012-03-07 16:31:52

  1. /*
  2.     时间:2012年3月7日 16:29:19
  3.     目的:学习数据结构,学习c语言
  4. */
  5. #include <stdio.h>
  6. #include <stdlib.h>

  7. #define TRUE 1
  8. #define FALSE 0
  9. #define OK 1
  10. #define ERROR 0
  11. #define OVERFLOW -2

  12. typedef int Status;

  13. #define LIST_INIT_SIZE 10
  14. #define LISTINCREMENT 4

  15. typedef int ElemType;

  16. typedef struct SqList
  17. {
  18.     ElemType * elem;
  19.     int length;        //表的初始长度
  20.     int listsize; //表的初始存储容量
  21. }SqList;

  22. Status InitList_Sq(SqList *L); /*初始化建立线性表*/

  23. Status IsFull_Sq(SqList *L);    /*判断线性表是否满*/

  24. Status IsEmpty_Sq(SqList *L);    /*判断线性表是否为空*/

  25. void DestoryList_Sq(SqList *L); /*销毁线性表*/

  26. void ClearList_Sq(SqList *L);    /*置空线性表*/

  27. int ListLength_Sq(SqList *L);    /*获取线性表长度*/

  28. Status ListInsert_Sq(SqList *L, int i, ElemType e); /*在表L中第i个位置插入一个元素,元素值为e*/

  29. Status ListDelete_Sq(SqList *L, int i, ElemType *e); /*删除线性表中第i个位置元素*/

  30. Status GetElem_Sq(SqList *L, int i, ElemType *e);     /*获取线性表L中第i个位置的值*/

  31. /*若cur_e是L的数据元素,且不是第一个,则用pre_e返回它的前驱,否则操作失败*/
  32. Status PriorElem(SqList *L, ElemType cur_e, ElemType *pre_e);

  33. /*若cur_e是L的数据元素,且不是最后一个,则用next_e返回它的后继,否则操作失败*/
  34. Status NextElem(SqList *L, ElemType cur_e, ElemType *next_e);

  35. /*遍历输出顺序表中所有元素*/
  36. Status ListTransver_Sq(SqList *L);

  37. /*对顺序表中元素进行排序*/
  38. void ListSort_Sq(SqList *L);

  39. /*倒置顺序表中所有元素*/
  40. void ListInvert_Sq(SqList *L);

  41. Status InitList_Sq(SqList *L)
  42. {
  43.     L->elem = (ElemType *)malloc(LIST_INIT_SIZE * sizeof(ElemType));
  44.     if (NULL == L->elem)
  45.     {
  46.         printf("动态内存分配失败!\n");
  47.         return OVERFLOW;
  48.     }
  49.     L->length = 0;                    //表初始长度为0
  50.     L->listsize = LIST_INIT_SIZE;    //表的初始容量为LIST_INIT_SIZE
  51.     return OK;
  52. }

  53. /*销毁线性表*/
  54. void DestroyList_Sq(SqList *L)
  55. {
  56.     if(L->elem != NULL)
  57.     {
  58.         free(L->elem);
  59.         L->elem = NULL; //释放elem所指内存空间后,置空elem指针
  60.         L->length = 0;
  61.         L->listsize = 0;
  62.     }
  63. }

  64. /*置空线性表*/
  65. void ClearList_Sq(SqList *L)    
  66. {
  67.     L->length = 0;
  68. }

  69. /*获取线性表长度*/
  70. int ListLength_Sq(SqList *L)
  71. {
  72.     return L->length;
  73. }

  74. /*获取线性表L中第i个位置的值*/
  75. Status GetElem_Sq(SqList *L, int i, ElemType *e)
  76. {
  77.     //判断L是否为空
  78.     if (IsEmpty_Sq(L))
  79.     {
  80.         printf("线性表L为空\n");
  81.         return ERROR;
  82.     }
  83.     //判断i是否合法
  84.     if (i < 1 || i > L->length)
  85.     {
  86.         printf("不存在%d位置元素\n", i);
  87.         return ERROR;
  88.     }
  89.     *e = L->elem[i - 1];

  90.     return OK;
  91. }

  92. /*判断线性表是否为空*/
  93. Status IsEmpty_Sq(SqList *L)
  94. {
  95.     if (L->length == 0)
  96.         return TRUE;
  97.     else
  98.         return FALSE;
  99. }

  100. /*判断线性表是否满*/
  101. Status IsFull_Sq(SqList *L)
  102. {
  103.     if (L->length >= L->listsize)
  104.         return TRUE;
  105.     else
  106.         return FALSE;
  107. }

  108. /*若cur_e是L的数据元素,且不是第一个,则用pre_e返回它的前驱,否则操作失败*/
  109. Status PriorElem(SqList *L, ElemType cur_e, ElemType *pre_e)
  110. {
  111.     int i = 2; //从第二个元素开始
  112.     
  113.     while (i <= L->length)
  114.     {
  115.         if (L->elem[i - 1] == cur_e)
  116.         {
  117.             *pre_e = L->elem[i - 2];
  118.             return OK;
  119.         }
  120.         i++;
  121.     }

  122.     return ERROR;
  123. }

  124. /*若cur_e是L的数据元素,且不是最后一个,则用next_e返回它的后继,否则操作失败*/
  125. Status NextElem(SqList *L, ElemType cur_e, ElemType *next_e)
  126. {
  127.     int i = 0;

  128.     while (i < L->length - 1)
  129.     {    
  130.         if (L->elem[i] == cur_e)
  131.         {
  132.             *next_e = L->elem[i + 1];
  133.             return OK;
  134.         }
  135.         i++;
  136.     }
  137.     
  138.     return ERROR;
  139. }


  140. /*在表L中第i个位置插入一个元素,元素值为e*/
  141. Status ListInsert_Sq(SqList *L, int i, ElemType e)
  142. {
  143.     if (i < 1 || i > L->length + 1) //判断插入位置i是否合法
  144.         return ERROR;

  145.     if (L->length >= L->listsize) //判读存储空间是否已满
  146.     {
  147.         ElemType * newbase = (ElemType *)realloc(L->elem, (L->listsize + LISTINCREMENT) * sizeof(ElemType));
  148.         if (!newbase)
  149.             exit(OVERFLOW);
  150.         L->elem = newbase;
  151.         L->listsize = L->listsize + LISTINCREMENT;
  152.     }
  153.     ElemType *q = &(L->elem[i - 1]); //q指向第i个元素
  154.     ElemType *p;                     //p指向最后一个元素

  155.     for (p= &(L->elem[L->length - 1]); p >= q; p--) //从最后一个位置开始移动
  156.     {
  157.         *(p + 1) = *p;
  158.     }

  159.     *q = e;
  160.     L->length++;

  161.     return OK;
  162. }

  163. //删除线性表中第i个位置元素
  164. Status ListDelete_Sq(SqList *L, int i, ElemType *e)
  165. {

  166.     if (L->length == 0) //判断表是否为空
  167.     {
  168.         printf("表为空\n");
  169.         return ERROR;
  170.     }

  171.     if (i < 1 || i > L->length) //判断删除位置是否合法
  172.     {
  173.         printf("删除位置不合法\n");
  174.         return ERROR;
  175.     }
  176. /*
  177.     int j;
  178.     *e = L->elem[i - 1];
  179.     for (j = i; j < L->length; j++)
  180.     {
  181.         L->elem[j - 1] = L->elem[j];
  182.     }
  183. */

  184.     ElemType *p = &(L->elem[i - 1]); //p指向待删除的元素
  185.     *e = *p;
  186.     ElemType *q = &(L->elem[L->length - 1]);
  187.     //ElemType *q = L->elem + L->length - 1; //q指向最后一个元素
  188.     for (p = p + 1; p <= q; p++) //p从待删除的后一个元素开始,直到最后一个元素,每个元素一次向前移动一个位置
  189.     {
  190.         *(p - 1) = *p;
  191.     }

  192.     L->length--; //最后,线性表长度减一

  193.     return OK;
  194. }

  195. /*在表尾增加元素*/
  196. Status ListAppend_Sq(SqList *L, ElemType e)
  197. {
  198.     if (L->length >= L->listsize) //当前表已经满了,重新分配LISTINCREMENT个空间
  199.     {
  200.         ElemType *newbase = (ElemType *)realloc(L->elem, (L->length + LISTINCREMENT) * sizeof(ElemType));
  201.         if (!newbase)
  202.             return ERROR;
  203.         L->elem = newbase; //L->elem指向新分配的内存开始地址
  204.         L->listsize += LISTINCREMENT; //表的容量增加LISTINCREMENT
  205.     }

  206.     L->elem[L->length] = e;
  207.     L->length++;

  208.     return OK;
  209. }

  210. /*倒置顺序表中所有元素*/
  211. void ListInvert_Sq(SqList *L)
  212. {
  213.     int i = 0, j = L->length - 1, tmp;
  214.     while (i < j)
  215.     {
  216.         tmp = L->elem[i];
  217.         L->elem[i] = L->elem[j];
  218.         L->elem[j] = tmp;
  219.         i++;
  220.         j--;
  221.     }
  222. }

  223. /*对顺序表中元素进行排序*/
  224. void ListSort_Sq(SqList *L)
  225. {
  226.     int i, j, tmp, min;
  227.     for (i = 0; i < L->length - 1; i++)
  228.     {
  229.         min = i;
  230.         for (j = i; j < L->length; j++)
  231.         {
  232.             if (L->elem[j] < L->elem[min])
  233.                 min = j;
  234.         }
  235.         tmp = L->elem[i];
  236.         L->elem[i] = L->elem[min];
  237.         L->elem[min] = tmp;
  238.     }
  239. }

  240. /*遍历输出顺序表中所有元素*/
  241. Status ListTransver_Sq(SqList *L)
  242. {
  243.     int i;
  244.     if (L->length == 0)
  245.     {
  246.         printf("表为空\n");
  247.         return ERROR;
  248.     }
  249.     else
  250.     {
  251.         for (i = 0; i < L->length; i++)
  252.         {
  253.             printf("%d ", L->elem[i]);    
  254.         }
  255.         printf("\n");
  256.     }
  257.     return OK;
  258. }

  259. int main()
  260. {
  261.     SqList L;
  262.     ElemType e;

  263.     if(InitList_Sq(&L))
  264.     {
  265.         printf("--->顺序表初始化成功\n");
  266.     }
  267.     else
  268.     {
  269.         printf("--->顺序表初始化失败,程序退出\n");
  270.         return ERROR;
  271.     }

  272.     printf("--->顺序表是否为空? \n");
  273.     if (IsEmpty_Sq(&L))
  274.         printf("为空\n");
  275.     else
  276.         printf("非空\n");
  277.     printf("长度为: %d\n", ListLength_Sq(&L));

  278.     
  279.     printf("--->在顺序表中插入元素: ");
  280.     int i;
  281.     for (i = 1; i <= 5; i++)
  282.         ListInsert_Sq(&L, i, i * 2);
  283.     ListTransver_Sq(&L);
  284.     printf("长度为: %d\n", ListLength_Sq(&L));

  285.     printf("--->在顺序表末尾添加元素: ");
  286.     ListAppend_Sq(&L, 9);
  287.     ListAppend_Sq(&L, 3);
  288.     ListAppend_Sq(&L, 6);
  289.     ListAppend_Sq(&L, 15);
  290.     ListTransver_Sq(&L);
  291.     printf("长度为: %d\n", ListLength_Sq(&L));

  292.     printf("--->查找元素的前驱:\n");
  293.     printf("输入要查找的值: ");
  294.     scanf("%d", &i);
  295.     if (PriorElem(&L, i, &e))
  296.     {
  297.         printf("存在前驱,前驱值为: %d\n", e);
  298.     }
  299.     else
  300.     {
  301.         printf("不存在前驱\n");
  302.     }

  303.     printf("--->查找元素的后继:\n");
  304.     printf("输入要查找的值: ");
  305.     scanf("%d", &i);
  306.     if (NextElem(&L, i, &e))
  307.     {
  308.         printf("存在后继,后继值为: %d\n", e);
  309.     }
  310.     else
  311.     {
  312.         printf("不存在后继\n");
  313.     }

  314.     printf("--->删除顺序表中某个位置的元素: \n");
  315.     printf("输入位置<1 ~ %d>: ", L.length);
  316.     scanf("%d", &i);
  317.     printf("删除前: ");
  318.     printf("长度为: %d\n", ListLength_Sq(&L));
  319.     ListTransver_Sq(&L);
  320.     ListDelete_Sq(&L, i, &e);
  321.     printf("删除后: ");
  322.     printf("长度为: %d\n", ListLength_Sq(&L));
  323.     ListTransver_Sq(&L);

  324.     printf("--->倒置顺序表中的所有元素: \n");
  325.     printf("倒置前: ");
  326.     ListTransver_Sq(&L);
  327.     printf("倒置后: ");
  328.     ListInvert_Sq(&L);
  329.     ListTransver_Sq(&L);

  330.     printf("--->对顺序表中的元素进行排序: \n");
  331.     printf("排序前: ");
  332.     ListTransver_Sq(&L);
  333.     printf("排序后: ");
  334.     ListSort_Sq(&L);
  335.     ListTransver_Sq(&L);

  336.     printf("释放线性表\n");
  337.     DestroyList_Sq(&L);

  338.     return 0;
  339. }
vc++6.0输出结果:
  1. --->顺序表初始化成功
  2. --->顺序表是否为空?
  3. 为空
  4. 长度为: 0
  5. --->在顺序表中插入元素: 2 4 6 8 10
  6. 长度为: 5
  7. --->在顺序表末尾添加元素: 2 4 6 8 10 9 3 6 15
  8. 长度为: 9
  9. --->查找元素的前驱:
  10. 输入要查找的值: 8
  11. 存在前驱,前驱值为: 6
  12. --->查找元素的后继:
  13. 输入要查找的值: 8
  14. 存在后继,后继值为: 10
  15. --->删除顺序表中某个位置的元素:
  16. 输入位置<1 ~ 9>: 4
  17. 删除前: 长度为: 9
  18. 2 4 6 8 10 9 3 6 15
  19. 删除后: 长度为: 8
  20. 2 4 6 10 9 3 6 15
  21. --->倒置顺序表中的所有元素:
  22. 倒置前: 2 4 6 10 9 3 6 15
  23. 倒置后: 15 6 3 9 10 6 4 2
  24. --->对顺序表中的元素进行排序:
  25. 排序前: 15 6 3 9 10 6 4 2
  26. 排序后: 2 3 4 6 6 9 10 15
  27. 释放线性表





阅读(1551) | 评论(0) | 转发(1) |
0

上一篇:单链表实现(c语言)

下一篇:指针&数组

给主人留下些什么吧!~~