线性表的C++实现
来源:互联网 发布:php插件机制 编辑:程序博客网 时间:2024/04/30 02:38
/*************************************************************************> File Name: List.cpp> Author: Shaojie Kang> Mail: kangshaojie@ict.ac.cn > Created Time: 2015年09月15日 星期二 09时43分55秒 > Problem: 线性表的实现 ************************************************************************/#include<iostream>using namespace std;const int MaxSize = 20;const int IncrementSize = 2;struct List { int *head; int length; int size;};void InitList(List *list){ list->head = new int[MaxSize]; list->length = 0; list->size = MaxSize;}void DestoryList(List *list){ delete [](list->head); list->head = NULL; list->length = 0; list->size = 0;}void ClearList(List *list){ list->length = 0;}int GetListLength(List *list){ return list->length;}bool GetElement(List *list, int i, int &element){ if(i < 1 || i > list->length) return false; element = *(list->head + i - 1); return true;}int LocateElement(List *list, int element){ int position = 0; while(position < list->length) { if(element == *(list->head + position)) break; position++; } return position < list->length ? position+1 : 0;}bool PriorElement(List *list, int key, int &priorElement){ int cur = 1; while(cur < list->length) { if(key == *(list->head + cur)) break; cur++; } if(cur < list->length) { priorElement = *(list->head + cur - 1); return true; } else return false;}bool ListInsert(List *list, int i, int key){ if(i < 1 || i > (list->length + 1)) return false; if(list->size == list->length) { list->size += IncrementSize; int *newHead = new int[list->size]; for(int j = 0; j < i-1; ++j) *(newHead + j) = *(list->head + j); for(int j = i-1; j < list->length; ++j) *(newHead + j + 1) = *(list->head + j); *(newHead + i - 1) = key; delete [](list->head); list->head = newHead; } else { for(int j = list->length-1; j >= i-1; --j) *(list->head + j + 1) = *(list->head + j); *(list->head + i - 1) = key; } list->length++; return true;}bool ListDelete(List *list, int i, int &element){ if(i < 1 || i > list->length) return false; element = *(list->head + i - 1); for(int j = i; j < list->length; ++j) *(list->head + j - 1) = *(list->head + j); list->length--; return true;}bool ListTraverse(List *list, bool(*visit)(int *)){ int *element = list->head; for(int i = 0; i < list->length; ++i) { if(!visit(element++)) return false; } cout<<endl; return true;}bool traverse(int *element){ cout<<++(*element)<<" "; return true;}int main(){ List list; InitList(&list); int arr[] = { 6, 2, 1, 7, 8, 4, 9 }; for(int i = 0; i < sizeof(arr)/sizeof(int); ++i) ListInsert(&list, i+1, arr[i]); ListTraverse(&list, traverse); DestoryList(&list); return 0;}
0 0
- 线性表的顺序实现(C语言)
- 线性表的顺序实现(C++)
- 线性表的顺序实现(c语言)
- 线性表的C语言实现
- C语言线性表的实现
- 线性表的C语言实现
- 线性表基本功能的c语言实现
- 线性链表的c语言实现
- C语言中线性表的实现
- 线性表的实现(C语言)
- 数据结构 C语言实现 线性表的链式实现
- C语言实现线性表
- 线性表 C语言实现
- c语言实现线性表
- 数据结构--线性表C实现
- 用c写的线性表的顺序实现
- 用c写的线性表的链式实现
- 线性表的顺序存储结构的c语言实现
- Web学习第一天:Servlet+JSP简单Demo
- vs2010 fatal error c1083
- linux鸟哥私房菜学习笔记--第八章
- 数据结构之自建算法库——链队(链式队列)
- EPLAN Electric P8最常遇见的几个问题
- 线性表的C++实现
- iOS开发-清理缓存功能的实现
- C++Primer第五版 6.3.2节练习
- oracle存储过程常用技巧
- Android中解析XML
- openssh升级
- Struts2自定义拦截器,实现登录检查。
- 关于Android app首次安装完成后在安装界面直接“打开”应用再按home键返回桌面,重新进入app重复实例化launcher activity的问题的解决
- 静态代码块(一)