<span style="font-size:18px;"><strong>#pragma once
#include <iostream>
using namespace std; typedef enum { FALSE, TRUE }Status; template<class Type>
class SeqList
{
public:
SeqList(int sz = DefaultSize)
{
capacity = sz > DefaultSize ? sz : DefaultSize;
base = new Type[capacity];
size = 0;
}
~SeqList()
{
destroy();
/* delete []base;
base = NULL;
capacity = size = 0;*/
}
public:
Status Inc()
{
Type *newbase = new Type[capacity + INC_SIZE];
if (newbase == NULL)
{
return FALSE;
}
memcpy(newbase, base, sizeof(Type)*size);
delete[]base;
base = newbase;
capacity += INC_SIZE;
return TRUE;
}
Status merge(SeqList<Type> <1, SeqList<Type> <2)
{
int i = 0;
int j = 0;
int k = 0;
while (i < lt1.size && j < lt2.size)
{
if (lt1.base[i] > lt2.base[j])
{
base[k++] = lt2.base[j++];
}
else
{
base[k++] = lt1.base[i++];
}
}
while(i < lt1.size)
{
base[k++] = lt1.base[i++];
}
while (j < lt2.size)
{
base[k++] = lt2.base[j++];
}
size = lt1.size + lt2.size;
return TRUE;
}
Status IsFull() const
{
if (size >= capacity)
{
return TRUE;
}
else
{
return FALSE;
}
}
Status Empty() const
{
if (size == 0)
{
return TRUE;
}
else
{
return FALSE;
}
}
Status push_back(const Type& x)
{
if (IsFull() && !Inc())
{
cout << "IsFull!不能尾插" << x << endl;
return FALSE;
}
base[size++] = x;
return TRUE;
}
Status push_front(const Type& x)
{
if (IsFull() && !Inc())
{
cout << "IsFull!不能头插" << x << endl;
return FALSE;
}
for (int i = size; i > 0; --i)
{
base[i] = base[i - 1];
}
base[0] = x;
size++;
return TRUE;
}
Status pop_back()
{
if (Empty())
{
cout << "Empty!不能尾删" << endl;
return FALSE;
}
size--;
return TRUE;
}
Status pop_front()
{
if (Empty())
{
cout << "Empty!不能头删" << endl;
return FALSE;
}
for (int i = 0; i < size; ++i)
{
base[i] = base[i + 1];
}
size--;
return TRUE;
}
Status insert_val(const Type& x)
{
if (IsFull() && !Inc())
{
cout << "IsFull!不能按值插入" << x << endl;
return FALSE;
}
int i = 0;
while (base[i] < x && i < size)
{
i++;
}
insert_pos(i, x);
return TRUE;
}
Status insert_pos(int pos, const Type& x)
{
if (IsFull() && !Inc())
{
cout << "IsFull! 不能按位置插入" << endl;
return FALSE;
}
if (pos < 0 || pos > size)
{
return FALSE;
}
for (int i = 0; i < size; ++i)
{
if (i == pos)
{
for (int j = size; j > i; --j)
{
base[j] = base[j - 1];
}
}
}
base[pos] = x;
size++;
return TRUE;
}
int find(const Type& x)
{
if (size == 0)
{
return -1;
}
for (int i = 0; i < size; ++i)
{
if (base[i] == x)
{
return i;
}
}
}
Status delete_pos(int pos)
{
if (pos < 0 || pos > size)
{
return FALSE;
}
for (int i = 0; i < size; ++i)
{
if (i == pos)
{
for (int j = i; j < size; ++j)
{
base[j] = base[j + 1];
}
break;
}
}
size--;
return TRUE;
}
Status delete_val(const Type& x)
{
int key = find(x);
if (key == -1)
{
return FALSE;
}
delete_pos(key);
return TRUE;
/*if(Empty())
{
cout << "Empty!" << endl;
return FALSE;
}
for(int i = 0; i < size; ++i)
{
if(base[i] == x)
{
for(int j = i; j < size; ++j)
{
base[j] = base[j+1];
}
break;
}
}
size--;
return TRUE;*/
}
void sort()
{
if (size == 0 || size == 1)
{
return;
}
for (int i = 0; i < size - 1; ++i)
{
for (int j = 0; j < size - i - 1; ++j)
{
if (base[j] > base[j + 1])
{
int tmp = base[j];
base[j] = base[j + 1];
base[j + 1] = tmp;
}
}
}
}
void resver()
{
if (size == 0 || size == 1)
{
return;
}
for (int i = 0, j = size - 1; i < j; ++i, --j)
{
int tmp = base[i];
base[i] = base[j];
base[j] = tmp;
}
}
int length()
{
return size;
}
void clear()
{
size = 0;
}
Status destroy()
{
if (base == NULL)
{
return FALSE;
}
delete[]base;
base = NULL;
size = capacity = 0;
return TRUE;
}
Status modify_val(const Type& x, const Type& y)
{
int key = find(x);
if (key == -1)
{
return FALSE;
}
modify_pos(key, y);
return TRUE;
}
Status modify_pos(int pos, const Type& x)
{
if (pos < 0 || pos > size)
{
return FALSE;
}
for (int i = 0; i < size; ++i)
{
if (i == pos)
{
base[i] = x;
}
}
return TRUE;
}
void show_list()
{
for (int i = 0; i < size; ++i)
{
cout << base[i] << ' ';
}
cout << endl;
}
private:
enum {DefaultSize = 20, INC_SIZE = 3};
Type *base;
int capacity;
int size;
};</strong></span>



<span style="font-size:18px;"><strong>#include "SeqList.h"

void main()
{
SeqList<int> It1;
SeqList<int> It2;
SeqList<int> mylist; int select = 1;
int pos;
int item;
system("Color 0d");
while (select)
{
cout << "************************************" << endl;
cout << "* [0] quit_system [1] push_back *" << endl;
cout << "* [2] push_front [3] show_list *" << endl;
cout << "* [4] pop_back [5] pop_front *" << endl;
cout << "* [6] insert_val [7] insert_pos *" << endl;
cout << "* [8] find [9] delete_pos *" << endl;
cout << "* [10] delete_val [11] sort *" << endl;
cout << "* [12] resver [13] length *" << endl;
cout << "* [14] clear [15] destroy *" << endl;
cout << "* [16] modify_val [17] modify_pos *" << endl;
cout << "* [18] merge *" << endl;
cout << "************************************" << endl;
cout << "请选择:>";
cin >> select;
switch (select)
{
case 1:
cout << "请输入要插入的数据(-1结束):>";
while (cin >> item, item != -1)
{
mylist.push_back(item);
}
break;
case 2:
cout << "请输入要插入的数据(-1结束):>";
while (cin >> item, item != -1)
{
mylist.push_front(item);
}
break;
case 3:
system("cls");
mylist.show_list();
system("pause");
break;
case 4:
mylist.pop_back();
break;
case 5:
mylist.pop_front();
break;
case 6:
cout << "请输入要插入的值:>";
cin >> item;
mylist.insert_val(item);
break;
case 7:
cout << "请输入要插入的值的下标:>";
cin >> pos;
cout << "请输入要插入的值";
cin >> item;
mylist.insert_pos(pos, item);
break;
case 8:
cout << "请输入要查找的值:>";
cin >> item;
cout << "该值下标为:" << mylist.find(item) << endl;
break;
case 9:
cout << "请输入要删除的值的下标:>";
cin >> item;
mylist.delete_pos(item);
break;
case 10:
cout << "请输入要删除的值:>";
cin >> item;
mylist.delete_val(item);
break;
case 11:
mylist.sort();
break;
case 12:
mylist.resver();
break;
case 13:
cout << "线性表的长度为:" << mylist.length() << endl;
break;
case 14:
mylist.clear();
break;
case 15:
mylist.destroy();
break;
case 16:
cout << "请输入要改动的值:>";
cin >> item;
cout << "请输入改动后的值:>";
cin >> pos;
mylist.modify_val(item, pos);
break;
case 17:
cout << "请输入要改动的值的下标:>";
cin >> pos;
cout << "请输入改动后的值:>";
cin >> item;
mylist.modify_pos(pos, item);
break;
case 18:
for (int i = 1; i < 10; i += 2)
{
It1.push_back(i);
}
for (int i = 2; i <= 10; i += 2)
{
It2.push_back(i);
}
mylist.merge(It1, It2);
mylist.show_list();
default:
break;
}
}
} </strong></span>



执行结果例如以下:

【C++/数据结构】顺序表的基本操作的更多相关文章

  1. hrbustoj 1545:基础数据结构——顺序表(2)(数据结构,顺序表的实现及基本操作,入门题)

    基础数据结构——顺序表(2) Time Limit: 1000 MS    Memory Limit: 10240 K Total Submit: 355(143 users) Total Accep ...

  2. 顺序表的基本操作【c语言】【创建、插入、删除、输出】

    作为数据结构初学者,上课时对一些知识点掌握得不是很透彻,所以利用课余时间通过微博平台总结所学知识,加深对知识的见解,记录学习历程便于后需要时参考. #include<stdio.h> #i ...

  3. hrbust-1545-基础数据结构——顺序表(2)

    http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=1545 基础数据结构——顺序表(2) ...

  4. 顺序表的基本操作(C)

    在顺序存储结构实现基本操作:初始化.创建.插入.删除.查找.遍历.逆置.合并运算. 运行示例: 请输入线性表La的长度: 请输入线性表La中的元素(共5个) *** 此时线性表La中的元素 *** * ...

  5. 数据结构---顺序表(C++)

    顺序表 是用一段地址连续的存储单元依次存储线性表的数据元素. 通常用一维数组来实现 基本操作: 初始化 销毁 求长 按位查找 按值查找 插入元素 删除位置i的元素 判空操作 遍历操作 示例代码: // ...

  6. 顺序表及基本操作(C语言)

    #include <stdio.h> #include <stdlib.h> //基本操作函数用到的状态码 #define TRUE 1; #define FALSE 0; # ...

  7. c数据结构 顺序表和链表 相关操作

    编译器:vs2013 内容: #include "stdafx.h"#include<stdio.h>#include<malloc.h>#include& ...

  8. 数据结构顺序表删除所有特定元素x

    顺序表类定义: template<class T> class SeqList : { public: SeqList(int mSize); ~SeqList() { delete[] ...

  9. 数据结构顺序表Java实现

    Java实现顺序表算法:1:首先我们需要定义我们的接口,关于顺序表的一些基本的操作:顺序表中的操作都有增删改查. //List接口 public interface IList { //返回线性表的大 ...

  10. python算法与数据结构-顺序表(37)

    1.顺序表介绍 顺序表是最简单的一种线性结构,逻辑上相邻的数据在计算机内的存储位置也是相邻的,可以快速定位第几个元素,中间不允许有空,所以插入.删除时需要移动大量元素.顺序表可以分配一段连续的存储空间 ...

随机推荐

  1. POJ 1236 Tarjan算法

    这道题认真想了想.. [ 题目大意:有N个学校,从每个学校都能从一个单向网络到另外一个学校,两个问题 1:初始至少需要向多少个学校发放软件,使得网络内所有的学校最终都能得到软件. 2:至少需要添加几条 ...

  2. 跳出双重for循环的案例__________跳出当前循环(continue out)

    package com.etc.operator; public class demo { public static void main(String[] args) { // break out; ...

  3. 上传预览图片的插件jquery-fileupload

    上传预览图片的插件jquery-fileupload github地址:https://github.com/blueimp/jQuery-File-Upload 中文文档:http://www.jq ...

  4. Android 使用SQLite存储以及读取Drawable对象

    在进行Android开发过程中,我们经常会接触到Drawable对象,那么,若要使用数据库来进行存储及读取,该如何实现? 一.存储 //第一步,将Drawable对象转化为Bitmap对象 Bitma ...

  5. wamp中的mysql服务与原来安装的mysql服务冲突的解决办法

    如果原来机器上已经安装了mysql,在安装wamp之后,打开wamp上的mysql时会打不开,或者会将原来安装的mysql服务关闭.原因是两个mysql共用了3306端口,解决办法是更改其中的一个端口 ...

  6. nodejs要远程连接另一个主机上的monogodb数据库服务器

    我的mongodb是装在linux下的. 首先,先添加用户 1.首先在mongodb服务器主机上进行terminal命令行,输入 mongo 2.输入 use admin 进入用户管理数据库 3.db ...

  7. WM消息大全

    消息名 消息值 说明 WM_CREATE 0x0001 应用程序创建一个窗口 WM_DESTROY 0x0002 一个窗口被销毁 WM_MOVE 0x0003 移动一个窗口 WM_SIZE 0x000 ...

  8. ANN:DNN结构演进History—LSTM网络

    为了保持文章系列的连贯性,参考这个文章: DNN结构演进History-LSTM_NN 对于LSTM的使用:谷歌语音转录背后的神经网络 摘要: LSTM使用一个控制门控制参数是否进行梯度计算,以此避免 ...

  9. 关于python学习路线

    *Python进阶(目录/书籍/学习路线) 忘了从哪里转的了,没办法标记哦,实在不好意思... 目录:) 1. 简介&helloworld&开发环境配置 2.基本语法:标识符& ...

  10. Java Web_过滤器

    过滤器分类: Servlet2.5: request:用户直接访问页面时,Web容器将会调用过滤器. forward:目标资源是通过RequestDispatcher的forward访问时,该过滤器将 ...