顺序表子函数
来源:互联网 发布:c语言生成随机数 编辑:程序博客网 时间:2024/06/06 01:01
#include<iostream>#include<stdlib.h>typedef int ElemType;struct List{ElemType *list;int size;int Maxsize;};void InitList(List &L){L.Maxsize = 10;L.list = new ElemType[L.Maxsize];if (L.list == NULL){std::cout << "动态可分配的存储空间用完,退出运行" << std::endl;exit(1);}L.size = 0;}void ClearList(List &L){if (L.list != NULL){ delete []L.list; L.list = NULL;}L.Maxsize = 0;L.size = 0;}int LenthList(List &L){return L.size;}bool EmptyList(List &L){return L.size == 0;}ElemType GetList(List &L, int pos){if (pos<1 || pos>L.size){std::cerr << "pos is out range!" << std::endl;exit(1);}return L.list[pos - 1];}void TraverseList(List &L){for (int i = 0; i < L.size; i++)std::cout << L.list[i] << ' ';std::cout << std::endl;}bool FindList(List &L, ElemType& item){for (int i = 0; i < L.size; i++)if (L.list[i] == item){item = L.list[i];return true;}return false;}bool InsertList(List &L, ElemType item, int pos){if (pos<-1 || pos>L.size){std::cout << "pos值无效!" << std::endl;return false;}int i;if (pos == 0){for (i = 0; i < L.size; i++)if (item < L.list[i])break;pos = i + 1;}else if (pos == -1)pos = L.size + 1;if (L.size == L.Maxsize) {int k = sizeof(ElemType);L.list = (ElemType*)realloc(L.list, 2 * L.Maxsize*k);if (L.list == NULL){std::cout << "动态可分配的储存空间用完,退出运行!" << endl;exit(1);}L.Maxsize = 2 * L.Maxsize;}for (i = L.size - 1; i >= pos - 1; i--)L.list[i + 1] = L.list[i];L.list[pos - 1] = item;L.size++;return true;}
阅读全文
0 0
- 顺序表子函数
- main 函数和子函数的顺序关系
- 子函数
- 子函数插入头结点链表
- 关于子函数内存分配和链表的一点随笔
- 通过子函数调用创建链表的三种方式
- 子函数注意事项
- 汇编 子函数
- perl 子函数
- matlab 子函数
- 子函数配置
- 函数调用子函数,注意子函数的位置
- 子函数修改值
- Perl子函数参数传递
- Perl 子函数练习
- 关于子函数调用
- 打卡17-perl 子函数
- Matlab定义子函数
- C++与Opengl交互 Python与Opnegl交互(使用鼠标函数)
- HDU4085-Peach Blossom Spring
- 删除linux自带VIM 重新安装
- 数据结构回顾与总结 图(3)Floyd算法(多源最短路问题)
- Hdu 4436 str2int 后缀自动机
- 顺序表子函数
- 加密器
- 没有指定返回类型,可能查询不到返回结果
- Ubuntu 16.04安装Nvidia显卡驱动:在安装N卡驱动的过程中,我遇到了不少问题,例如安装后会出现循环登录问题,以及登录后桌面显示不完整(启动器消失,任务栏消失),打不开系统设置等问题。
- POJ 1612 The Geodetic Set Problem (Floyd)
- SpringMVC中mvc-servlet.xml的常用配置
- python2,3 区别
- scrapy
- 算法期中1003. 最近的0