大话数据结构之顺序表

走上数据结构与算法前的开胃菜:

如何学习数据结构与算法

史上最全的国嵌视频笔记链接

一些需要了解的算法

数据结构与算法动态可视化

书籍:大话数据结构

顺序表代码:

1、Seqlist.c
  1 /************************************************************************************ 
  2 文件名:Seqlist.c
  3 头文件:Seqlist.h 
  4 功能:可以复用 带有增 删 改 查 功能的顺序表
  5 难点:1.顺序表中存放的都是 各种数据的地址
  6       2.void *是用来隔离封装用的 保证顺序表结构体只能被特定的函数改变 
  7 ************************************************************************************/
  8 #include <stdio.h>
  9 #include <malloc.h>
 10 #include "Seqlist.h"
 11  
 12 typedef unsigned int TSeqListNode;//这个顺序表中存放的是 各种数据的地址 所以用unsigned int 
 13 typedef struct str_SeqList
 14 {
 15     int length;//顺序已用的长度 
 16     int capacity;//顺序表的总容量 
 17     TSeqListNode* node;//这个指针是用来在顺序表中游走读取数据用的 
 18 }TSeqList;  //定义描述顺序表的结构体 
 19  
 20 /************************************************************************************ 
 21 函数名:   Creat_SeqList
 22 函数功能: 创建一个容量为capacity的顺序表 
 23 参数:     int capacity 创建顺序表中成员的个数 即顺序表容量
 24 返回值:   void* ret 如果返回NULL 说明创建顺序表失败
 25                      如果返回ret 说明创建顺序表成功,且ret为描述顺序表的结构体 
 26 ************************************************************************************/
 27 SeqList* Creat_SeqList(int capacity)
 28 {
 29     TSeqList* ret = NULL;
 30     /*进入函数 第一点是先判断传人参数的合法性*/
 31     if(capacity >= 0)
 32     {
 33         /*给顺序表开辟空间*/
 34         ret=(TSeqList* )malloc(sizeof(TSeqList)+sizeof(TSeqListNode)*capacity);
 35         if(NULL!=ret)//空间开辟成功给描述顺序表的结构体赋值 
 36         {
 37             ret->capacity=capacity;
 38             ret->length=0;
 39             ret->node=(TSeqListNode* )(ret+1);//把真正顺序表的地址赋给 node 
 40         }
 41     }
 42     else
 43     {
 44         ret = NULL;
 45     } 
 46     return (SeqList*)(ret);
 47 } 
 48  
 49 /************************************************************************************ 
 50 函数名:   Destroy_SeqList
 51 函数功能: 销毁顺序表   free开辟的内存 
 52 参数:     void* list 描述顺序表结构体指针
 53 返回值:   void 
 54 ************************************************************************************/
 55 void  Destroy_SeqList(SeqList* list)
 56 {
 57     free(list);
 58 }
 59  
 60 /************************************************************************************ 
 61 函数名:  Get_Seqlist_Length
 62 函数功能:获得顺序表 现在的大小
 63 函数参数:void* list 描述顺序表结构体指针
 64 函数返回值:int ret  成功返回length
 65                      失败返回-1 
 66 ************************************************************************************/
 67 int Get_Seqlist_Length(SeqList* list)
 68 {
 69     int ret;
 70     TSeqList *Tlist=(TSeqList* )list;
 71     /*函数参数合法性检测*/
 72     if(NULL != Tlist)
 73     {
 74         ret=Tlist->length;
 75     } 
 76     else
 77         ret=-1;
 78     return ret;
 79 }
 80  
 81 /************************************************************************************
 82 函数名:  Get_Seqlist_Capacity
 83 函数功能:获得顺序表 的容量 
 84 函数参数:void* list 描述顺序表结构体指针
 85 函数返回值:int ret  成功返回capacity 
 86                      失败返回-1 
 87 ************************************************************************************/
 88 int Get_Seqlist_Capacity(SeqList* list)
 89 {
 90     int ret;
 91     TSeqList *Tlist=(TSeqList* )list;
 92     /*函数参数合法性检测*/
 93     if(NULL != Tlist)
 94     {
 95         ret = Tlist->capacity;
 96     } 
 97     else
 98         ret=-1;
 99     return ret;
100 }
101  
102 /************************************************************************************ 
103 函数名:  Clean_Seqlist_Length
104 函数功能:清空顺序表  其实就是给length=0; 
105 函数参数:void* list 描述顺序表结构体指针
106 函数返回值:int ret  成功返回0
107                      失败返回-1 
108 ************************************************************************************/
109 int Clean_Seqlist_Length(SeqList* list)
110 {
111     int ret;
112     TSeqList *Tlist=(TSeqList* )list;
113     /*函数参数合法性检测*/
114     if(NULL != Tlist)
115     {
116         Tlist->length=0;
117         ret=0;
118     } 
119     else
120         ret=-1;
121     return ret;
122 }
123  
124 /************************************************************************************
125 函数名:  Seqlist_Add
126 函数功能:顺序表中有length个数据  在下标为pos的位置上 插入数据node  所以pos是从0开始的 length是从1开始的 
127 参数:      SeqList* list描述顺序表的结构体地址   SeqListNode* node插入顺序表的数据的地址  
128            int pos插入顺序表的位置   pos的范围是从0(此时在顺序表头部插入)开始  到length(此时就是在顺序尾部插入)
129             总共是length+1个位置 
130 返回值 :  返回1 说明插入数据成功  返回0 说明插入数据失败
131 ************************************************************************************/
132 int Seqlist_Add(SeqList* list, SeqListNode* node ,int pos)
133 {
134     /*参数合法性检测*/
135     TSeqList *Tlist=(TSeqList* )list;
136     int ret = (NULL != list);
137     int i;
138     ret=ret && (pos >= 0);
139     ret=ret && (Tlist->length+1 <= Tlist->capacity);  //判断再插入一个数据的时候  length有没有超过 capacity 
140     if(1 == ret)
141     {
142         if(pos >= Tlist->length)//如果插入的位置pos比 length大的话 默认把length+1赋值给pos 
143         {
144             pos = Tlist->length;
145         }
146         for(i=Tlist->length;i>pos;i--)
147         {
148             Tlist->node[i]=Tlist->node[i-1];
149         } 
150         Tlist->node[i]=(TSeqListNode)node; //把要插入的地址强制类型转换成 unsigned int* 
151         Tlist->length++;
152     } 
153     return ret;//返回1 说明插入数据成功  返回0 说明插入数据失败 
154 }    
155  
156  
157 /************************************************************************************
158 函数名:   Get_Node
159 函数功能:找到顺序表中下标为pos的值  
160 参数:    pos插入顺序表的下标   pos的范围是从0到length-1   
161           SeqList* list描述顺序表的结构体地址
162 返回值:  void* ret 找到pos为下标的那个值
163         如果成功返回pos为下标的那个值   如果失败  返回NULL
164 ************************************************************************************/
165  
166 SeqListNode* Get_Node(SeqList* list, int pos)
167 {
168     TSeqList* Tlist=(TSeqList* )list;
169     SeqListNode* ret=NULL;
170     if( (NULL!=Tlist) && (pos>=0) && (pos<Tlist->length) )
171     {
172         ret=(SeqListNode* )Tlist->node[pos]; //强制类型转换成void*  
173     }
174     return ret;
175 } 
176  
177 /************************************************************************************
178 函数名:   Del_Node
179 函数功能:找到顺序表中下标为pos的值  并且删除它 
180 参数:    删除pos为下标的值   pos的范围是从0到length-1   
181           SeqList* list描述顺序表的结构体地址
182 返回值:  void* ret 
183           如果成功返回pos为下标的那个值   如果失败  返回NULL 
184 ************************************************************************************/
185 SeqListNode* Del_Node(SeqList* list, int pos)
186 {
187     TSeqList* Tlist=(TSeqList* )list;
188     SeqListNode* ret=NULL;
189     int i;
190     if( (NULL!=Tlist) && (pos>=0) && (pos<Tlist->length) )
191     {
192         ret=(SeqListNode* )Tlist->node[pos];
193         for(i=pos+1; i<Tlist->length; i++)
194         {
195             Tlist->node[i-1]=Tlist->node[i];
196         }
197         Tlist->length--;
198     }
199     return ret;
200 }

 

2、SeqList.h

 1 #ifndef __Seqlist__
 2 #define __Seqlist__
 3  
 4 typedef void SeqList;  //是用来封装 使顺序表结构体 不被外界改变 只可被Seqlist.c文件中的函数改变
 5                        //因为 这些函数 对外的接口 都是void*  
 6 typedef void SeqListNode;//SeqList 是用来表示 顺序表的    SeqListNode是用来表示顺序表 中变量的 
 7  
 8 SeqList* Creat_SeqList(int capacity);
 9 void  Destroy_SeqList(SeqList* list);
10 int Get_Seqlist_Length(SeqList* list);
11 int Get_Seqlist_Capacity(SeqList* list);
12 int Clean_Seqlist_Length(SeqList* list);
13 int Seqlist_Add(SeqList* list, SeqListNode* node ,int pos);
14 SeqListNode* Get_Node(SeqList* list, int pos);
15 SeqListNode* Del_Node(SeqList* list, int pos); 
16  
17 #endif

 

3、main.c

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include "Seqlist.h"
 4 int main(int argc, char *argv[])
 5 {
 6     SeqList* My_SeqList = NULL;
 7     int a = 10;
 8     int b = 5;
 9     int c = 3;
10     int d = 6;
11     int e = 1;
12     int *p = NULL;
13     int i = 0;
14     My_SeqList = Creat_SeqList(5);
15     if( NULL != My_SeqList )
16     {
17             Seqlist_Add(My_SeqList, &a ,0);
18             Seqlist_Add(My_SeqList, &b ,0);
19             Seqlist_Add(My_SeqList, &c ,0);
20             Seqlist_Add(My_SeqList, &d ,0);
21             Seqlist_Add(My_SeqList, &e ,0);
22             
23             for(i=0; i<Get_Seqlist_Length(My_SeqList); i++)
24             {
25                 p=Get_Node(My_SeqList, i);
26                 printf("%d\n",*p);
27             }
28             
29             Del_Node(My_SeqList, 3);
30             for(i=0; i<Get_Seqlist_Length(My_SeqList); i++)
31             {
32                 p=Get_Node(My_SeqList, i);
33                 printf("%d\n",*p);
34             }
35             
36     } 
37     Clean_Seqlist_Length(My_SeqList);
38     Destroy_SeqList(My_SeqList);
39     return 0;
40 }

 

4、学生信息管理:

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <malloc.h>
  4 #include "Seqlist.h"
  5  
  6 typedef struct student
  7 {
  8     int student_num;
  9     char name[30];
 10     char sex[20];
 11     int age;
 12 }str;
 13 int main()
 14 {
 15     str* str1;
 16     SeqList* slist=NULL;
 17     int i=0;
 18     int age=0;
 19     slist=Creat_SeqList(50);
 20     if(NULL == slist)
 21     {
 22         printf("malloc error!!!\n");
 23         return -1;
 24     }
 25     for(i=0; i<3; i++)
 26     {
 27         put_student(slist, str1);
 28     }
 29     
 30     printf("输入你要删除的年龄:\n");
 31     scanf("%d",&age);
 32     printf("\n");
 33     find_student(slist, str1, age);
 34     get_student(slist, str1);
 35     
 36     destroy_student(slist, str1);
 37     Clean_Seqlist_Length(slist);
 38     Destroy_SeqList(slist);
 39     return 0;
 40 }
 41  
 42 int put_student(SeqList* slist, str* str1)
 43 { 
 44     int num;
 45     int ret=(NULL != str1);
 46     if(1 == ret)
 47     {
 48         ret=ret && Seqlist_Add(slist, (str* )malloc(sizeof(str)*1) ,50);
 49         num = Get_Seqlist_Length(slist);
 50         str1 = (str* )Get_Node(slist, num-1);
 51         printf("请输入学生学号:\n"); 
 52         scanf("%d",&str1->student_num);
 53         printf("请输入学生姓名:\n");
 54         scanf("%s",str1->name);
 55         printf("请输入学生性别:\n");
 56         scanf("%s",str1->sex);
 57         printf("请输入学生年龄:\n");
 58         scanf("%d",&str1->age);
 59         printf("\n"); 
 60     }        
 61     else
 62     {
 63         ret = 0;
 64     }
 65     return ret;
 66 }
 67  
 68 int get_student(SeqList* slist, str* str1)
 69 {
 70     int ret=(NULL != str1);
 71     int i=0;
 72     if(1 == ret)
 73     {
 74         for(i=0; i<Get_Seqlist_Length(slist); i++)
 75         {
 76             str1 = (str*)Get_Node(slist, i);
 77             printf("学生学号:%d\n",str1->student_num);
 78         
 79             printf("学生姓名:%s\n",str1->name);
 80             
 81             printf("学生性别:%s\n",str1->sex);
 82             
 83             printf("学生年龄:%d\n",str1->age);
 84         }
 85     }        
 86     else
 87     {
 88         ret = 0;
 89     }
 90     return ret;
 91 }
 92  
 93 int destroy_student(SeqList* slist, str* str1)
 94 {
 95     int ret=(NULL != str1);
 96     int i=0;
 97     if(1 == ret)
 98     {
 99         for(i=0; i<Get_Seqlist_Length(slist); i++)
100         {
101             str1 = (str*)Get_Node(slist, i);
102             free(str1);
103         }
104     }        
105     else
106     {
107         ret = 0;
108     }
109     return ret;
110 }
111  
112 int find_student(SeqList* slist, str* str1, int age)
113 {
114     int ret=(NULL != str1);
115     int i=0;
116     int num=0;
117     if(1 == ret)
118     {
119         num=Get_Seqlist_Length(slist);
120         for(i=0; i<num; i++)
121         {
122             str1 = (str*)Get_Node(slist, i);
123             if(str1->age == age)
124             {
125                 Del_Node(slist, i);
126                 num=Get_Seqlist_Length(slist);
127                 i--;
128             }
129         }
130     }        
131     else
132     {
133         ret = 0;
134     }
135     return ret;
136 }

 

posted @ 2019-06-09 17:22  千小塔  阅读(196)  评论(0编辑  收藏  举报