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

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

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

編寫程序,實現(xiàn)順序表的下列功能:

  • 從鍵盤輸入數(shù)據(jù)建立一個順序表
  • 輸出該順序表
  • 往順序表中插入數(shù)據(jù)
  • 從順序表中刪除數(shù)據(jù)
  • 給定數(shù)據(jù),進行查找,給出查找成功和失敗信息

C++代碼:

#include <iostream>
#include <string> 
#include "windows.h"
using namespace std;

// 定義順序表結構體
struct SequentialList
{
	int* base;   // 順序表首地址
	int length;  // 順序表長度
	int count;	 // 順序表當前元素個數(shù)
};

// 初始化結構體
bool initSequentialList(SequentialList& s) {
	
	// 如果之前已有數(shù)組,則釋放之前的數(shù)組
	if (s.length >= 0) {
		delete s.base;
	}

	// 初始化一個長度為size數(shù)組
	int size;
	cout << "輸入順序表的長度:";
	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;
	}
}


// 打印順序表各個元素
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 << "無元素打印!" << endl;
	}
}

// 頭插
bool topInsert(SequentialList& s, int item) {
	// 清理屏幕
	system("cls||clear");
	// 表滿 操作失敗
	if (s.count == s.length) {
		return false;
	}
	if (s.count > 0) {
		// 所有元素向后移動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) { // 表滿->結束
		return false;
	}else {	// 沒滿直接尾插
		s.base[s.count] = item;
		s.count++;
		return true;
	}
}

// 指定位置插入
bool indexInsert(SequentialList& s, int index, int item) {
	// 清理屏幕
	system("cls||clear");
	// 判斷下標是否給錯 或者 表滿 -> 結束
	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以及后面的所有元素 向后移動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");
	// 如果沒元素 -> 結束
	if (s.count == 0) {
		return false;
	}
	// 元素個數(shù)大于1 所有元素向前移動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");
	// 如果沒元素 -> 結束
	if (s.count == 0) {
		return false;
	}
	// 偽刪除
	s.count--;
	return true;
}

// 刪除指定位置的元素
bool indexDelete(SequentialList& s, int index) {
	// 清理屏幕
	system("cls||clear");
	// 沒元素 或 給錯下標 -> 結束
	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) + "個"+to_string(item)+"的下標為["+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]建立一個順序表" << 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;			// 選項
	int flag = true;		// while循環(huán)標記
	SequentialList s;		// 順序表結構體變量
	int newItem;			// 新元素
	int index;				// 插入|刪除元素的下標
	while (flag) {
		printMenu();
		cout << "請操作:" ;
		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 << "插入位置錯誤或順序表已滿,操作失敗!!!" << 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 << "刪除位置錯誤或順序表為空,操作失敗!!!" << endl;
				}
				break;
			case 9:
				cout << "要查找的元素:";
				cin >> newItem;
				findElement(s, newItem);
				break;
			case 0:
				// 清理屏幕
				system("cls||clear");
				flag = false;
				cout << "---本次操作結束---" << endl;
				break;
			default:
				cout << "請輸入正確的序號!!!" << endl;
				break;
		}
	}
	return 0;
}

運行結果:

到此這篇關于C++中順序表操作的示例代碼的文章就介紹到這了,更多相關C++順序表操作內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • C語言求逆矩陣案例詳解

    C語言求逆矩陣案例詳解

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

    C語言零基礎精通變量與常量

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

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

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

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

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

    C語言字符串快速壓縮算法代碼

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

    C語言入門篇--學習選擇,if,switch語句以及代碼塊

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

    C語言中dlopen和dlsym的使用方式詳解

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

    C++中雙冒號::的作用淺析

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

    C語言判斷一個數(shù)是否為素數(shù)方法解析

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

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

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

最新評論