简单数据结构-数组实现线性表
2016-04-20 10:31
453 查看
/* * File name : List.cpp * Function : * Created on : 2016年4月19日 * Author : beijiwei@qq.com * Copyright : 欢迎大家和我一起交流学习,转载请保持源文件的完整性。 任何单位和个人不经本人允许不得用于商业用途 */ #include <cstdio> #include <iostream> using namespace std; #define MAX 10 typedef struct{ int array[MAX]; int length; }List; void list_init(List &L); void list_clear(List &L); bool list_is_empty(List &L); bool list_insert(List &L, int i, int elem); bool list_delete(List &L, int i); bool list_get(List &L, int i,int &elem); int list_search(List &L, int elem); int list_get_length(List &L); int main(int argc, char** argv) { List one; list_init(one); cout<<"List one is empty ? "<<list_is_empty(one)<<endl; cout<<"The length of List one is :"<<list_get_length(one)<<endl; for(int i=0;i<=MAX;i++) { list_insert(one,i,i+10); } cout<<"\n\n\n"; cout<<"List one is empty ? "<<list_is_empty(one)<<endl; cout<<"The length of List one is :"<<list_get_length(one)<<endl; cout<<"The elem in List one is:"<<endl; for(int i=0;i<list_get_length(one);i++) { int elem; list_get(one,i,elem); cout<<elem<<"\t"; } list_delete(one,4); cout<<endl<<"The length of List one is :"<<list_get_length(one)<<endl; cout<<"The elem in List one is:"<<endl; for(int i=0;i<list_get_length(one);i++) { int elem; list_get(one,i,elem); cout<<elem<<"\t"; } list_delete(one,1); cout<<endl<<"List one is empty ? "<<list_is_empty(one)<<endl; cout<<"The length of List one is :"<<list_get_length(one)<<endl; cout<<"The elem in List one is:"<<endl; for(int i=0;i<list_get_length(one);i++) { int elem; list_get(one,i,elem); cout<<elem<<"\t"; } return 0; } void list_init(List &L) { L.length=0; } void list_clear(List &L) { L.length=0; } bool list_is_empty(List &L) { return (L.length==0) ? true : false; } bool list_insert(List &L, int k,int elem) { if( k<0 || k>L.length+1 || k>MAX-1) return false; if(L.length==MAX) return false; for(int i=L.length;i>k;i--) { L.array[i]=L.array[i-1]; } L.array[k]=elem; L.length++; return true; } bool list_delete(List &L, int k) { if(k>=L.length || k<0 || L.length==0) return false; for(int i=k;i<L.length-1;i++) { L.array[i]=L.array[i+1]; } L.length--; return true; } bool list_get(List &L, int i, int &elem) { if(i>L.length-1 || i<0 || L.length==0) return false; elem=L.array[i]; return true; } int list_search(List &L, int elem) { if(L.length==0) return -1; for(int i=0;i< L.length;i++) { if(elem==L.array[i]) return i; } return -1; } int list_get_length(List &L) { return L.length; }
相关文章推荐
- 【数据结构】树
- 单向链表
- 数据结构(五)位图算法
- 数据结构-Collection
- linux数据结构—链表
- 数据结构 ---- 链表
- 数据结构4.1--部分排序算法
- 用Doxygen和Graphviz给Contiki文档添加类图和调用图
- 重学数据结构系列之——八大排序算法
- 数据结构之循环链表
- 啊哈!算法【转】
- 菜鸟nginx源代码剖析数据结构篇(八) 缓冲区链表ngx_chain_t
- 数据结构--二叉树的遍历
- 数据结构算法应用C++语言描述——(1)C++基础知识
- MySQL索引背后的数据结构及算法原理
- (转载)动态规划之背包问题(一)
- 【数据结构】二叉树的实现(如:默认成员函数、(叶子)节点数、深度、四种遍历)
- 数据结构学习安排
- 数据结构串之块链串
- 数据结构串之堆串