C++中順序表操作的示例代碼
編寫程序,實(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é)果:

到此這篇關(guān)于C++中順序表操作的示例代碼的文章就介紹到這了,更多相關(guān)C++順序表操作內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Visual Studio2019調(diào)試DLL的實(shí)現(xiàn)
本文主要介紹了Visual Studio2019調(diào)試DLL的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2025-01-01
C語(yǔ)言植物大戰(zhàn)數(shù)據(jù)結(jié)構(gòu)快速排序圖文示例
這篇文章主要為大家介紹了C語(yǔ)言植物大戰(zhàn)數(shù)據(jù)結(jié)構(gòu)快速排序圖文示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05
C++實(shí)現(xiàn)LeetCode(20.驗(yàn)證括號(hào))
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(20.驗(yàn)證括號(hào)),本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07
探究在C++程序并發(fā)時(shí)保護(hù)共享數(shù)據(jù)的問(wèn)題
這篇文章主要介紹了探究在C++程序并發(fā)時(shí)保護(hù)共享數(shù)據(jù)的問(wèn)題,也有利于大家更好地理解C++多線程的一些機(jī)制,需要的朋友可以參考下2015-07-07
C++圖形界面開發(fā)Qt教程:嵌套圓環(huán)示例
這篇文章主要介紹了C++實(shí)現(xiàn)圖形界面開發(fā)Qt教程,涉及坐標(biāo)函數(shù)的應(yīng)用及圖形界面程序設(shè)計(jì),需要的朋友可以參考下,希望能給你帶來(lái)幫助2021-08-08
C/C++?Qt?運(yùn)用JSON解析庫(kù)的實(shí)例代碼
這篇文章主要介紹了C/C++?Qt?運(yùn)用JSON解析庫(kù)的相關(guān)知識(shí),通過(guò)代碼依次解析這個(gè)json文件中的每一個(gè)參數(shù),代碼簡(jiǎn)單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-01-01
C++使用HDF5庫(kù)實(shí)現(xiàn)將h5圖像轉(zhuǎn)為tif格式
這篇文章主要為大家詳細(xì)介紹了C++ 語(yǔ)言如何通過(guò)hdf5庫(kù)與gdal庫(kù)實(shí)現(xiàn)將.h5格式的多波段HDF5圖像批量轉(zhuǎn)換為.tif格式,有需要的可以參考一下2024-12-12
C/C++ 中memset() 函數(shù)詳解及其作用介紹
這篇文章主要介紹了C/C++ 中memset() 函數(shù)詳解及其作用介紹,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
C++類和對(duì)象之類的6個(gè)默認(rèn)成員函數(shù)詳解
類是對(duì)某一事物的抽象描述,具體地講類是C++中的一種構(gòu)造的數(shù)據(jù)類型,下面這篇文章主要給大家介紹了關(guān)于C++類和對(duì)象之類的6個(gè)默認(rèn)成員函數(shù)的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-02-02

