欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++中順序表操作的示例代碼

 更新時(shí)間:2022年10月19日 14:06:55   作者:我不是禿頭sheep  
這篇文章主要為大家詳細(xì)介紹了C++中順序表的基礎(chǔ)操作的相關(guān)代碼,主要有順序表的輸出、插入和刪除數(shù)據(jù)等,感興趣的小伙伴可以了解一下

編寫程序,實(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 {	// 沒滿直接尾插
		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");
	// 如果沒元素 -> 結(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");
	// 如果沒元素 -> 結(jié)束
	if (s.count == 0) {
		return false;
	}
	// 偽刪除
	s.count--;
	return true;
}

// 刪除指定位置的元素
bool indexDelete(SequentialList& s, int index) {
	// 清理屏幕
	system("cls||clear");
	// 沒元素 或 給錯(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)文章

  • C語(yǔ)言求逆矩陣案例詳解

    C語(yǔ)言求逆矩陣案例詳解

    這篇文章主要介紹了C語(yǔ)言求逆矩陣案例詳解,本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • C語(yǔ)言零基礎(chǔ)精通變量與常量

    C語(yǔ)言零基礎(chǔ)精通變量與常量

    這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言的變量和常量,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助
    2022-04-04
  • 淺談#ifndef,#define,#endif的作用和用法

    淺談#ifndef,#define,#endif的作用和用法

    下面小編就為大家?guī)?lái)一篇淺談#ifndef,#define,#endif的作用和用法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧
    2016-12-12
  • rapidjson解析json代碼實(shí)例以及常見的json core dump問題

    rapidjson解析json代碼實(shí)例以及常見的json core dump問題

    今天小編就為大家分享一篇關(guān)于rapidjson解析json代碼實(shí)例以及常見的json core dump問題,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2019-04-04
  • C語(yǔ)言字符串快速壓縮算法代碼

    C語(yǔ)言字符串快速壓縮算法代碼

    這篇文章主要介紹了C語(yǔ)言字符串快速壓縮算法代碼,將字符串中連續(xù)出席的重復(fù)字母進(jìn)行壓縮,其主要的壓縮字段的格式為”字符重復(fù)的次數(shù)+字符”。有需要的小伙伴參考下吧。
    2015-03-03
  • C語(yǔ)言入門篇--學(xué)習(xí)選擇,if,switch語(yǔ)句以及代碼塊

    C語(yǔ)言入門篇--學(xué)習(xí)選擇,if,switch語(yǔ)句以及代碼塊

    本篇文章是基礎(chǔ)篇,適合c語(yǔ)言剛?cè)腴T的朋友,本文主要帶大家學(xué)習(xí)一下C語(yǔ)言的選擇,if,switch語(yǔ)句及代碼塊,幫助大家快速入門c語(yǔ)言的世界,更好的理解c語(yǔ)言
    2021-08-08
  • C語(yǔ)言中dlopen和dlsym的使用方式詳解

    C語(yǔ)言中dlopen和dlsym的使用方式詳解

    這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言中dlopen和dlsym的使用方式,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助
    2022-03-03
  • C++中雙冒號(hào)::的作用淺析

    C++中雙冒號(hào)::的作用淺析

    在C++中經(jīng)常使用雙冒號(hào)::,很多朋友不知道是什么意思,這篇文章主要介紹了C++中雙冒號(hào)::的作用,需要的朋友可以參考下
    2018-06-06
  • C語(yǔ)言判斷一個(gè)數(shù)是否為素?cái)?shù)方法解析

    C語(yǔ)言判斷一個(gè)數(shù)是否為素?cái)?shù)方法解析

    這篇文章主要介紹了C語(yǔ)言判斷一個(gè)數(shù)是否為素?cái)?shù)方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • C語(yǔ)言實(shí)現(xiàn)飛機(jī)訂票系統(tǒng)的完整代碼

    C語(yǔ)言實(shí)現(xiàn)飛機(jī)訂票系統(tǒng)的完整代碼

    為了免去在窗口排隊(duì)買票的麻煩,飛機(jī)訂票系統(tǒng)應(yīng)運(yùn)而生,下面這篇文章主要給大家介紹了關(guān)于C語(yǔ)言實(shí)現(xiàn)飛機(jī)訂票系統(tǒng)的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-06-06

最新評(píng)論