zoukankan      html  css  js  c++  java
  • 数据结构(c语言实现)--线性表

    下面是线性表的一些基本操作的实现:

    seqlist.h:  
    1
    #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 #define LIST_SIZE 100 5 #define LIST_INCREMENT 10 6 typedef int Datatype ; 7 typedef struct{ 8 Datatype* data; 9 int length; 10 int listsize; 11 }seqlist; 12 13 /*InitList(&L) 14 初始条件:无 15 操作结果:构造一个空的线性表。成功返回0出错返回-1*/ 16 17 int InitList(seqlist *L) 18 { 19 L->data = (Datatype *)malloc(sizeof(Datatype)*LIST_SIZE); 20 if (L->data == NULL) 21 return -1; 22 L->length = 0; 23 L->listsize = LIST_SIZE; 24 printf("initial finish "); 25 return 0; 26 } 27 28 /*DestroyList(&L) 29 初始条件:线性表L已经存在 30 操作结果:销毁线性表L。成功返回0出错返回-1*/ 31 32 int DestroyList(seqlist *L) 33 { 34 while (!L->data) 35 free(L->data); 36 L->length = 0; 37 printf("destroy finish "); 38 return 0; 39 40 } 41 42 /*ListEmpty(L) 43 初始条件:线性表L已经存在 44 操作结果:若L为空表,返回0,否则返回-1*/ 45 46 int ListEmpty(seqlist L) 47 { 48 if (L.length == 0) 49 { 50 printf("list is empty "); 51 return 0; 52 } 53 else 54 return -1; 55 } 56 57 /*ListLength (L) 58 初始条件:线性表L已经存在 59 操作结果:返回表的长度,失败返回-1*/ 60 61 62 int ListLength(seqlist L) 63 { 64 65 return L.length; 66 } 67 68 /*Getelem(L,i,&e) 69 初始条件:线性表L已经存在1=<i<=LIST_SIZE 70 操作结果:用e返回L中第i个元素的值成功返回0出错返回-1*/ 71 int Getelem(seqlist L, int i, Datatype *e) 72 { 73 if (i < 0 || i >LIST_SIZE) 74 { 75 printf("position error "); 76 return -1; 77 } 78 *e = L.data[i]; 79 return 0; 80 81 } 82 /*Locateelem(L,e) 83 初始条件:线性表L已经存在 84 操作结果:返回L中第一个和e相等的序列号,若元素不存在,则返回-1*/ 85 int Locateelem(seqlist L, Datatype e) 86 { 87 88 int i;//遍历整个链表,直到结尾若没找到返回-1 89 for (i = 0; i < L.length; i++) 90 { 91 if (L.data[i] == e) 92 return i+1; 93 } 94 return -1; 95 } 96 /*Priorelem(L,cur_e,&pre_e) 97 初始条件:线性表已经存在 98 操作结果:若cur_e是L中的元素,且不是第一个,则用pre_e返回它的前驱元素,否则返回错误-1,成功返回0*/ 99 int Priorelem(seqlist L, Datatype cur_e, Datatype *pre_e) 100 { 101 int i = 0; 102 if (cur_e == L.data[0]) 103 return -1; 104 i = Locateelem(L, cur_e); 105 if (i== -1) 106 return -1; 107 else 108 { 109 *pre_e = L.data[i - 2]; 110 return 0; 111 } 112 113 } 114 /*Nextelem(L,cur_e,&next_e) 115 初始条件:线性表已经存在 116 操作结果:若cur_e是L中的元素,且不是最后一个,则用next_e返回它的前驱元素,否则返回错误-1,成功返回0*/ 117 int Nextelem(seqlist L, Datatype cur_e, Datatype *next_e) 118 { 119 int i = 0; 120 if (cur_e == L.data[ListLength(L)-1]) 121 return -1; 122 i = Locateelem(L, cur_e); 123 if (i == -1) 124 return -1; 125 else 126 { 127 *next_e = L.data[i]; 128 return 0; 129 } 130 131 } 132 /*ListInsert(&L,i,e) 133 初始条件:线性表L已经存在 134 操作结果:在L中第i个位置之前插入新的数据,表长加1,返回0成功,-1错误*/ 135 int ListInsert(seqlist *L, int i, Datatype e) 136 { 137 if (i<1 || i>L->listsize) 138 { 139 printf("position error "); 140 return -1; 141 } 142 if (L->length >= L->listsize) 143 L->data = (Datatype *)realloc(L->data, (LIST_INCREMENT + LIST_SIZE)*sizeof(Datatype)); 144 if (!L->data) 145 { 146 printf("realloc error "); 147 return -1; 148 } 149 Datatype *q=NULL; 150 q = &L->data[i - 1];//插入位置 151 Datatype *p = NULL; 152 for (p = &L->data[L->length - 1]; p >= q;p--)//从最后一个位置开始依次把前面的值赋值给后一个位置 153 { 154 *(p + 1) = *p; 155 } 156 *q = e; 157 L->length++; 158 L->listsize += LIST_INCREMENT; 159 return 0; 160 161 162 } 163 /*Listdelete(&L,i,&e) 164 初始条件:线性表L存在 165 操作结果:删除L中序号为i的元素,并将其值由e带回,成功返回0,出错返回-1*/ 166 167 int Listdelete(seqlist *L, int i, Datatype *e) 168 { 169 170 if (i<1 || i>L->listsize) 171 { 172 printf("postion error: "); 173 return -1; 174 } 175 Datatype *q = NULL; 176 Datatype *p = NULL; 177 q = &L->data[i - 1];//删除元素位置 178 *e = *q; 179 p = &L->data[L->length - 1];//表尾位置 180 while (q <= p) 181 { 182 *q = *(q + 1); 183 q++; 184 } 185 L->length--; 186 return 0; 187 }

    下面是关于上述线性表的基本操作的一些简单测试:

    main.c:
    1
    #include "seqlist.h" 2 3 int main(void) 4 { 5 seqlist L; 6 InitList(&L); 7 ; 8 int i,k=0; 9 Datatype e; 10 Datatype ee; 11 Datatype eee; 12 Datatype eeee; 13 for (i = 1; i <=LIST_SIZE+1; i++) 14 { 15 if (!ListInsert(&L, i, i*i)) 16 printf("第%d个数%d插入列表 ",i, L.data[i-1]); 17 } 18 printf("the length of list is %d ", ListLength(L)); 19 20 21 k = 5; 22 Getelem(L, k-1, &e); 23 printf("第%d个数为%d ",k, e); 24 if (ListEmpty(L)) 25 printf("list is not empty "); 26 printf("%d的序号是%d ", 25, Locateelem(L, 25)); 27 Priorelem(L, 4,&ee); 28 printf("%d的前一个元素是是%d ", 4, ee); 29 Nextelem(L, 256, &eee); 30 printf("%d的后一个元素是是%d ", 256, eee); 31 Listdelete(&L,1,&eeee); 32 printf("删除元素是%d ", eeee); 33 printf("the length of list is %d ", ListLength(L)); 34 DestroyList(&L); 35 getchar(); 36 return 0; 37 38 }
  • 相关阅读:
    天心阁漫步
    大美湘江
    easyui tree基本操作
    盗墓笔记
    半年了
    文件上传控件值发生变化后自动提交表单
    宁静的夏夜
    今天您给别人让座,将来别人给您让座
    优先队列priority_queue 用法详解
    POJ2387
  • 原文地址:https://www.cnblogs.com/zydark/p/7778131.html
Copyright © 2011-2022 走看看