附:C语言数据结构-1.线性表之顺序存储结构
1. seqlist.h
<span style="font-size:14px;"><span style="font-family:Comic Sans MS;font-size:14px;">#ifndef _SEQ_LIST_H_
#define _SEQ_LIST_H_
#define MAX 16
typedef int data_t;
typedef struct
{
data_t data[MAX];
int last; /* pointer to the last element of the list */
} 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
*/
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 position
* Input :
* list: the list to be operated.
* at : the position at which to insert the new element into, start from zero
* x : the data value
* Output: void
* Return:
* 0 : success;
* -1: 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 where to delete the element, start 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, started from zero
* Output:
* x: the value returned
* Return:
* 0 : success;
* -1: error: 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, started with zero
* x : the new data value
* Output: void
* Return:
* 0 : success;
* -1: error: list is invalid; at extends the range of the list
*/
int SetSqlist(seqlist_t *list, int at, data_t x);
/*
* search the first element match the value of specified data
* and return its position
* Input :
* list: the list to be operated.
* x : the data value to be compared when search
* Output: void
* Return:
* >=0 : the position of the element found;
* -1 : not found or error
*/
int SearchSqlist(seqlist_t *list, data_t x);
/*
* iterate through the list and print out info of each element
* Input :
* list: the list to be operated.
* Output: void
* Return: void
*/
void VisitSqlist(seqlist_t *list);
#endif /* _SEQ_LIST_H_ */</span></span>
2. seqlist.c
<span style="font-size:14px;"><span style="font-family:Comic Sans MS;font-size:14px;">#include <stdio.h>
#include <stdlib.h>
#include "seqlist.h"
seqlist_t *CreateEmptySqlist()
{
seqlist_t *list;
list = (seqlist_t *)malloc(sizeof(seqlist_t));
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;
}
}
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 InsertSqlist(seqlist_t *list, int at, data_t x)
{
int i;
if (!list) return -1;
if (FullSqlist(list) ||
(at < 0) ||
(at > (list->last + 1))) {
return -1;
}
for (i = list->last; i >= at; i--) {
list->data[i + 1] = list->data[i];
}
list->data[at] = x;
list->last++;
return 0;
}
void VisitSqlist(seqlist_t *list)
{
int i;
printf("list.last = %d, list = {", list->last);
for (i = -1; i < list->last;) {
printf("%d,", list->data[++i]);
}
if (i > -1)
printf("\b}\n");
else
printf("}\n");
}
int SearchSqlist(seqlist_t *list, data_t x)
{
int i;
if (!list) return -1;
for (i = 0; i <= list->last; i++) {
if (list->data[i] == x) return i;
}
return -1;
}
int EmptySqlist(seqlist_t *list)
{
if (list) {
if (-1 == list->last) {
return 1;
} else {
return 0;
}
} else {
return -1;
}
}
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 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;
}
</span></span>
3.main.c
<span style="font-size:14px;"><span style="font-family:Comic Sans MS;font-size:14px;">#include <stdio.h>
#include <stdlib.h>
#include "seqlist.h"
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;
}
VisitSqlist(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);
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));
VisitSqlist(list);
ClearSqlist(list);
printf("after clear, total number of list is %d\n", LengthSqlist(list));
VisitSqlist(list);
DestroySqlist(list);
return 0;
}
</span></span>
本文介绍了一种使用C语言实现线性表的方法,重点讲解了顺序存储结构的设计与操作,包括创建、销毁、清空、判断空满状态、获取长度、插入、删除元素等核心功能。
299

被折叠的 条评论
为什么被折叠?



