线性表的顺序表示和实现(C语言描述)
2010-04-05 13:13
656 查看
严蔚敏《数据结构(C语言版)》
code:
code:
输出:
code:
]//SqList.h #ifndef SQLISTH #define SQLISTH #include <stdio.h> #include <malloc.h> #define LISTINITSIZE 10 #define LISTINCREMENT 10 typedef int ElemType; typedef struct { ElemType *elem; int length; int listsize; }SqList; int initList(SqList &L) { L.elem=(ElemType *)malloc(LISTINITSIZE*sizeof(ElemType)); if(!L.elem) return -1; L.length=0; L.listsize=LISTINITSIZE; return 0; } int destroyList(SqList &L) { delete [] L.elem; L.length=0; L.listsize=0; return 0; } int getElem(SqList L,int i) { if(i<1||i>L.length) return -1; return L.elem[i-1]; } int searchElem(SqList L,ElemType e) { for(int i=0;i<L.length;i++) { if(L.elem[i]==e) return i; } return -1; } int insertList(SqList &L,int i,ElemType e) { if(i<1||i>L.length+1) return -1; if(L.length>=L.listsize) { ElemType *newbase=(ElemType *)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(ElemType)); if(!newbase) return -1; L.elem=newbase; L.listsize+=LISTINCREMENT; } for(int n=L.length;n>=i-1;n--) L.elem[n+1]=L.elem ; L.elem[i-1]=e; L.length+=1; return 0; } int deleteList(SqList &L,int i,ElemType &e) { if(i<1||i>L.length) return -1; e=L.elem[i-1]; for(i=i-1;i<L.length-1;i++) L.elem[i]=L.elem[i+1]; L.length-=1; return 0; } int printList(SqList L) { for(int i=0;i<L.length;i++) printf("%d ",L.elem[i]); printf("/n"); return 0; } #endif
code:
]//test1.cpp #include <stdio.h> #include "SqList.h" int main(int argc,char *argv[]) { SqList List; initList(List); insertList(List,1,0); insertList(List,2,1); insertList(List,3,2); insertList(List,4,3); insertList(List,5,4); insertList(List,6,5); insertList(List,7,6); insertList(List,8,7); insertList(List,9,8); insertList(List,10,9); insertList(List,11,10); printList(List); printf("%d/n",searchElem(List,5)); printf("%d/n",getElem(List,5)); ElemType e; deleteList(List,11,e); printf("%d/n",e); printList(List); return 0; }
输出:
相关文章推荐
- 线性表的顺序表示(C语言实现)
- C语言线性表的顺序表示与实现实例详解
- C语言实现顺序线性表的表示、插入、删除
- 线性表的顺序表示和实现(C语言)
- 线性表的顺序表示(C语言实现)
- 线性表的顺序表示及实现(C语言编写)
- 2.2笔记一线性表的顺序表示和实现
- 线性表的顺序表示和实现
- 线性表的顺序表示与实现--顺序表
- 线性表的顺序实现(c语言)
- 线性表顺序实现(C语言)
- 数据结构-----线性表的顺序表示与实现
- C语言--线性表的顺序存储的实现
- 动态分配的顺序线性表的十五种操作—C语言实现
- 实验一 线性表的顺序表示与实现
- C语言实现一般线性表的顺序存储
- 数据结构—线性表的顺序表示与实现
- 2.2线性表的顺序表示和实现
- 2.2线性表的顺序表示和实现(1)
- 数据结构教程 第六课 线性表的顺序表示和实现