码迷,mamicode.com
首页 > 其他好文 > 详细

数据结构之顺序表

时间:2015-02-06 07:07:57      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:顺序表 数据结构

         好不容易linux的课程结束了,下面就进入了数据结构的课程,对于没学过这本书的我,只能弱弱的说一句,数据结构真的好难,在学习的过程中,觉得最经典的一句话便是,数据结构+算法=程序。我只想说理解数据结构真的好难,太富有逻辑性了,有时候真的还需要发挥自己的空间想象能力,几天的学习,其实还算可以吧,下面我就给大家写一点线性表的基本操作吧,我尽可能的讲解的清楚一些,方便看的人有兴趣能有自己实现。

#include <stdio.h>

#include <stdlib.h>


#include "datatype.h"

#include "seqlist.h"


void iterate_list(seqlist_t *list)

{

int i;

printf("list.last = %d, list = {", list->last);

for (i = -1; i < list->last;) {

printf("%d,", list->data[++i]);

}

if (LengthSqlist(list) > 0)

printf("\b}\n");

else

printf("}\n");

}


int main(int argc, char *argv[])

{

int i;

data_t a[10] = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20};

data_t x;

seqlist_t *list;

list = CreateEmptySqlist();

if (NULL == list) return -1;

for (i = 0; i < 10; i++) {

if (InsertSqlist(list, i, a[i]) < 0)

break;

}

iterate_list(list);

GetSqlist(list, 4, &x);

printf("list[4] = %d\n", x);

printf("updated list[4] to 100\n");

SetSqlist(list, 4, 100);

GetSqlist(list, 4, &x);

printf("now list[4] = %d\n", x);

iterate_list(list);

printf("removed list[4]\n");

DeleteSqlist(list, 4);

GetSqlist(list, 4, &x);

printf("now list[4] = %d\n", x);

printf("and total number of list is %d\n", LengthSqlist(list));

iterate_list(list);

ClearSqlist(list);

printf("after clear, total number of list is %d\n", LengthSqlist(list));


iterate_list(list);

DestroySqlist(list);

return 0;

}



#include <stdio.h>

#include <stdlib.h>


#include "seqlist.h"


seqlist_t *CreateEmptySqlist()

{

seqlist_t *list;


list = (seqlist_t *)malloc(sizeof(seqlist_t));

if (list) {

list->last = -1;

}


return list;

}


void DestroySqlist(seqlist_t *list)

{

if (NULL != list)

free(list);

}


int FullSqlist(seqlist_t *list)

{

if (list) {

if ((MAX - 1) == list->last) {

return 1;

} else {

return 0;

}

} else {

return -1;

}

}


int EmptySqlist(seqlist_t *list)

{

if (list) {

if (-1 == list->last) {

return 1;

} else {

return 0;

}

} else {

return -1;

}

}


void ClearSqlist(seqlist_t *list)

{

if (list) {

list->last = -1;

}


return;

}


int LengthSqlist(seqlist_t *list)

{

if (list) {

return (list->last + 1);

} else {

return -1;

}

}



int GetSqlist(seqlist_t *list, int at, data_t *x)

{

if (!list) return -1;

if ((at < 0) || (at > list->last)) return -1;

if (x) {

*x = list->data[at];

}

return 0;

}


int SetSqlist(seqlist_t *list, int at, data_t x)

{

if (!list) return -1;

if ((at < 0) || (at > list->last)) return -1;

list->data[at] = x;

return 0;

}


int InsertSqlist(seqlist_t *list, int at, data_t x)

{

int i;

if (!list) return -1;


if (at < 0) {

/* at must >=0 */

return -1;

}

if (FullSqlist(list)) {

/* memory space is not sufficient */

return -2;

if (at > list->last) {

at = list->last + 1;

}

for (i = list->last; i >= at; i--) {

list->data[i + 1] = list->data[i];

}

list->data[at] = x;

list->last++;


return 0;

}


int DeleteSqlist(seqlist_t *list, int at)

{

int i;

if (!list) return -1;


if ((at < 0) || (at > list->last)) return 0;


for (i = at; i < list->last; i++) {

list->data[i] = list->data[i + 1];

}

list->last--;


return 1;

}


#ifndef _SEQ_LIST_H_

#define _SEQ_LIST_H_


#include "datatype.h"


#define MAX 100


typedef struct {

data_t data[MAX];

int last; /* pointer to the last element of the array */

} seqlist_t;


/* 

 * create a list and init it as empty 

 * Input : void

 * Output: void

 * Return: new list, NULL when failed 

 */

seqlist_t *CreateEmptySqlist();


/* 

 * destroy a list 

 * Input : the list to be destroied. 

 * Output: void

 * Return: void

 */

void DestroySqlist(seqlist_t *list);


/*

 * clear the list and reset it as empty

 * Input : the list to be cleared. 

 * Output: void

 * Return: void

 */

void ClearSqlist(seqlist_t *list);


/* 

 * judge if the list is empty

 * Input: the list to be tested. 

 * Output: void

 * Return:

 * 1: list is empty

 * 0: not 

 * -1: error, e.g. the list is invalid

 */

int EmptySqlist(seqlist_t *list);


/* 

 * judge if the list is full 

 * Input : the list to be tested. 

 * Output: void

 * Return:

 * 1 : list is full

 * 0 : not 

 * -1: error

 */

int FullSqlist(seqlist_t *list);


/* 

 * get length of the list 

 * Input : the list to be tested. 

 * Output: void

 * Return: 

 * >= 0: length of the list;

 * -1 : means error 

 */

int LengthSqlist(seqlist_t *list);


/* 

 * Insert element at the specified position. If the "at" exceed the 

 * up-range of the list, append the data at the end of the list. 

 * e.g. insert a new data into a {}.

 * Input : 

 * list : the list to be operated.

 * at : the position at which to insert the new element, 

 * position index starts from zero.

 * x : the data to be inserted 

 * Output: void

 * Return:

 * 0 : success; 

 * <0: error 

 */

int InsertSqlist(seqlist_t *list, int at, data_t x);


/*

 * delete the element by the position

 * Input : 

 * list : the list to be operated.

 * at : the position at which to delete the element, 

 * position index starts from zero

 * Output : void

 * Return :

 * 1 : success;

 * 0 : not found

 * -1: error  

 */

int DeleteSqlist(seqlist_t *list, int at);


/*

 * get data of element at specified position

 * Input : 

 * list : the list to be operated.

 * at : the position where to get the element at, 

 * position index starts from zero.

 * Output:

 * x : the data value returned

 * Return:

 * 0 : success;

 * -1: error, e.g. list is invalid; ‘at‘ extends 

 * the range of the list    

 */

int GetSqlist(seqlist_t *list, int at, data_t *x);


/*

 * set/update data of element at specified position

 * Input : 

 * list : the list to be operated.

 * at : the position at where to set the element, 

 * position index starts from zero

 * x : the new data value

 * Output: void

 * Return:

 * 0 : success;

 * -1: error, e.g. list is invalid; ‘at‘ extends the 

 * range of the list   

 */

int SetSqlist(seqlist_t *list, int at, data_t x);

#endif /* _SEQ_LIST_H_ */


#ifndef _DATA_TYPE_H_

#define _DATA_TYPE_H_


typedef int data_t;


#endif /* _DATA_TYPE_H_ */



上面的内容是四个部分,其中两个.c文件,两个.h文件。共同实现顺序表的各种操作,时间不多了,要下课了,只能轻描淡写的为大家写到这里,大家可以把上面的程序仔细的研究一下,希望能有能好学习顺序表为下面的链表打下基础。

本文出自 “9808690” 博客,请务必保留此出处http://9818690.blog.51cto.com/9808690/1612126

数据结构之顺序表

标签:顺序表 数据结构

原文地址:http://9818690.blog.51cto.com/9808690/1612126

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!