C++顺序表(十四)
来源:互联网 发布:js将字符串向下取整 编辑:程序博客网 时间:2024/05/18 02:27
- 顺序表是在计算机内存中以数组的形式保存的线性表,是指用一组地址连续的存储单元依次存储数据元素的线性结构。线性表采用顺序存储的方式存储就称之为顺序表。顺序表是将表中的结点依次存放在计算机内存中一组地址连续的存储单元中。
如果每次只加一个值进入表中,用数组就行了,如果要加入多个值,就可以使用结构体。
顺序表一般有这几个操作:
1、输入数据
2、查找数据
3、插入数据
4、删除数据
【代码示例】
#include <iostream>#include <cstdlib>using namespace std;int Error = 0;#define MAXLISTLEN 100int ListLen = 0;int SeqList[MAXLISTLEN + 1];//顺序表查找int SearchSeqList(int i){ if ((i > ListLen) || (i < 1) || (ListLen == 0)){ Error = -1; return (-1); //查找出错返回}else{ return SeqList[i]; //返回指定位置的元素值}}//顺序表的插入 void InsertSeqList(int NewItem, int i){int j;if ((i > ListLen + 1) || (i < 1) || (ListLen == MAXLISTLEN)){Error = -2; //插入出错返回cout << "插入出错啦!!!";} else{for (j = ListLen; j >= i; j--) //从后往前移{SeqList[j + 1] = SeqList[j];}SeqList[i] = NewItem; //插入新元素ListLen = ListLen + 1; //表长加一}} //顺序表指定位置数据的删除void DeleteSeqList(int i){int j;if ((i > ListLen) || (i < 1) || (ListLen == 0)){Error = -3; //删除出错返回cout << "删除出错啦!!!";} else{for (j = i; j < ListLen; j++) //从前往后移{SeqList[j] = SeqList[j + 1];}ListLen = ListLen - 1; //表长减一}} //顺序表显示 void ShowSeqList(){int i;cout << "The list : ";for (i = 1; i <= ListLen; i++){cout << SeqList[i] << " "; //逐个显示数据元素}cout << endl; //换行} //主函数int main(int argc, char * argv[]){int r[MAXLISTLEN], i, SearchPos, NewPos, NewItem, DelPos, switch_on;cout << "Please input the ListLen : ";cin >> ListLen; //输入样本数目(表长)//创建顺序表for (i = 1; i <= ListLen; i++){cout << "Please input No." << i << "Item : ";cin >> SeqList[i];}ShowSeqList(); //显示顺序表while (true){cout << "请输入你想要的操作序号:\n" << "1、查找位置" << "\t2、插入数据" << "\t3、删除数据\t";cin >> switch_on;switch (switch_on){case 1:cout << "Please input the search pos : ";cin >> SearchPos; //输入查找位置cout << "Your Searched Item is : " << SearchSeqList(SearchPos) << endl; //输出查找的数据元素值break;case 2:cout << "Please input the NewPos where you want to insert : ";cin >> NewPos; //插入位置输入cout << "Please input the NewItem what you want to insert : ";cin >> NewItem; //插入元素输入InsertSeqList(NewItem, NewPos); //新数据插入顺序表中cout << "After insert : ";ShowSeqList(); //插入数据后,输出新的顺序表break;case 3:cout << "Please input the DelPos where you want to delete : ";cin >> DelPos; //输入删除元素位置DeleteSeqList(DelPos); //按位置删除数据cout << "After delete : ";ShowSeqList();break;default:break;}}if (Error < 0) cout << "Error" << Error << endl;system("pause");return 0;}【演示结果】
阅读全文
0 0
- C++顺序表(十四)
- 一起talk C栗子吧(第二十四回:C语言实例--顺序查找)
- 顺序表(C++)
- 顺序表(c/c++)
- C核心技术手册(十四)
- 顺序表(C语言)
- 顺序表(C实现)
- C核心技术手册(二十四)
- C语言函数集(十四)
- C语言程序学习(十四)笔记
- 算法精解十四(C语言版)
- 算法精解二十四(C语言版)
- C语言开发总结(十四)
- C语言开发总结(二十四)
- C Tricks(十四)—— 余数
- iOS学习(十四)Objective-C 继承
- C语言中的一些关键字(十四)
- (十四)C语言高级编程
- C语言 两个整数合并成一个整数
- 重启Oracle服务
- IOS6和IOS7的屏幕适配问题
- 2. linux C -- 通用双向链表
- 嚼得菜根做得大事·《菜根谭》·二
- C++顺序表(十四)
- Eclipse开发Webservice
- ECS 还是轻量应用服务器,看完评测你就知道了?
- jeecg 文本编辑器
- 测试项目招标拿不下?一定是因为你没有这个
- bootstrap前端框架的简单使用
- bzoj2438 [中山市选2011]杀人游戏(tarjan缩点)
- Source code does not match the bytecode
- AsyncChannel的使用和原理(原)