顺序链表(C++)
顺序表结构

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++)的更多相关文章
- java实现顺序链表
C&C++是那么难学,以至于我连指针是什么都不知道.所以只能学习java了. 如今想用java实现N年前学过“数据结构(c语言版)”却又是那么吃力! 慢慢练吧! 写此博客,仅标记自己学过数据结 ...
- 顺序链表的C风格实现
//头文件 #ifndef _SEQLIST_H_ #define _SEQLIST_H_ //定义数据类型 typedef void SeqList; typedef void SeqListNod ...
- 基于顺序链表的栈的顺序存储的C风格实现
头文件: #ifndef _SEQSTACK_H_ #define _SEQSTACK_H_ typedef void SeqStack; //创建一个栈 SeqStack* SeqStack_Cre ...
- [leetcode]21Merge Sorted ListNode递归合并顺序链表
/** * Merge two sorted linked lists and return it as a new list. * The new list should be made by sp ...
- 删除顺序链表中重复的数 (一) leecode
Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...
- java实现顺序表、链表、栈 (x)->{持续更新}
1.java实现节点 /** * 节点 * @luminous-xin * @param <T> */ public class Node<T> { T data; Node& ...
- 由后序遍历结果构造二叉查找树 && 二叉查找树链表化
二叉查找树通俗说就是左孩子比父亲小,右孩子比父亲大.构造这么一个树,树嘛,递归即可. 例如一棵树后序遍历是这样(下图的树):2 9 8 16 15 10 25 38 45 42 30 20.最后的20 ...
- 反转链表算法Java实现
之前遇到反转链表的算法,比较晦涩难解,但其实挺简单的. 目标:将一个顺序链表反转. 思路:用三个辅助节点,每次实现一个节点的指向反转,即他的后继变为他的前驱. 三个辅助节点: p q r 按顺序 ...
- PHP 实现单链表
数据结构是计算机存储.组织数据的方式,结构不同那么数据的检索方式和效率都不一样, 常用的数据结构有 数组 .栈 .队列 .链表 .树.堆 今天讲下单链表,单链表是一种链式存取的数据结构, 跟顺序链表 ...
随机推荐
- hpp文件简介
Boost库文件采用的.hpp的后缀,而不是分成两个文件,也就是”.h+.cpp”,之所以这样做是有理由的,首先就是与普通的C/C++头文件区分,另外一个原因就是使Boost库不需要预先编译,直接引用 ...
- mysql远程访问被禁止
远程连接Mysql服务器的数据库,错误代码是1130,ERROR 1130: Host xxx.xxx.xxx.xxx is not allowed to connect to this MySQL ...
- 最长回文串:LeetCode:Longest Palindromic Substring
class Solution { public: string longestPalindrome(string s) { int length=s.length(); ; ; ][]={false} ...
- springcloud系列五 feign远程调用服务
一:Feign简介 Feign 是一种声明式.模板化的 HTTP 客户端,在 Spring Cloud 中使用 Feign,可以做到使用 HTTP请求远程服务时能与调用本地方法一样的编码体验,开发者完 ...
- Python-6-字典-函数dict,字典的基本操作及将字符串设置功能用于字典
phonebook = {'Alice': '2341', 'Beth': '9102', 'Cecil': '3258'} 字典由键及其相应的值组成,这种键-值对称为项. 键必须为独一无二,值不必如 ...
- IT网址大全
图像处理 [素材] 在线作图 [素材] 思缘设计论坛 [素材] ps联盟 [素材] ps学习网 [素材] ps教程论坛 [素材] ps爱好者 [素材] 46ps [素材] ...
- 毕业设计 python opencv实现车牌识别 矩形矫正
主要代码参考https://blog.csdn.net/wzh191920/article/details/79589506 GitHub:https://github.com/yinghualuow ...
- python3 读取表格的数据
python3 读取表格的数据 xlrd1.1.0的下载网址:https://pypi.python.org/pypi/xlrd. xlrd1.1.0兼容python2和python3. python ...
- mybatis mapper问题列表
id出现两次 2018-11-14 16:15:03.833 DEBUG 41432 --- [ main] c.a.i.o.d.mapper.DatvMapper.insert ...
- SQL Server外部链接时报错:Error locating serverInstance specified
SQL Server外部链接时报错:Error locating server/Instance specified 连接时报错信息: 08001 sql server network interfa ...