網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
編寫程序,實(shí)現(xiàn)順序表的下列功能:
- 從鍵盤輸入數(shù)據(jù)建立一個(gè)順序表
- 輸出該順序表
- 往順序表中插入數(shù)據(jù)
- 從順序表中刪除數(shù)據(jù)
- 給定數(shù)據(jù),進(jìn)行查找,給出查找成功和失敗信息
C++代碼:
#include <iostream>
#include <string>
#include "windows.h"
using namespace std;
// 定義順序表結(jié)構(gòu)體
struct SequentialList
{
int* base; // 順序表首地址
int length; // 順序表長(zhǎng)度
int count; // 順序表當(dāng)前元素個(gè)數(shù)
};
// 初始化結(jié)構(gòu)體
bool initSequentialList(SequentialList& s) {
// 如果之前已有數(shù)組,則釋放之前的數(shù)組
if (s.length >= 0) {
delete s.base;
}
// 初始化一個(gè)長(zhǎng)度為size數(shù)組
int size;
cout << "輸入順序表的長(zhǎng)度:";
cin >> size;
s.base = new int[size];
s.length = size;
s.count = 0;
// 清理屏幕
system("cls||clear");
// 判斷初始化成功或失敗
if (s.base != NULL) {
return true;
}else {
return false;
}
}
// 打印順序表各個(gè)元素
void printItems(SequentialList& s) {
// 清理屏幕
system("cls||clear");
if (s.count > 0) {
string temp(10, '-');
cout << temp + "打印順序表" + temp << endl;
for (int i = 0; i < s.count; i++) {
cout << "[" + to_string(i) + "]" + to_string(s.base[i]) << endl;
}
}
else {
cout << "無(wú)元素打印!" << endl;
}
}
// 頭插
bool topInsert(SequentialList& s, int item) {
// 清理屏幕
system("cls||clear");
// 表滿 操作失敗
if (s.count == s.length) {
return false;
}
if (s.count > 0) {
// 所有元素向后移動(dòng)1位
for (int i = s.count - 1; i >= 0; i--) {
s.base[i + 1] = s.base[i];
}
}
// 如果count為0 正常插入元素
s.base[0] = item;
s.count ++;
return true;
}
// 尾插
bool bottomInsert(SequentialList& s, int item) {
// 清理屏幕
system("cls||clear");
if (s.count == s.length) { // 表滿->結(jié)束
return false;
}else { // 沒(méi)滿直接尾插
s.base[s.count] = item;
s.count++;
return true;
}
}
// 指定位置插入
bool indexInsert(SequentialList& s, int index, int item) {
// 清理屏幕
system("cls||clear");
// 判斷下標(biāo)是否給錯(cuò) 或者 表滿 -> 結(jié)束
if (0 > index || index > s.count || s.length == s.count) {
return false;
}
if (index == 0) {
// 調(diào)用頭插
topInsert(s, item);
}else if (index == s.count) {
// 調(diào)用尾插
bottomInsert(s, item);
}else {
// index以及后面的所有元素 向后移動(dòng)1位
for (int i = s.count - 1; i >= index; i--) {
s.base[i + 1] = s.base[i];
}
//插入操作
s.base[index] = item;
s.count++;
}
return true;
}
// 頭刪
bool topDelete(SequentialList& s) {
// 清理屏幕
system("cls||clear");
// 如果沒(méi)元素 -> 結(jié)束
if (s.count == 0) {
return false;
}
// 元素個(gè)數(shù)大于1 所有元素向前移動(dòng)1位
if (s.count > 1) {
for (int i = 0; i < s.count - 1; i++) {
s.base[i] = s.base[i + 1];
}
}
s.count--;
return true;
}
// 尾刪
bool bottomDelete(SequentialList& s) {
// 清理屏幕
system("cls||clear");
// 如果沒(méi)元素 -> 結(jié)束
if (s.count == 0) {
return false;
}
// 偽刪除
s.count--;
return true;
}
// 刪除指定位置的元素
bool indexDelete(SequentialList& s, int index) {
// 清理屏幕
system("cls||clear");
// 沒(méi)元素 或 給錯(cuò)下標(biāo) -> 結(jié)束
if (s.count == 0 || index < 0 || index >= s.count ) {
return false;
}
if (index == 0) {
// 調(diào)用頭刪
topDelete(s);
}else if (index == s.count) {
// 調(diào)用尾刪
bottomDelete(s);
}else {
// index后面的元素向前覆蓋
for (int i = index; i < s.count - 1; i++) {
s.base[i] = s.base[i + 1];
}
s.count--;
}
return true;
}
// 查找某元素在順序表的位置
void findElement(SequentialList& s, int item) {
// 清理屏幕
system("cls||clear");
// -1為找不到
int count = 0;
// 匹配
for (int i = 0; i < s.count; i++) {
if (s.base[i] == item) {
count++;
cout << "找到第" + to_string(count) + "個(gè)"+to_string(item)+"的下標(biāo)為["+to_string(i)+"]" << endl;
}
}
if (count == 0) {
cout << "未找到" + to_string(item) + "元素!" << endl;
}
}
// 打印菜單
void printMenu(){
string temp(10, '-');
cout << endl;
cout << temp+"操作菜單"+temp << endl;
cout << "[1]建立一個(gè)順序表" << endl;
cout << "[2]打印順序表" << endl;
cout << "[3]向順序表[頭部]插入新元素" << endl;
cout << "[4]向順序表[尾部]插入新元素" << endl;
cout << "[5]向順序[指定位置]部插入新元素" << endl;
cout << "[6]刪除順序表[頭部]的元素" << endl;
cout << "[7]刪除順序表[尾部]的元素" << endl;
cout << "[8]刪除順序表[指定位置]的元素" << endl;
cout << "[9]查找某元素在順序表的位置" << endl;
cout << "[0]退出操作" << endl;
cout << temp+temp+temp << endl;
}
// 函數(shù)主入口
int main() {
int options; // 選項(xiàng)
int flag = true; // while循環(huán)標(biāo)記
SequentialList s; // 順序表結(jié)構(gòu)體變量
int newItem; // 新元素
int index; // 插入|刪除元素的下標(biāo)
while (flag) {
printMenu();
cout << "請(qǐng)操作:" ;
cin >> options;
switch (options) {
case 1:
if (initSequentialList(s)) {
cout << "\t初始化成功" << endl;
}
else {
cout << "\t初始化失敗" << endl;
}
break;
case 2:
printItems(s);
break;
case 3:
cout << "新元素:";
cin >> newItem;
if (topInsert(s, newItem)) {
cout << "頭部成功插入("+ to_string(newItem)+")" << endl;
}
else {
cout << "順序表已滿,頭部插入操作失敗!!!" << endl;
}
break;
case 4:
cout << "新元素:";
cin >> newItem;
if (bottomInsert(s, newItem)) {
cout << "尾部成功插入(" + to_string(newItem) + ")!!!" << endl;
}
else {
cout << "順序表已滿,尾部插入操作失敗!!!" << endl;
}
break;
case 5:
cout << "新元素:";
cin >> newItem;
cout << "插入位置:";
cin >> index;
if (indexInsert(s, index, newItem)) {
cout << "在["+to_string(index) + "]成功插入(" + to_string(newItem) + ")!!!" << endl;
}
else {
cout << "插入位置錯(cuò)誤或順序表已滿,操作失敗!!!" << endl;
}
break;
case 6:
if (topDelete(s)) {
cout << "頭部元素刪除成功!!!" << endl;
}
else {
cout << "頭部元素刪除操作失敗!!!" << endl;
}
break;
case 7:
if (bottomDelete(s)) {
cout << "尾部元素刪除成功!!!" << endl;
}
else {
cout << "尾部元素刪除操作失敗!!!" << endl;
}
break;
case 8:
cout << "刪除位置:";
cin >> index;
if (indexDelete(s, index)) {
cout <<"刪除[" + to_string(index) + "]元素成功!!!" << endl;
}
else {
cout << "刪除位置錯(cuò)誤或順序表為空,操作失敗!!!" << endl;
}
break;
case 9:
cout << "要查找的元素:";
cin >> newItem;
findElement(s, newItem);
break;
case 0:
// 清理屏幕
system("cls||clear");
flag = false;
cout << "---本次操作結(jié)束---" << endl;
break;
default:
cout << "請(qǐng)輸入正確的序號(hào)!!!" << endl;
break;
}
}
return 0;
}
運(yùn)行結(jié)果:
原文鏈接:https://blog.csdn.net/weixin_44864260/article/details/127404654
相關(guān)推薦
- 2022-06-22 使用docker?compose一鍵部署WordPress博客的方法_docker
- 2022-04-22 Golang開(kāi)發(fā)中常見(jiàn)錯(cuò)誤以及注釋、規(guī)范代碼風(fēng)格
- 2022-04-29 C++?雙向循環(huán)鏈表類模版實(shí)例詳解_C 語(yǔ)言
- 2022-09-24 python實(shí)現(xiàn)梯度下降求解邏輯回歸_python
- 2022-05-08 利用Pandas讀取某列某行數(shù)據(jù)之loc和iloc用法總結(jié)_python
- 2022-10-25 如何利用Python和matplotlib更改縱橫坐標(biāo)刻度顏色_python
- 2022-01-12 nvm-windows使用與避坑指南,npm沒(méi)反應(yīng)也不報(bào)錯(cuò)怎么辦
- 2022-10-20 C#?Random類隨機(jī)函數(shù)實(shí)例詳解_C#教程
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支