Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4264866
  • 博文数量: 1148
  • 博客积分: 25453
  • 博客等级: 上将
  • 技术积分: 11949
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-06 21:14
文章分类

全部博文(1148)

文章存档

2012年(15)

2011年(1078)

2010年(58)

分类: C/C++

2011-05-10 13:40:43

附件: m.rar  

顺序线性表:

我们采用动态分配内存 malloc 初始化 线性表

      1. 首先申请 LIST 内存,(size ,length)
 
      2.然后申请 ElemType 数据空间内存


  1. typedef struct lnode{
  2.     ElemType *elem; //指向 数据 存储地址
  3.     int length ; // 用掉的 数据空间
  4.     int size; //初始化 最大 数据空间
  5. }LIST;





    

list.h 头文件

  1. #ifndef _LIST_H
  2. #define _LIST_H

  3. #define LIST_SIZE 10        // 初始化 最大 数据空间  10
  4. #define LIST_INCREME 10    //每次 递增 申请 大小
  5. typedef char ElemType;

  6. /*
  7.     我们也可以这么定义 ElemType 定义一个学生结构体作为 变量
  8.     typedef struct stu{
  9.         char snu[5];        
  10.         char name[21];    
  11.         char ***[3];
  12.         int score;
  13. }ElemType;
  14. */

  15. typedef struct lnode{
  16.     ElemType *elem; //指向 数据 存储地址
  17.     int length ; // 用掉的 数据空间
  18.     int size; //初始化 最大 数据空间
  19. }LIST;

  20. LIST *init_list();
  21. void free_list(LIST *l);
  22. int insert_list(LIST *l,int i,ElemType *e);
  23. void print_list(LIST *l);
  24. int delete_list(LIST *l,int i);
  25. #endif


list.c 文件
  1. #include <stdio.h>
  2. #include <stdlib.h> //malloc
  3. #include "list.h"

  4. LIST *init_list()
  5. {
  6.     LIST *l;
  7.     l =(LIST *)malloc(sizeof(LIST)); //首先 开辟存放 length size 的地址空间,
  8.                                          // 和 ElemType * elem 地址
  9.         if(l == NULL) //申请失败
  10.         exit(1); //返回 1 失败

  11.     l->elem =(ElemType *)malloc(sizeof(ElemType)*LIST_SIZE); //开辟 存放 数据 空间
  12.     
  13.     if(l == NULL)
  14.         exit(1);

  15.     
  16.     l->length = 0; //初始化 最大数据空间 10     
  17.     l->size = LIST_SIZE; //初始化 0 数据空间

  18.     return l;
  19. }

  20. void free_list(LIST *l)
  21. {
  22.     free(l->elem); //首先释放 数据
  23.     free(l); //然后释放自己
  24. }

  25. int insert_list(LIST *l,int i,ElemType *e)
  26. {
  27.     ElemType *p=NULL;
  28.     ElemType *q=NULL;
  29.     ElemType *newElem=NULL;
  30.     if( l ==NULL)
  31.         return 1;
  32.     if(i < 1 || i> l->length+1) // length 开始 =0,
  33.         return 1;

  34.     if(i > l->size) //大于 最大 申请空间,我们需要 增加空间
  35.     {
  36.             //realloc 再次申请空间        
  37.         newElem = realloc(l->elem,(l->size+LIST_INCREME)*sizeof(ElemType));
  38.                             
  39.         if(newElem == NULL)
  40.             return 0;
  41.         l->elem = newElem;
  42.         l->size =l->size+LIST_INCREME;
  43.     }

  44.     q=&(l->elem[i-1]);//暂存 i-1 的数据地址,然后将 i-1 之后的数据
  45.     for(p = &(l->elem[l->length-1]);p >= q;p--) //从最后,开始移动数据
  46.     {
  47.         *(p+1) = *(p);
  48.     }

  49.     *q = *e;
  50.     ++(l->length); //使用数据空间 加 一

  51.     return 0;
  52. }

  53. int delete_list(LIST *l,int i)
  54. {
  55.     ElemType *q=NULL;
  56.     ElemType *p=NULL;
  57.     
  58.     p=&l->elem[i-1];
  59.     q=&l->elem[l->length-1];
  60.     
  61.     for(;p<q;p++)
  62.     {
  63.         *p=*(p+1);
  64.     }
  65.     --l->length;
  66.     return 0;
  67. }
  68. void print_list(LIST *l)
  69. {
  70.     int i=0;
  71.     for(i=0;i<l->length;i++)
  72.     {
  73.         printf("the nubmer %d is %c \n",i+1,l->elem[i]);
  74.     }
  75. }

main.c

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

  4. int main()
  5. {
  6.     char ch1='a';
  7.     char ch2='b';
  8.     LIST *list;
  9.     list = init_list();

  10.     insert_list(list,1,&ch1);
  11.     insert_list(list,2,&ch2);   //插入 数据
  12.     print_list(list);            //打印数据
  13.     delete_list(list,1);       //删除数据
  14.     print_list(list);        //打印数据
  15.     free(list);
  16.     
  17.     return 0;
  18. }
显示 效果
  1. ywx@yuweixian:~/yu/data-struct/itboba/seqlist/m$ ./main
  2. the nubmer 1 is a
  3. the nubmer 2 is b
  4. the nubmer 1 is b




阅读(769) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~