顺序表(C++)
以下为数据结构中的顺序表实现代码,已测试能够运行。虽然说是C++版的,但是其实应该是C语言班的。C++应该是面向对象,用抽象方法实现,而以下代码是面向过程的,只是把C语言中的输入(scanf)和输出(printf)改为了cin和cout而已。如果想要改为C++版的,可以将各个函数变为类的成员函数,使用方法改为调用类的成员方法而已,没有什么特别的。
#include<iostream>
#include<stdlib.h>//清屏操作的头文件
using namespace std;
//主要操作的函数声明
void insert(int A[],int &length,int);
int _delete(int A[],int &length,int n);
int locate(int A[],int length,int n);
int get(int A[],int length,int );
void create(int A[],int length);
void show(int A[],int length);
void main1();
int main(){
int action,A[100],length;
char c;
main1();
while(cin>>action){
if(action==1){
//创建
system("cls");
cout<<"please input the length:"<<endl;
cin>>length;
create(A,length);
system("pause");
system("cls");
main1();
} else if(action==2){
//插入
system("cls");
cout<<"please input the integer:"<<endl;
int n;
cin>>n;
insert(A,length,n);
cout<<"OK! the number has been inserted!"<<endl;
system("pause");
system("cls");
main1();
} else if(action==3){
//打印
system("cls");
show(A,length);
system("pause");
system("cls");
main1();
} else if(action==4){
//获取
system("cls");
cout<<endl<<"please input the number's index you want to get:"<<endl;
int n;
cin>>n;
int result=get(A,length,n);
if(!result){
cout<<"wrong! can't find the number in the Array."<<endl;
} else {
cout<<"OK! find it! it's "<<result<<endl;
}
system("pause");
system("cls");
main1();
} else if(action==5){
//删除
system("cls");
cout<<endl<<"please input the number you want to delete:"<<endl;
int n;
cin>>n;
int result=_delete(A,length,n);
if(!result){
cout<<"wrong! can't find the number in the Arrar."<<endl;
} else {
cout<<"OK! the number has been deleted"<<endl;
}
system("pause");
system("cls");
main1();
} else if(action==6){
//定位
system("cls");
cout<<endl<<"please input the number you want to locate:"<<endl;
int n;
cin>>n;
int result=locate(A,length,n);
if(!result){
cout<<"wrong! can't find the number in the Array."<<endl;
} else {
cout<<"the index of "<<n<<" is "<<result<<endl;
}
system("pause");
system("cls");
main1();
} else {
//退出
exit(0);
}
}
}
void insert(int A[],int &length,int n){
int i=0;
while(i<length&&A[i]<n){
i++;
}
int temp=i;
i=length;
while(i>temp){
A[i]=A[i-1];
i--;
}
A[temp]=n;
length++;
}
int _delete(int A[],int &length,int n){
//用于删除确定的一个数,可稍加修改,用于删除下表为n的数
int i=0;
while(i<length&&A[i]!=n){
i++;
}
if(i==length){
//如果i等于数组的长度,则表示未查找到n值。
return 0;
} else {
//如果查找到n值。
while(i<length-1){
A[i]=A[i+1];
i++;
}
length--;
return 1;
}
}
int locate(int A[],int length,int n){
int i=0;
while(i<length&&A[i]!=n){
i++;
}
if(i==length){
return 0;
} else {
return i;
}
}
int get(int A[],int length,int index){
if(index<0||index>length-1){
//当索引下标小于0或大于长度-1的时候,就超出了数组范围。
return 0;
} else {
return A[index];
}
}
void create(int A[],int length){
cout<<"please input the numbers of the Array"<<endl;
for(int i=0;i<length;i++){
cin>>A[i];
}
cout<<endl<<"OK! the Array have been created!"<<endl;
}
void show(int A[],int length){
cout<<"current Array is:"<<endl;
for(int i=0;i<length;i++){
cout<<A[i]<<' ';
}
cout<<endl;
}
void main1(){
cout<<"--------------------------------------------"<<endl;
cout<<"| |"<<endl;
cout<<"| welcome to use the SquenticalList |"<<endl;
cout<<"| 1->create the list |"<<endl;
cout<<"| 2->insert to the list |"<<endl;
cout<<"| 3->show the list |"<<endl;
cout<<"| 4->get the number |"<<endl;
cout<<"| 5->delete the number |"<<endl;
cout<<"| 6->locate the numbet |"<<endl;
cout<<"| 7->exit |"<<endl;
cout<<"| |"<<endl;
cout<<"--------------------------------------------"<<endl;
}
顺序表(C++)的更多相关文章
- jdk顺序表笔记
一.AbstractCollection 提供了集合的最大实现 继承该类,必须实现size()和iterator(),因为该类操作集合都是通过iterator 二.fail-fast策略 该策略在集合 ...
- c++顺序表基本功能
头文件 #define LIST_MAX_SIZE 5#define LISTINCREMENT 2#include<assert.h>#include<string>temp ...
- 数据结构:顺序表(python版)
顺序表python版的实现(部分功能未实现) #!/usr/bin/env python # -*- coding:utf-8 -*- class SeqList(object): def __ini ...
- 《数据结构》2.2顺序表(sequence list)
//顺序表节点的定义 typedef struct { datatype data[MAXSIZE]; //数组容量的上限 int len; //记录最后一个元素的位置,相当于一个指针,表空时len= ...
- c数据结构 顺序表和链表 相关操作
编译器:vs2013 内容: #include "stdafx.h"#include<stdio.h>#include<malloc.h>#include& ...
- java顺序表和树的实现
一.顺序表 1.线性表 //java顺序表的实现,如ArrayList就是用线性表实现的,优点是查找快,缺点是添加或删除要移动很多元素,速度慢 public class SequenceList { ...
- 数据结构顺序表删除所有特定元素x
顺序表类定义: template<class T> class SeqList : { public: SeqList(int mSize); ~SeqList() { delete[] ...
- 顺序表C语言版
#include <stdio.h> /* * 顺序表最多输入N个数 */ #define N 10 #define OK 1 #define ERROR -1 struct sequeu ...
- C#线性表之顺序表
线性表是最简单.最基本.最常用的数据结构.线性表是线性结构的抽象(Abstract), 线性结构的特点是结构中的数据元素之间存在一对一的线性关系. 这种一对一的关系指的是数据元素之间的位置关系,即: ...
- C语言 线性表 顺序表结构 实现
一个能够自动扩容的顺序表 ArrList (GCC编译). #include <stdio.h> #include <stdlib.h> #include <string ...
随机推荐
- c# BackGroundWorker 多线程操作的小例子
在我们的程序中,经常会有一些耗时较长的运算,为了保证用户体验,不引起界面不响应,我们一般会采用多线程操作,让耗时操作在后台完成,完成后再进行处理或给出提示,在运行中,也会时时去刷新界面上的进度条等显示 ...
- javaSE之如何将一个文件复制到另一个文件
/* * (1). 文件字符输入,输出流 * 文件字节输入,输出流的read和write方法使用 * 字节数组读写数据,即以字节为单位处理数据,因此,字节流不能很好的操作Unicode字符 * ,比如 ...
- Reverse Linked List II [LeetCode]
Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1-> ...
- Play framework 2.0 -应用程序全局设置(转)
转载自: http://shenbai.iteye.com/blog/1517366 1.全局对象 在工程中定义全局对象可以允许你操作你的应用程序的全局设置.这个全局对象必须定义在根包下. impor ...
- OpenTSDB介绍——基于Hbase的分布式的,可伸缩的时间序列数据库,而Hbase本质是列存储
原文链接:http://www.jianshu.com/p/0bafd0168647 OpenTSDB介绍 1.1.OpenTSDB是什么?主要用途是什么? 官方文档这样描述:OpenTSDB is ...
- Greenplum——升级的分布式PostgresSQL
Greenplum数据库基于PostgreSQL开源技术.本质上讲,它是多个PostgreSQL实例一起充当一个数据库管理系统.Greenplum以PostgreSQL 8.2.15为基础构建,在SQ ...
- 在SQLite中创建数据库时总是提示错误?
答案:原先以为是因为编码影响的其实不是,是因为逗号和分号的原因,不是标准的英文状态下的格式
- 【bzoj3160】万径人踪灭
题意:给一个只含a.b的字符串,求所有的回文不连续子序列. manacher+FFT. 先求出所有回文序列,再减去连续子序列(即回文串). 将a.b分开考虑,对于一个对称轴,以其为回文中心的回文序列的 ...
- [Jquery]网页定位导航特效
描述:左右联动的导航,非常适合展示页面内容多,区块划分又很明显的,点击右边固定导航项时,左边的内容跟着切换.滑动滚动条的时候,右边的导航也随着左边的展示而进行高亮切换. 思路:比较滚动距离和楼层距离( ...
- 从原理上搞定编码(四)-- Base64编码
开发者对Base64编码肯定很熟悉,是否对它有很清晰的认识就不一定了.实际上Base64已经简单到不能再简单了,如果对它的理解还是模棱两可实在不应该.大概介绍一下Base64的相关内容,花几分钟时间就 ...