日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無(wú)先后,達(dá)者為師

網(wǎng)站首頁(yè) 編程語(yǔ)言 正文

C++中順序表操作的示例代碼_C 語(yǔ)言

作者:我不是禿頭sheep ? 更新時(shí)間: 2022-11-27 編程語(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

欄目分類
最近更新