顺序链表(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 实现单链表
数据结构是计算机存储.组织数据的方式,结构不同那么数据的检索方式和效率都不一样, 常用的数据结构有 数组 .栈 .队列 .链表 .树.堆 今天讲下单链表,单链表是一种链式存取的数据结构, 跟顺序链表 ...
随机推荐
- Div的移动
//JQuery 拖拽本体DIV,把以下代码全部复制即可 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN&q ...
- ELK+Filebeat 实践 Error : read tcp 192.168.0.2:48968->121.40.216.20:5044: i/o timeout
问题描述: 这是一个i/o time 的问题,一般考虑就是磁盘满的问题.以下是我遇到的具体问题: 1.Kibana页面刷新,没有新数据出来,再次刷新或者点击页面上其他地方,kibana就变成这样了: ...
- luogu1632 点的移动
其实只需要开三重循环 根据OI中的一个重要的原理 给定一个序列a,求一个数x使得\(\sum |a_i-x|\)最小,那么这个数是序列a的中位数 证明略 然后既然是中位数,一定是数列中的数,类比到这题 ...
- zabbix发送邮件脚本
#!/usr/bin/env python #-*- coding: UTF- -*- import os,sys reload(sys) sys.setdefaultencoding('utf8') ...
- springboot整合xxl-mq学习笔记
首先xxl-mq是大神xuxueli开发的一个消息中间件框架: 与springboot整合过程: <?xml version="1.0" encoding="UTF ...
- Codeforces-B-Divisors of Two Integers(思维技巧)
Recently you have received two positive integer numbers xx and yy. You forgot them, but you remember ...
- JAVA Font类
java.awt.Font 设计字体显示效果 Font mf = new Font(String 字体,int 风格,int 字号); 字体:TimesRoman, Courier, Arial等 风 ...
- pandas实例美国人口分析
- haproxy安装:
haproxy安装: tar zxf haproxy-1.4.24.tar.gz ##解压 yum install rpm-build -y ...
- JDK Throwable
Throwable 1. 使用大量数组和List常量: private static final StackTraceElement[] UNASSIGNED_STACK = new StackTra ...