Chinaunix首页 | 论坛 | 博客
  • 博客访问: 152708
  • 博文数量: 50
  • 博客积分: 83
  • 博客等级: 民兵
  • 技术积分: 297
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-12 11:47
文章分类
文章存档

2012年(43)

2011年(7)

分类:

2012-03-16 21:12:09

原文地址:数据结构系列(一) 作者:高傲的活着

线性表的顺序存储结构
list.h

点击(此处)折叠或打开

  1. #ifndef _LIST_H
  2. #define _LIST_H

  3. #define LIST_INIT_SIZE 2      //线性表初始化存储空间大小
  4. #define LIST_INCREME 2        //线性表存储空间递增大小 

  5. typedef struct{
  6.  ElemType * elem;             //线性表首地址
  7.  int length;                   //线性表当前使用长度
  8.  int size;                    //线性表当前最大长度
  9. }LIST;

  10.  LIST *InitList();
  11.  void FreeList(LIST *l);
  12.  int InsertList(LIST *l,int i,ElemType *m);
  13.  int DeleteList(LIST *l,int i);
  14. #endif

点击(此处)折叠或打开

  1. stu.h
  2. #ifndef _STU_H
  3. #define _STU_H

  4. typedef struct
  5. {
  6.   char sno[5];
  7.   char name[21];
  8.   char ***[3];
  9.   int score;
  10. }ElemType;

  11. #endif
list.c

点击(此处)折叠或打开

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

  5. LIST * InitList()
  6. {
  7.    LIST *l=(LIST *)malloc(sizeof(LIST));
  8.    if(l==NULL)
  9.        exit(0);
  10.    l->elem=(ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType));
  11.    if(l->elem==NULL)
  12.    {
  13.        free(l);
  14.      exit(0);
  15.    }
  16.    l->length=0;
  17.    l->size=LIST_INIT_SIZE;
  18.    return l;
  19. }

  20. void FreeList(LIST *l)
  21. {
  22.   free(l->elem);     //先释放成员里面的空间,最后释放自己
  23.   free(l);
  24. }

  25. int InsertList(LIST *l,int i,ElemType *e)
  26. {
  27.     ElemType *p=NULL,*q=NULL,*newElem=NULL;
  28.    if(l==NULL||e==NULL)
  29.       return 0;
  30.    if(i<1||i>l->length+1)
  31.      return 0;
  32.     if(l->length>=l->size)   //如果它的有效长度大于它的最大长度的话,要扩充了
  33.     {
  34.          newElem=realloc(l->elem,(l->size+LIST_INCREME)*sizeof(ElemType));
  35.          if(newElem==NULL)
  36.              return 0;
  37.          l->elem=newElem;
  38.          l->size+=LIST_INCREME;
  39.     }

  40.     q=&l->elem[i-1];
  41.     for(p=&(l->elem[l->length-1]);p>=q;p--)
  42.         *(p+1)=*p;
  43.      *q=*e;
  44.      ++l->length;

  45.      return 1;
  46. }

  47. int DeleteList(LIST *l,int i)
  48. {
  49.   ElemType *p=NULL,*q=NULL;
  50.   if(l==NULL)
  51.      return 0;
  52.   if(i<1||i>l->length)
  53.      return 0;
  54.    p=&l->elem[i-1];
  55.    q=&l->elem[l->length-1];
  56.    
  57.    for(;p<q;p++)
  58.      *p=*(p+1);
  59.    --l->length;
  60.    return 1;
  61. }
main.c

点击(此处)折叠或打开

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

  4. ElemType stu[3]={
  5.     {"S101","张三","男",80},
  6.     {"S102","小红","女",75},
  7.     {"S103","王五","男",90}
  8. };
  9. void main()
  10. {
  11.     int i;
  12.    LIST *list=NULL;
  13.    list=InitList();
  14.     
  15.     for(i=0;i<3;i++)
  16.         InsertList(list,1,&stu[i]);
  17.     // DeleteList(list,2);
  18.     FreeList(list);
  19. }
学习心得:
ElemType是实现数据元素类型,根据使用进行定义.
堆区的内存要自己去释放
内存情况: (头插法)
 
阅读(606) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~