顺序表结构

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. [Swift实际操作]九、完整实例-(7)登录页面:创建自定义视图及相关组件Swift实际操作

    本文将开始创建登录页面,首先创建该页面所需的一些自定义组件:做为登录按钮的自定义视图对象.在[RegLogin]组的名称上点击鼠标右键,打开右键菜单.[New File]->[Cocoa Tou ...

  2. 屏幕字段不允许直接输入,只能通过SearchHelp(F4)

    ---恢复内容开始--- REPORT  z_barry_test NO STANDARD PAGE HEADING . PARAMETERS: p_date TYPE sy-datum ,      ...

  3. Oracle使用PARTITION BY 实现数据稠化报表

    所谓的数据稠化,就是补全缺失的数据.因为在数据库表中,存储的数据经常是稀疏的(sparse data),也就是不完整的.比如记录一个员工每个月的销售额,用这么一个销售表来记录:SalesRecord( ...

  4. Vue-Router路由Vue-CLI脚手架和模块化开发 之 路由的动态跳转

    在上一篇的博文中,实现的跳转是在页面中进行实现的 利用vue-router实例方法,使用js对路由进行动态跳转: 1.router.push:参数为路由对象,跳转到指定路由,跳转后会产生历史记录: & ...

  5. 解决maven项目中有小红叉的问题

    首先在window--perferences--showview中显示problems中查看出错的原因

  6. VisualSVN的安装使用

    1.什么是VisualSVN VisualSVN Server是集成了Subversion和Apache的一种版本管理工具,它简化了手工配置Subversion的繁琐步骤,安装的时候SVN Serve ...

  7. POJ1057 FILE MAPPING

    题目来源:http://poj.org/problem?id=1057 题目大意:计算机的用户通常希望能够看到计算机存储的文件的层次结构的图形化表示.Microsoft Windows的 " ...

  8. fsockopen函数被禁用的解决方法

    判断fsockopen 是否可用:function_exists('fsockopen');如果没有开启 一.开启fsockopen函数 修改php.ini,将 disable_functions = ...

  9. 75th LeetCode Weekly Contest Champagne Tower

    We stack glasses in a pyramid, where the first row has 1 glass, the second row has 2 glasses, and so ...

  10. 2015苏州大学ACM-ICPC集训队选拔赛(1) 1005

    发现 Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 65535/32768K (Java/Other) Total Submission ...