顺序链表(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 实现单链表
数据结构是计算机存储.组织数据的方式,结构不同那么数据的检索方式和效率都不一样, 常用的数据结构有 数组 .栈 .队列 .链表 .树.堆 今天讲下单链表,单链表是一种链式存取的数据结构, 跟顺序链表 ...
随机推荐
- Chrome插件-Postman Interceptor
postman有一个chrome插件 Postman Interceptor,可以让postman中发送请求的时候使用这个网站的浏览器cookie Postman Interceptor,可以让pos ...
- C语言数据结构-顺序线性表的实现-初始化、销毁、长度、查找、前驱、后继、插入、删除、显示操作
1.数据结构-顺序线性表的实现-C语言 #define MAXSIZE 100 //结构体定义 typedef struct { int *elem; //基地址 int length; //结构体当 ...
- CocosPods 每次install pod 都卡在analyzing
最近使用CocoaPods来添加第三方类库,无论是执行pod install还是pod update都卡在了Analyzing dependencies不动 原因在于当执行以上两个命令的时候会升级Co ...
- cocos2d 3.3 安装教程
最近在学习cocos-2d,百度一下cocos-2d,铺天盖地的都是cocos-2dx的教程,不得不说,老外还是钟情cocos2d,之前安装过cocos2d 2.0版本,网上的教程还是都是0.9的安装 ...
- kuangbin专题十二 POJ1661 Help Jimmy (dp)
Help Jimmy Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14214 Accepted: 4729 Descr ...
- react 中文文档案例六 (表单)
class Reservation extends React.Component { constructor(props) { super(props); this.state = { isGoin ...
- window下安装git与git使用
有的eclipse已经自带了Git了,就不用安装了.如果,想重新安装,可以先卸载GIT,卸载 不同eclipse卸载不一样: 1.在Eclipse中依次点击菜单"Help"-> ...
- P2575 高手过招
传送门 直接搞好像搞不了 考虑转换模型 显然每一行棋子不会跑到其他行.. 所以可以把每一行的情况看成一个子博弈 显然整个答案就是每一行的SG值的异或和 不懂的回去学SG函数... 考虑怎么分析一行的状 ...
- poj3040 发工资(贪心)
题目传送门 题目大意:给一个人发工资,给出不同数量不同面额,(大面额一定是小面额的倍数),问最多能发几天,(每天实发工资>=应发工资). 思路:首先,将大于等于c的面额的钱直接每个星期给奶牛一张 ...
- 江西财经大学第一届程序设计竞赛 H
链接:https://www.nowcoder.com/acm/contest/115/H来源:牛客网 题目描述 晚上,小P喜欢在寝室里一个个静静的学习或者思考,享受自由自在的单身生活. 他总是能从所 ...