數據結構之順序表

         好不容易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文件。共同實現順序表的各種操作,時間不多了,要下課了,只能輕描淡寫的爲大家寫到這裏,大家可以把上面的程序仔細的研究一下,希望能有能好學習順序表爲下面的鏈表打下基礎。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章