C++中單鏈表操作的示例代碼
任務(wù)要求
實(shí)現(xiàn)單鏈表的下列功能:
1、 從鍵盤輸入一組數(shù)據(jù),創(chuàng)建單鏈表;
2、 輸出單鏈表;
3、 插入元素,給出插入成功或失敗的信息;
4、 頭插,給出插入成功或失敗的信息;
5、 尾插,給出插入成功或失敗的信息;
6、 刪除元素,給出插入成功或失敗的信息;
7、 頭刪,給出插入成功或失敗的信息;
8、 尾刪,給出插入成功或失敗的信息;
9、 打印鏈表長度。
代碼
#include <iostream>
#include <string>
using namespace std;
// 定義結(jié)構(gòu)體
typedef struct Node {
int data; // 數(shù)據(jù)域
Node* next; // 指針域
}Node, *LinkList;
// 打印菜單
void printMenu() {
string temp(10, '-');
cout << temp + temp + temp << endl;
cout << "\t單鏈表操作界面\n";
cout << "\t 1.創(chuàng)建單鏈表\n";
cout << "\t 2.打印單鏈表\n";
cout << "\t 3.插入結(jié)點(diǎn)(指定位置)\n";
cout << "\t 4.插入結(jié)點(diǎn)(頭插)\n";
cout << "\t 5.插入結(jié)點(diǎn)(尾插)\n";
cout << "\t 6.刪除結(jié)點(diǎn)(指定位置)\n";
cout << "\t 7.刪除結(jié)點(diǎn)(頭刪)\n";
cout << "\t 8.刪除結(jié)點(diǎn)(尾刪)\n";
cout << "\t 9.統(tǒng)計(jì)鏈表長度\n";
cout << temp + temp + temp << endl;
}
// 初始化鏈表
bool initLinkList(LinkList& L) {
// 獲取要初始化的長度
cout << "你要初始化的單鏈表長度:";
int count;
cin >> count;
// 初始化頭結(jié)點(diǎn) 頭結(jié)點(diǎn)的數(shù)據(jù)域存放數(shù)據(jù)個(gè)數(shù)
L = new Node;
L->next = NULL;
L->data = count;
// 根據(jù)count 插入數(shù)據(jù)(尾插法)
Node* p = L;
Node* temp;
for (int i = 0; i < count; i++) {
temp = new Node;
cout << "請輸入第" + to_string(i+1) + "個(gè)數(shù)據(jù):";
cin >> temp->data;
temp->next = NULL;
p->next = temp;
p = p->next;
}
return true;
}
// 打印鏈表
bool printLinkList(LinkList& L) {
// 判斷是否初始化
if (L == NULL) {
cout << "未初始化單鏈表"<<endl ;
return false;
}
// 從首元結(jié)點(diǎn)往下一一打印數(shù)據(jù)
Node* temp = L->next;
while (temp != NULL) {
cout << to_string(temp->data) + " -> ";
temp = temp->next;
}
// 補(bǔ)一個(gè)NULL
cout << "NULL" << endl;
return true;
}
// 指定位置插入(index范圍 1 - n)
bool locationInsert(LinkList& L) {
// 判斷是否初始化
if (L == NULL) {
cout << "未初始化單鏈表" << endl;
return false;
}
// 獲取要插入的位置 范圍判斷->輸入到對為止
int index;
while (true) {
cout << "輸入要插入的位置(1~n):";
cin >> index;
if (index <= L->data + 1 && index >= 1)break;
cout << "輸入的位置有誤" <<endl;
};
// 新建節(jié)點(diǎn) 獲取數(shù)據(jù)存放到數(shù)據(jù)域
Node* temp = new Node;
cout << "輸入要插入的數(shù)據(jù):";
cin >> temp->data;
// p指針向后移動 移動到要插入位置的前一個(gè)節(jié)點(diǎn)
Node* p = L;
for (int i = 1; i < index; i++) {
p = p->next;
}
// 插入操作
temp->next = p->next;
p->next = temp;
// 數(shù)據(jù)個(gè)數(shù)+1
L->data += 1;
return true;
}
// 頭插
bool headInsert(LinkList& L) {
// 判斷是否初始化
if (L == NULL) {
cout << "未初始化單鏈表" << endl;
return false;
}
// 新建節(jié)點(diǎn) 獲取數(shù)據(jù)存放到數(shù)據(jù)域
Node* temp = new Node;
cout << "輸入要插入的數(shù)據(jù):";
cin >> temp->data;
// 頭插操作
temp->next = L->next;
L->next = temp;
// 數(shù)據(jù)個(gè)數(shù)+1
L->data += 1;
return true;
}
// 尾插
bool tailInsert(LinkList& L) {
// 判斷是否初始化
if (L == NULL) {
cout << "未初始化單鏈表" << endl;
return false;
}
// 新建節(jié)點(diǎn) 獲取數(shù)據(jù)存放到數(shù)據(jù)域
Node* temp = new Node;
cout << "輸入要插入的數(shù)據(jù):";
cin >> temp->data;
// p指針移動到最后一個(gè)節(jié)點(diǎn)
Node* p = L->next;
while (p->next != NULL){
p = p->next;
}
// 尾插操作
p->next = temp;
temp->next = NULL;
// 數(shù)據(jù)個(gè)數(shù)+1
L->data += 1;
return true;
}
// 頭刪
bool headDelete(LinkList& L) {
// 判斷是否有元素
if (L->data < 1) {
cout << "單鏈表沒有元素" << endl;
return false;
}
// 頭刪操作
if (L->data > 1) { // 有多個(gè)數(shù)據(jù)節(jié)點(diǎn)
Node* temp = L->next;
L->next = L->next->next;
delete temp;
}else { // 只有一個(gè)首元結(jié)點(diǎn)
delete L->next;
L->next = NULL;
}
// 數(shù)據(jù)個(gè)數(shù)-1
L->data -= 1;
return true;
}
// 尾刪
bool tailDelete(LinkList& L) {
// 判斷是否有元素
if (L->data < 1) {
cout << "單鏈表沒有元素" << endl;
return false;
}
// 尾刪操作
if (L->data > 1) { // 有多個(gè)數(shù)據(jù)節(jié)點(diǎn)
// 移動p指針到尾結(jié)點(diǎn)的前一個(gè)節(jié)點(diǎn)
Node* p = L;
for (int i = 0; i < L->data-1 ; i++) {
p = p->next;
}
// 刪除操作
delete p->next;
p->next = NULL;
}else { // 只有一個(gè)首元結(jié)點(diǎn)
delete L->next;
L->next = NULL;
}
// 數(shù)據(jù)個(gè)數(shù)-1
L->data -= 1;
return true;
}
// 指定位置刪除(index范圍 1 - n)
bool locationDelete(LinkList& L) {
// 判斷是否有元素
if (L->data < 1) {
cout << "單鏈表沒有元素" << endl;
return false;
}
// 獲取要?jiǎng)h除的位置 范圍判斷->輸入到對為止
int index;
while (true) {
cout << "輸入要?jiǎng)h除的位置(1~n):";
cin >> index;
if (index <= L->data && index >= 1)break;
cout << "輸入的位置有誤" << endl;
};
if (index == 1) { // 頭刪
headDelete(L);
}else if (index == L->data) { // 尾刪
tailDelete(L);
}else { // 中間刪
// 將p指針移動到要?jiǎng)h除節(jié)點(diǎn)的前一個(gè)節(jié)點(diǎn)
Node* p = L;
for (int i = 1; i < index; i++) {
p = p->next;
}
// 刪除操作
Node* temp = p->next;
p->next = p->next->next;
delete temp;
// 數(shù)據(jù)個(gè)數(shù)-1
L->data -= 1;
}
return true;
}
// 打印鏈表長度
bool printLength(LinkList& L) {
// 判斷是否有元素
if (L == NULL) {
cout << "未初始化單鏈表" << endl;
return false;
}
// 打印頭結(jié)點(diǎn)的數(shù)據(jù)
cout << "鏈表長度為[" + to_string(L->data) + "]" << endl;
return true;
}
// 程序主入口
int main() {
bool flag = true; // 標(biāo)記
int option; // 操作選項(xiàng) [0 - 9]
LinkList L = NULL; // 初始化鏈表指針為NULL
printMenu(); // 打印菜單
while (flag) {
cout << "請操作:";
cin >> option;
switch (option) {
case 1: // 初始化
initLinkList(L);
printLinkList(L);
break;
case 2: // 打印菜單
printLinkList(L);
break;
case 3: // 指定位置插入
locationInsert(L);
printLinkList(L);
break;
case 4: // 頭插
headInsert(L);
printLinkList(L);
break;
case 5: // 尾插
tailInsert(L);
printLinkList(L);
break;
case 6: // 指定位置刪除
locationDelete(L);
printLinkList(L);
break;
case 7: // 頭刪
headDelete(L);
printLinkList(L);
break;
case 8: // 尾刪
tailDelete(L);
printLinkList(L);
break;
case 9: // 打印鏈表長度
printLength(L);
printLinkList(L);
break;
case 0: // 退出
cout << "成功退出程序!" << endl;
flag = false;
break;
default:// 其他情況
cout << "操作有誤,重新輸入!" << endl;
}
}
return 0;
}
運(yùn)行結(jié)果

到此這篇關(guān)于C++中單鏈表操作的示例代碼的文章就介紹到這了,更多相關(guān)C++單鏈表內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
QT中QTableWidget加載大量數(shù)據(jù)不卡頓的解決
本文主要介紹了QT中QTableWidget加載大量數(shù)據(jù)不卡頓的解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07
C語言雙指針多方法旋轉(zhuǎn)數(shù)組解題LeetCode
這篇文章主要為大家介紹了C語言雙指針使用多方法旋轉(zhuǎn)數(shù)組題解LeetCode,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-02-02
win10系統(tǒng)VS2019配置點(diǎn)云庫PCL1.12.1的詳細(xì)流程
這篇文章主要介紹了win10系統(tǒng)VS2019配置點(diǎn)云庫PCL1.12.1的教程與經(jīng)驗(yàn)總結(jié),本文記錄小白在配置過程中踩過的一些小坑,需要的朋友可以參考下2022-07-07
C語言中關(guān)于sizeof 和 strlen的區(qū)別分析
本文通過示例簡單分析了4種情況下C語言中sizeof 和 strlen的區(qū)別,算是個(gè)人經(jīng)驗(yàn)的一個(gè)小小的總結(jié),如有遺漏還請大家告知。2015-02-02
詳解VS2019+OpenCV-4-1-0+OpenCV-contrib-4-1-0
這篇文章主要介紹了詳解VS2019+OpenCV-4-1-0+OpenCV-contrib-4-1-0,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04

