1.数据结构之线性表的顺序存储
2011-09-04 21:24
726 查看
#include <stdio.h> #include <stdlib.h> #define Description LinkListSquence #define SIZE 20 typedef int type; typedef int bool; typedef struct{ type data[SIZE]; int length; }List; List * InitList(List *list); bool ListEmpty(List *list); List * ClearList(List *list); type GetElem(List *list,int index); int LocateElem(List *list,type element); int ListLength(List *list); bool ListInsert(List * list,int index,type element); bool ListAppend(List * list,type element); type ListDelete(List *list,int index); void ShiftToRight(List * list,int index); void ShiftToLeft(List * list,int index); int main(int argc, char ** argv) { List list; type ele ; InitList(&list); if(ListEmpty(&list)) { printf("empty\n"); } ele = 0; ListAppend(&list,ele); printf("length:%d\n",list.length); ListInsert(&list,list.length-1,ele+1); printf("length:%d\n",list.length); return 0; } List * InitList(List *list) { list->length = 0; return list; } bool ListEmpty(List *list) { if(list->length == 0) { return 1; } return 0; } bool ListFull(List * list) { return list->length == SIZE; } /*Clear the list ,so that can use the element*/ List * ClearList(List *list) { list->length = 0; return list; } /*get a element by index*/ type GetElem(List *list,int index) { return list->data[index]; } /*get the index of the element in the list*/ int LocateElem(List *list,type element) { int i = list->length - 1; for(; i >= 0; i--) { if(list->data[i] == element) { return i; } } return -1; } int ListLength(List *list) { return list->length; } /*index starts with 0 to SIZE-1 */ bool ListInsert(List * list,int index,type element) { if(list->length < SIZE && index < SIZE) { int i = list->length - 1; for(;i > index;i--) { ShiftToRight(list,i); } list->data[index] = element; list->length++; return 1; } return 0; } bool ListAppend(List * list,type element) { if(list->length < SIZE) { list->data[list->length] = element; list->length++; return 1; } return 0; } /*index starts with 0 to SIZE-1 */ bool ListDelete(List *list,int index) { if(list->length < SIZE && index < SIZE) { int i = index; for(;i <= list->length - 1;i++) { ShiftToLeft(list,i); } list->length--; return 1; } return 0; } void ShiftToRight(List * list,int index) { list->data[index+1] = list->data[index]; return ; } void ShiftToLeft(List * list,int index) { list->data[index-1] = list->data[index]; return ; }
相关文章推荐
- 数据结构2----线性表顺序存储和链式存储的实现(霜之小刀)
- 【数据结构之线性表顺序存储】简单的数组的方式实现
- 数据结构之线性表顺序存储的常用操作(转载)
- [SDUT](2116)数据结构实验之链表一:顺序建立链表 ---链式存储(线性表)
- c/c++常用算法(1) -- 数据结构(线性表的顺序存储)
- 数据结构(严蔚敏)C语言实现---线性表的顺序存储
- 数据结构复习——线性表的顺序存储实现
- 数据结构之线性表顺序存储
- 数据结构——线性表之顺序存储
- 数据结构之线性表——链表的顺序存储(数组描述)
- 数据结构之线性表——栈的顺序存储
- 数据结构之线性表代码实现顺序存储,链式存储,静态链表(选自大话数据结构)
- 数据结构——线性表之顺序存储学习
- 数据结构之线性表——链表的顺序存储(数组描述)
- 数据结构之线性表——队列的顺序存储
- 数据结构之线性表顺序存储
- 【数据结构作业一】写出顺序表的结构体类型定义及查找、插入、删除算法,并以顺序表作存储结构,实现线性表的插入、删除
- 数据结构——线性表的顺序存储
- 数据结构之线性表-顺序存储
- 数据结构——线性表(一)线性表的顺序存储和单链表