顺序表结构

struct Sq_list
{
int elem[Max_size+];
int length;
};

创建并初始化顺序表

int Init_list(Sq_list *L)
{
L->length = ;
return ;
}

按序插入元素

int insert (Sq_list *L)
{
int n;
cout << "请输入顺序表的长度:" << endl;
cin >> n;
cout << "请输入元素:" << endl;
for (int i = ; i <= n; i++)
{
int data;
cin >> data;
L->elem[i] = data;
L->length++;
}
return ;
}

在指定位置插入元素

int Insert_elem(Sq_list *L)
{
int cor,elem;
cout << "请输入指定位置的元素:" << endl;
cin >> cor;
cin >> elem;
if (cor< || cor>L->length+)
{
cout << "输入的位置不在顺序链表内!" << endl;
}
else
{
for (int i = L->length; i >= cor; i--)
{
L->elem[i+] = L->elem[i];
}
L->elem[cor] = elem;
L->length++;
}
return ;
}

删除元素

删除指定值元素

int Delete(Sq_list *L)
{
int data;
cout << "请输入要删除的元素值:" << endl;
cin >> data;
int length = L->length;//存放表的原长
for (int i = ; i <= L->length; i++)
{
if (L->elem[i] == data)
{
for (int j = i; j < L->length; j++)
{
L->elem[j] = L->elem[j + ];
}
i--;
L->length--;
}
}
if (L->length == length)//判断表长是否发生改变
{
cout << "您想要删除的元素不在本链表中" << endl;
}
return ;
}

删除指定位置的元素

int Delete2(Sq_list *L)
{
int length;
cout << "请输入要删除元素的位置:" << endl;
cin >> length;
if (length< || length>L->length)
{
cout << "您输入的位置不在本表中" << endl;
}
else
{
for (int i = length; i <= L->length; i++)
{
L->elem[i] = L->elem[i + ];
}
L->length--;
}
return ;
}

查找

按值查找

int seek_elem(Sq_list *L)
{
int elem2;
bool check = false;//用于判断表中是否有该元素
cout << "请输入您要查找的值:" << endl;
cin >> elem2;
cout << "您所查元素的位置为" << ' ';
for (int i = ; i <= L->length; i++)
{
if (L->elem[i] == elem2)
{
cout << i << ' ';
check = true;
}
}
cout << '\n';
if (check == false)
{
cout << "表中没有您需要查找的元素" << endl;
}
return ;
}

按位置查找

int seek_length(Sq_list *L)
{
int length2;
cout << "请输入您要查找的位置:" << endl;
cin >> length2;
if (length2 < || length2 > L->length)
{
cout << "您输入的位置不在本表中" << endl;
}
else
{
cout << "您所查找位置的元素为 " << L->elem[length2] << endl;
}
return ;
}

遍历一遍顺序表

int show(Sq_list *L)
{
cout << "遍历一遍当前数据表" << endl;
for (int i = ; i < L->length; i++)
{
cout << L->elem[i] <<' ';
}
cout << L->elem[L->length] << ' ' << endl;
return ;
}

完整代码

//注:主函数没写,自行调用功能函数即可测试,"stdafx.h"为vs编译器必须,其他可忽视
#include "stdafx.h"
#include <iostream>
#include <stdlib.h>
#define Max_size 1000
using namespace std;
//顺序表结构
struct Sq_list
{
int elem[Max_size+];
int length;
};
//创建并初始化顺序表
int Init_list(Sq_list *L)
{
L->length = ;
return ;
}
//按序插入元素
int insert (Sq_list *L)
{
int n;
cout << "请输入顺序表的长度:" << endl;
cin >> n;
cout << "请输入元素:" << endl;
for (int i = ; i <= n; i++)
{
int data;
cin >> data;
L->elem[i] = data;
L->length++;
}
return ;
}
//在指定位置插入元素
int Insert_elem(Sq_list *L)
{
int cor,elem;
cout << "请输入指定位置的元素:" << endl;
cin >> cor;
cin >> elem;
if (cor< || cor>L->length+)
{
cout << "输入的位置不在顺序链表内!" << endl;
}
else
{
for (int i = L->length; i >= cor; i--)
{
L->elem[i+] = L->elem[i];
}
L->elem[cor] = elem;
L->length++;
}
return ;
}
//删除指定值元素
int Delete(Sq_list *L)
{
int data;
cout << "请输入要删除的元素值:" << endl;
cin >> data;
int length = L->length;//存放表的原长
for (int i = ; i <= L->length; i++)
{
if (L->elem[i] == data)
{
for (int j = i; j < L->length; j++)
{
L->elem[j] = L->elem[j + ];
}
i--;
L->length--;
}
}
if (L->length == length)//判断表长是否发生改变
{
cout << "您想要删除的元素不在本链表中" << endl;
}
return ;
}
//删除指定位置的元素
int Delete2(Sq_list *L)
{
int length;
cout << "请输入要删除元素的位置:" << endl;
cin >> length;
if (length< || length>L->length)
{
cout << "您输入的位置不在本表中" << endl;
}
else
{
for (int i = length; i <= L->length; i++)
{
L->elem[i] = L->elem[i + ];
}
L->length--;
}
return ;
}
//按值查找
int seek_elem(Sq_list *L)
{
int elem2;
bool check = false;//用于判断表中是否有该元素
cout << "请输入您要查找的值:" << endl;
cin >> elem2;
cout << "您所查元素的位置为" << ' ';
for (int i = ; i <= L->length; i++)
{
if (L->elem[i] == elem2)
{
cout << i << ' ';
check = true;
}
}
cout << '\n';
if (check == false)
{
cout << "表中没有您需要查找的元素" << endl;
}
return ;
}
//按位置查找
int seek_length(Sq_list *L)
{
int length2;
cout << "请输入您要查找的位置:" << endl;
cin >> length2;
if (length2 < || length2 > L->length)
{
cout << "您输入的位置不在本表中" << endl;
}
else
{
cout << "您所查找位置的元素为 " << L->elem[length2] << endl;
}
return ;
}
//遍历顺序表
int show(Sq_list *L)
{
cout << "遍历一遍当前数据表" << endl;
for (int i = ; i < L->length; i++)
{
cout << L->elem[i] <<' ';
}
cout << L->elem[L->length] << ' ' << endl;
return ;
}

觉得文章不错,可以点个赞和关注哟.

顺序链表(C++)的更多相关文章

  1. java实现顺序链表

    C&C++是那么难学,以至于我连指针是什么都不知道.所以只能学习java了. 如今想用java实现N年前学过“数据结构(c语言版)”却又是那么吃力! 慢慢练吧! 写此博客,仅标记自己学过数据结 ...

  2. 顺序链表的C风格实现

    //头文件 #ifndef _SEQLIST_H_ #define _SEQLIST_H_ //定义数据类型 typedef void SeqList; typedef void SeqListNod ...

  3. 基于顺序链表的栈的顺序存储的C风格实现

    头文件: #ifndef _SEQSTACK_H_ #define _SEQSTACK_H_ typedef void SeqStack; //创建一个栈 SeqStack* SeqStack_Cre ...

  4. [leetcode]21Merge Sorted ListNode递归合并顺序链表

    /** * Merge two sorted linked lists and return it as a new list. * The new list should be made by sp ...

  5. 删除顺序链表中重复的数 (一) leecode

    Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...

  6. java实现顺序表、链表、栈 (x)->{持续更新}

    1.java实现节点 /** * 节点 * @luminous-xin * @param <T> */ public class Node<T> { T data; Node& ...

  7. 由后序遍历结果构造二叉查找树 && 二叉查找树链表化

    二叉查找树通俗说就是左孩子比父亲小,右孩子比父亲大.构造这么一个树,树嘛,递归即可. 例如一棵树后序遍历是这样(下图的树):2 9 8 16 15 10 25 38 45 42 30 20.最后的20 ...

  8. 反转链表算法Java实现

    之前遇到反转链表的算法,比较晦涩难解,但其实挺简单的. 目标:将一个顺序链表反转. 思路:用三个辅助节点,每次实现一个节点的指向反转,即他的后继变为他的前驱. 三个辅助节点: p  q  r  按顺序 ...

  9. PHP 实现单链表

    数据结构是计算机存储.组织数据的方式,结构不同那么数据的检索方式和效率都不一样, 常用的数据结构有  数组 .栈 .队列 .链表 .树.堆 今天讲下单链表,单链表是一种链式存取的数据结构, 跟顺序链表 ...

随机推荐

  1. New Distinct Substrings(后缀数组)

    New Distinct Substrings(后缀数组) 给定一个字符串,求不相同的子串的个数.\(n<=50005\). 显然,任何一个子串一定是后缀上的前缀.先(按套路)把后缀排好序,对于 ...

  2. Python和FTP

    1.HTTP主要用于基于Web的文件下载以及访问Web服务,一般客户端无须登录就可以访问服务器上的文件和服务.大部分HTTP文件传输请求都用于获取网页(即将网页文件下载到本地). 2.FTP主要用于匿 ...

  3. vue脚手架的搭建

    另一博客地址:https://segmentfault.com/a/1190000016451376 一.基础的知识 1.html 2.js 3.css二.搭建项目过程--windows系统1.nod ...

  4. Sublime Text3安装、配置及常用插件(陆续补全中~)

    一.安装Sublime Text3 网址:http://www.sublimetext.com/3 注册码:(sublime Text3汉化和激活注册码) ----- BEGIN LICENSE -- ...

  5. 生成对抗网络(GAN)相关链接汇总

    1.基础知识 创始人的介绍: “GANs之父”Goodfellow 38分钟视频亲授:如何完善生成对抗网络?(上) “GAN之父”Goodfellow与网友互动:关于GAN的11个问题(附视频) 进一 ...

  6. CF796D Police Stations 思维

    Inzane finally found Zane with a lot of money to spare, so they together decided to establish a coun ...

  7. 【模板】割点(割顶) Tarjan

    题目背景 割点 题目描述 给出一个nnn个点,mmm条边的无向图,求图的割点. 输入输出格式 输入格式: 第一行输入n,mn,mn,m 下面mmm行每行输入x,yx,yx,y表示xxx到yyy有一条边 ...

  8. $.ajax 错误信息

    $.extend({ getHtml:function(url,callback){ $.ajax({ dataType:"html", url:url, timeout:1500 ...

  9. dataTable调用接口渲染数据,没有数据,报错

    当没有数据的时候,报错: 解决方法: 在后台那边处理一下,当没有数据的时候,令 data : ' ' 或者 data : [ ] 前端代码: var loading = layer.load(1, { ...

  10. Go语言基础之4--流程控制

    一.if else语句 1.1 基本语法1 语法1: if condition { //do something } 语法2: if condition { //do something } else ...