A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© cjfire 中级黑马   /  2014-9-25 15:17  /  1017 人查看  /  2 人回复  /   1 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. //
  2. //  main.c
  3. //  静态链表
  4. //

  5. #include <stdio.h>

  6. #define MAXSIZE 15
  7. #define OK 1
  8. #define ERROR 0
  9. #define TRUE 1
  10. #define FALSE 0

  11. typedef int Status;
  12. typedef int ElemType;
  13. typedef struct{
  14.     ElemType data;
  15.     int cur;
  16. }Component,StaticList[MAXSIZE];

  17. /*初始化*/
  18. Status InitList(StaticList space){
  19.    
  20.     for (int i = 0; i <= MAXSIZE -1; i++) {
  21.         space[i].cur = i + 1;
  22.         space[i].data = 0;
  23.     }
  24.     space[MAXSIZE - 1].cur = 0;
  25.    
  26.     return OK;
  27. }
  28. /*插入*/
  29. int InsertList(StaticList space,ElemType e){
  30.     int i = space[0].cur;
  31.     //顺序插入,第一次的首地址指向1
  32.     if (!space[MAXSIZE-1].cur) {
  33.         space[MAXSIZE-1].cur = 1;
  34.     }
  35.    
  36.     if (space[0].cur) {
  37.         space[0].cur = space[i].cur;
  38.         space[i].data = e;
  39.     }
  40.     return i;
  41. }
  42. /*demo 插入*/
  43. int Malloc_SLL(StaticList space)
  44. {
  45.     int i = space[0].cur;
  46.    
  47.     if (space[0].cur) {
  48.         space[0].cur = space[i].cur;
  49.     }
  50.    
  51.     return i;
  52. }

  53. /*指定位置上插入元素*/
  54. Status ListInsert(StaticList space,int i,ElemType e){
  55.     int j,k,l,length;
  56.    
  57.     k = MAXSIZE - 1;
  58.     length = ListLength(space);
  59.     if (i < 1 || i > length - 1) {
  60.         return ERROR;
  61.     }
  62.    
  63.     j = Malloc_SLL(space);
  64.    
  65.     if (j) {
  66.         space[j].data = e;
  67.         for (l = 1; l < i-1; l++) {

  68.             k = space[k].cur;
  69.         }
  70.         space[j].cur = space[k].cur;
  71.         space[k].cur = j;
  72.     }
  73.    
  74.     return OK;
  75. }


  76. int ListLength(StaticList list){
  77.     int length = 0;
  78.     int k = MAXSIZE - 1;
  79.     for (int i = 0; i < MAXSIZE - 1; i++) {
  80.         if (k == list[0].cur) {
  81.             break;
  82.         }
  83.         length++;
  84.         k = list[k].cur;
  85.     }
  86.    
  87.     return length;
  88. }

  89. /*print*/
  90. void PrintAllElem(StaticList space)
  91. {
  92.     int k = MAXSIZE - 1;
  93.    
  94.     for (int i = 0; i < MAXSIZE - 1; i++) {
  95.         printf("position is %d,data is %d,cru is %d\n",k,space[k].data,space[k].cur);
  96.         k = space[k].cur;
  97.     }
  98. }

  99. int main(int argc, const char * argv[])
  100. {
  101.     StaticList myspace;
  102.     InitList(myspace);
  103.    
  104.     InsertList(myspace, 11);
  105.     InsertList(myspace, 22);
  106.     InsertList(myspace, 33);
  107.     InsertList(myspace, 44);
  108.     InsertList(myspace, 55);
  109.    
  110.     ListInsert(myspace, 3, 66);
  111.     ListInsert(myspace, 3, 77);
  112.    
  113.     InsertList(myspace, 88);
  114.    
  115.     PrintAllElem(myspace);
  116.    
  117.     printf("length is %d\n",ListLength(myspace));
  118.     printf("last cur is %d\n",myspace[0].cur);
  119.     return 0;
  120. }
复制代码

ListInsert,这个函数调用完毕后,输出链表,觉得有些问题,想来是因为造成了有两个节点指向了一个节点,但是不知道怎么修正。望大神能给指出一下。

2 个回复

倒序浏览
有人回答一下么?帖子沉了?!
回复 使用道具 举报
感觉代码有点长...
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马