附:1.线性表之顺序存储结构
来源:互联网 发布:首届编程杯冠军 编辑:程序博客网 时间:2024/06/14 15:43
附: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 16typedef 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 */voidDestroySqlist(seqlist_t *list);/* * clear the list and reset it as empty * Input : the list to be cleared. * Output: void * Return: void */voidClearSqlist(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 */intEmptySqlist(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 */intFullSqlist(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 */intLengthSqlist(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 */intInsertSqlist(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 */intDeleteSqlist(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 */intGetSqlist(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 */intSetSqlist(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 */intSearchSqlist(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 */voidVisitSqlist(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");elseprintf("}\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>
0 0
- 附:1.线性表之顺序存储结构
- 线性表之顺序表存储结构
- 数据结构:线性表之顺序存储结构
- 线性表之顺序存储结构
- 数据结构-线性表之顺序存储结构
- 线性表之顺序存储结构
- 【数据结构】线性表之顺序存储结构
- 线性表之顺序存储结构
- 数据结构之顺序存储结构线性表
- 数据结构:线性表之顺序存储结构
- C# 线性表之顺序存储结构
- 线性表之顺序存储结构
- 线性表之顺序存储结构
- 线性表之顺序存储结构
- C语言数据结构-1.线性表之顺序存储结构
- 线性表之顺序存储结构和链式存储结构
- 线性表(一)之线性表的顺序存储结构
- 【线性表】顺序存储结构之:顺序表
- Git远程操作详解
- 不锈钢洁净地漏
- mysql 5.6加强密码强度插件validate_password 的使用
- 一些实用的安卓UI设计工具
- no-ip使用教程
- 附:1.线性表之顺序存储结构
- 【LeetCode with Python】 Sort List
- Windows 7 64bit和Visual Studio 2010下安装及使用Zlib 1.2.5
- alibaba远程调用框架dubbo原理
- CBD寝具:任流光溢彩,唯爱丽斯永恒
- 中沃云九周年感谢一路相伴
- HDOJ-2102-A计划 解题报告
- UVA 123 (暑假-排序、检索(2)-B-Searching Quickly)
- 我思故我在