线性表的顺序存储结构(C语言实现)

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 
  4 #define OK 1
  5 #define ERR 2
  6 #define TRUE 1
  7 #define FALSE 0
  8 #define MAXSIZE 20 //定义线性表的最大长度
  9 
 10 typedef int status; //定义函数返回的状态,OK & ERR
 11 typedef char datatype; //定义线性表中每个结点的数据类型,这里暂定为字符型
 12 
 13 typedef struct {
 14     datatype data[MAXSIZE]; //存储着线性表中的每个结点
 15     int length; //线性表当前的长度
 16 } SequenceList;
 17 
 18 /* 函数原型,线性表的基本操作 */
 19 SequenceList *createSequenceList(void);
 20 status isEmpty(SequenceList *L);
 21 void clear(SequenceList *L);
 22 int getLength(SequenceList *L);
 23 int locateNode(SequenceList *L,datatype node_to_locate);
 24 datatype getNode(SequenceList *L, int index);
 25 status insert(SequenceList *L, int index, datatype node_to_insert);
 26 status delete(SequenceList *L, int index);
 27 void showList(SequenceList *L);
 28 
 29 int main(){
 30     /* 测试 */
 31     SequenceList *root; //指向线性表
 32     root=createSequenceList(); //创建一个线性表
 33     printf("Length = %d\n",getLength(root)); //打印线性表的当前长度
 34     printf("isEmpty = %d\n",isEmpty(root)); //打印线性表是否为空
 35     insert(root,0,'A'); //分别插入4个结点
 36     insert(root,0,'B');
 37     insert(root,1,'C');
 38     insert(root,1,'D');
 39     printf("Length = %d\n",getLength(root));
 40     printf("isEmpty = %d\n",isEmpty(root));
 41     showList(root); //打印线性表
 42     putchar('\n');
 43     delete(root,1); //删除index=1(数组下标为1)的结点
 44     showList(root);
 45     putchar('\n');
 46     printf("Locate = %d\n",locateNode(root,'A')); //打印查找到的结点的位置
 47     printf("getNode = %c\n",getNode(root,1)); //打印下标是1的结点的值
 48     clear(root); //清空线性表
 49     printf("isEmpty = %d",isEmpty(root));
 50 
 51     return 0;
 52 }
 53 
 54 SequenceList *createSequenceList(void){
 55     SequenceList *tmp;
 56     tmp=malloc(sizeof(SequenceList));//void*类型指针能自动转为其他类型的指针
 57     tmp->length=0; //初始化线性表长度
 58     return tmp;
 59 }
 60 status isEmpty(SequenceList *L){
 61     if (L->length==0)
 62         return TRUE;
 63     else
 64         return FALSE;
 65 }
 66 void clear(SequenceList *L){
 67     L->length=0;
 68 }
 69 int getLength(SequenceList *L){
 70     return L->length;
 71 }
 72 int locateNode(SequenceList *L, datatype node_to_locate){
 73     //返回找到的结点的index
 74     //node_to_locate应当是能唯一标识一个结点的数据,否则只返回匹配的第一个结点
 75     int i;
 76     for (i=0; i<L->length; i++){
 77         if (L->data[i]==node_to_locate)
 78             return i;
 79     }
 80     return -1; //未找到任何匹配
 81 }
 82 datatype getNode(SequenceList *L, int index){
 83     //index表示线性表中第N个结点,头结点的index是0
 84     if (L->length==0 || index<0 || index>L->length-1) return (datatype)ERR;
 85     return L->data[index];
 86 }
 87 status insert(SequenceList *L, int index, datatype node_to_insert){
 88     //node_to_insert表示想要插入的结点
 89     //当列表为空时,只有index=0才能插入
 90     int k;
 91     if (L->length == MAXSIZE) return ERR; //线性表已满
 92     if (index<0 || index>L->length) return ERR; //index不在有效范围
 93     if (index<L->length){
 94         //插入的位置不是最后一个结点的下一个结点
 95         for (k=L->length-1; k>=index; k--){
 96             L->data[k+1]=L->data[k]; //将要插入结点后面的所有结点都往后移
 97         }
 98     }
 99     L->data[index]=node_to_insert; //将新结点插入
100     L->length++;
101     return OK;
102 }
103 status delete(SequenceList *L, int index){
104     int k;
105     if (L->length == 0) return ERR; //线性表为空
106     if (index<0 || index>L->length-1) return ERR; //index不在有效范围
107     if (index<L->length-1){
108         //删除的位置不是最后一个结点
109         for (k=index; k<L->length-1; k++){
110                 L->data[k]=L->data[k+1]; //将删除位置后面的结点都往前移
111         }
112     }
113     L->length--;
114     return OK;
115 }
116 void showList(SequenceList *L){
117     int i;
118     for (i=0; i<L->length; i++){
119         printf("%c\t",L->data[i]);
120     }
121 }
122 
123 /*
124     顺序存储结构的线性表的优缺点:
125     优点:
126         1.不必为每个结点之间的逻辑关系增加额外的存储空间
127         2.可以快速地读和写表中任意一个结点
128     缺点:
129         1.插入和删除需要移动大量结点
130         2.线性表动态变化较大,难以确定所需的存储空间
131         3.数组预设过长会造成空间浪费(存储碎片)
132 */
133 /* 环境: Code::Blocks with GCC 5.1 */

运行截图:

猜你喜欢

转载自www.cnblogs.com/ryzz/p/12209859.html