生活随笔
收集整理的這篇文章主要介紹了
实验一 线性表、堆栈和队列的操作与实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
前言
記錄實驗,同時也是記錄常見數據結構算法的實現。
廣州大學學生實驗報告
開課實驗室:計算機科學與工程實驗(電子樓418A)
學院 計算機科學與網絡工程學院
實驗課程 數據結構實驗
實驗項目 實驗一 線性表、堆棧和隊列的操作與實現
- 一、實驗目的:
1、線性表的鏈表實現:遍歷、查找、插入、刪除、翻轉
2、棧的鏈式存儲結構實現:入棧、出棧
3、隊列的鏈式存儲結構的實現:入隊、出隊
4、線性表、棧和隊列的應用實現 - 二、使用儀器、器材
微機一臺
操作系統:WinXP
編程軟件:C++ - 三、實驗內容及原理
1、線性表的鏈表實現:(返回目錄🖱)
#include <iostream>
#include <ctime>
#include <iomanip>
using namespace std
;typedef struct LNode
{int data
;LNode
* next
;
}LNode
, * LinkList
;
void Insert(LinkList
&l
, LNode
* new_node
, int n
)
{while (1){if (n
== 0){cout
<< "0處是頭結點,請重新輸入你想插入整數的位置\t位置: ";cin
>> n
;}else if (n
> 11 || n
< 1){cout
<< "輸入的位置超出鏈表的長度,請重新輸入...\t位置: ";cin
>> n
;}else break;}LNode
* p
= new LNode
;p
= l
;if (p
== NULL){cout
<< "空表,退出程序......";exit(0);}int i
= 0;while (i
< n
- 1 && p
){p
= p
->next
;++i
;}new_node
->next
= p
->next
;p
->next
= new_node
;cout
<< "在 " << setw(2) << n
<< " 處插入值為 " << new_node
->data
<< " 的節點" << endl
;
}
void List_show(LinkList
& l
)
{if (!(l
->next
)){cout
<< "出錯,此鏈表為空鏈表,退出程序......";exit(0);}else{LNode
* p
= new LNode
;p
= l
->next
;cout
<< "鏈表為:";for (int i
= 1; p
; i
++){if (p
->next
)cout
<< p
->data
<< "——";else cout
<< p
->data
<< "\t長度為:" << i
<< endl
;p
= p
->next
;}}
}
void Search_in_list(LinkList
& l
)
{int element
;cout
<< "請輸入你想查找的整數: " << endl
;cout
<< "————若想退出查找,請輸入0————" << endl
;LNode
* p
= new LNode
;p
= l
->next
;if (!p
){cout
<< "出錯,此鏈表為空鏈表,退出程序......";exit(0);}else{while (cin
>> element
){if (element
== 0){cout
<< "退出查找......" << endl
;break;}else{int index_not_in
= 0; for (int i
= 1; p
; i
++){if (p
->data
== element
){cout
<< "該整數在表中,且位置為" << i
<< endl
;cout
<< "請再次輸入你想查找的整數: " << endl
;cout
<< "————若想退出查找,請輸入0————" << endl
;}else ++index_not_in
;p
= p
->next
;if (i
== index_not_in
&& p
== NULL) {cout
<< "整數" << element
<< "不在此鏈表中" << endl
;cout
<< "請再次輸入你想查找的整數: " << endl
;cout
<< "————若想退出查找,請輸入0————" << endl
;}}}p
= l
->next
;}}
}
void Delete(LinkList
& l
)
{int n
;cin
>> n
;while (1){if (n
== 0){cout
<< "0處是頭結點,請重新輸入你想刪除的結點的位置\t位置: ";cin
>> n
;}else if (n
> 10 || n
< 1){cout
<< "輸入的位置超出鏈表的長度,請重新輸入...\t位置: ";cin
>> n
;}else{cout
<< endl
;break;}}LNode
* p
= new LNode
;p
= l
;int i
= 0;if (p
->next
== NULL){cout
<< "出現錯誤,退出程序......";exit(0);}while (i
< n
- 1 && p
){p
= p
->next
;++i
;}LNode
* temp_p
= p
->next
;p
->next
= p
->next
->next
;cout
<< "在 " << setw(2) << n
<< " 處刪除值為 " << temp_p
->data
<< " 的節點" << endl
;delete temp_p
;
}
void Reverse(LinkList
& l
)
{LNode
* p
= new LNode
;p
= l
;p
= p
->next
; if (p
== NULL){cout
<< "該鏈表為空鏈表,退出程序......";exit(0);}LNode
* p_backward
, * p_forward
;p_backward
= NULL;while (p
){p_forward
= p
->next
;p
->next
= p_backward
;p_backward
= p
;p
= p_forward
;}l
->next
= p_backward
;cout
<< "已翻轉成功" << endl
;
}int main()
{srand((unsigned(time
)));LinkList l
; l
= new LNode
; l
->next
= NULL; cout
<< "(1)" << endl
;for (int i
= 1; i
<= 10; i
++){int random_int
= rand() % 900 + 100;LNode
* p
= new LNode
;p
->data
= random_int
;Insert(l
, p
, i
);}cout
<< "(2)" << endl
;List_show(l
);cout
<< "(3)" << endl
;Search_in_list(l
);cout
<< "(4)" << endl
;LNode
* p1
= new LNode
;int location
;p1
->data
= 111; cout
<< "你想在鏈表的哪個位置插入新的元素?\t位置: ";cin
>> location
;Insert(l
, p1
, location
);List_show(l
);cout
<< "(5)" << endl
;LNode
* p2
= new LNode
;p2
->data
= 111; cout
<< "你想刪除哪個位置的結點?\t位置: ";Delete(l
);List_show(l
);cout
<< "(6)" << endl
;Reverse(l
);List_show(l
);
}
2、棧的鏈式存儲結構實現(返回目錄🖱)
#include <iostream>
#include <ctime>
#include <iomanip>
using namespace std
;typedef struct LNode
{int length
;int data
;LNode
* next
;
}LNode
, * LinkStack
;void InitStack(LinkStack
&s
)
{s
->next
= NULL;s
->length
= 0;cout
<< "鏈棧初始化成功,長度為0......" << endl
;
}
void Push(LinkStack
& s
, int data
)
{LNode
* p
= new LNode
;p
->data
= data
;p
->next
= s
->next
;s
->next
= p
;s
->length
++;cout
<< "在位置" << setw(3) << s
->length
<< "添加元素" << p
->data
<< endl
;
}void Pop(LinkStack
& s
)
{LNode
* p
= new LNode
;p
= s
->next
;s
->next
= p
->next
;s
->length
--;cout
<< "從棧中彈出元素 " << p
->data
<<setw(20)<<"深度為"<<s
->length
<< endl
;delete p
;
}
void ShowListStack(LinkStack
& s
)
{if (!s
->next
){cout
<< "棧空......退出程序......"<<endl
;exit(0);}else{LNode
* p
= new LNode
;p
= s
->next
;cout
<< "鏈棧為:" << endl
;;for (int i
= 1; p
; i
++){if (p
->next
)cout
<<"| " << p
->data
<< " |\n"<<endl
;else cout
<< "| " << p
->data
<< " |\n"<<"-------\t"<<"深度為:" << i
<< endl
;p
= p
->next
;}}
}int main()
{srand(unsigned(time
));LinkStack s
;s
= new LNode
;InitStack(s
);cout
<< "(1)\n";for (int i
= 0; i
< 10; i
++)Push(s
, rand() % 900 + 100);ShowListStack(s
);cout
<< "(2)\n";for (int i
= 0; i
< 10; i
++)Pop(s
);ShowListStack(s
);
}
3、隊列的鏈式存儲結構的實現(返回目錄🖱)
#include <iostream>
#include <ctime>
#include <iomanip>
using namespace std
;
typedef struct LQNode
{int data
;LQNode
* next
;
}LQNode
, * LQNodeptr
;
typedef struct LinkQueue
{LQNodeptr head
;LQNodeptr rear
;
}LinkQueue
;
void InitLQ(LinkQueue
&lq
)
{lq
.head
= lq
.rear
= new LQNode
; lq
.head
->next
= lq
.rear
->next
= NULL;
}
void Insert(LinkQueue
&lq
, int data
)
{LQNode
* p
= new LQNode
;p
->data
= data
;p
->next
= NULL;lq
.rear
->next
= p
;lq
.rear
= p
;cout
<< "在隊尾加入結點: " << data
<< endl
;
}
void ShowLQ(LinkQueue
&lq
)
{LQNode
* p
= new LQNode
;p
= lq
.head
->next
;cout
<< "鏈隊:";int length
= 0;while (p
){++length
;if (p
!= lq
.rear
)cout
<< p
->data
<< "—";else cout
<< p
->data
<< "\t長度為:"<<length
<<endl
;p
= p
->next
;}
}
void Reverse(LinkQueue
&lq
)
{LQNode
* p
= new LQNode
; LQNode
* q
= new LQNode
;LQNode
* r
= new LQNode
;LQNode
* p1
= new LQNode
;p
= lq
.head
; p1
= p
->next
; q
= p
->next
;lq
.head
->next
= NULL;while (q
){r
= q
->next
;q
->next
= p
;p
= q
;q
= r
;}lq
.rear
= p1
;p1
->next
= NULL;lq
.head
->next
= p
;
}
int main()
{srand(unsigned(time
));LinkQueue lq
;InitLQ(lq
);InitLQ(lq
);for (int i
= 0; i
< 10; i
++){Insert(lq
, rand() % 900 + 100);}ShowLQ(lq
);Reverse(lq
);ShowLQ(lq
);
}
4、線性表、棧和隊列的應用實現:(返回目錄🖱)
(1) 用隨機函數生成10個3位整數(100~999),把這些整數存于單鏈表中,然后讀入一個整數,以該值為基準把單鏈表分割為兩部分,所有小于該值的結點排在大于或等于該值的結點之前。
#include <iostream>
#include <ctime>
#include <iomanip>
using namespace std
;
typedef struct LNode
{int data
;LNode
* next
;
}LNode
, * LinkList
;
typedef struct Stack
{LNode
* next
;int depth
;
} *LinkStack
;
void Insert_LinkList(LinkList
& l
, LNode
* new_node
, int n
)
{LNode
* p
= new LNode
;p
= l
;for (int i
= 1; i
< n
; i
++){p
= p
->next
;}new_node
->next
= p
->next
;p
->next
= new_node
;
}
void Show_LinkList(LinkList
& l
)
{if (!(l
->next
)){cout
<< "出錯,此鏈表為空鏈表,退出程序......";exit(0);}else{LNode
* p
= new LNode
;p
= l
->next
;cout
<< "鏈表為:";for (int i
= 1; p
; i
++){if (p
->next
)cout
<< p
->data
<< "——";else cout
<< p
->data
<< "\t長度為:" << i
<< endl
;p
= p
->next
;}}
}
void Push(LinkStack
& s
, int data
)
{LNode
* p
= new LNode
;p
->data
= data
;p
->next
= s
->next
;s
->next
= p
;s
->depth
++;
}
int Pop(LinkStack
& s
)
{LNode
* p
= new LNode
;p
= s
->next
;s
->next
= p
->next
;s
->depth
--;return p
->data
;
}
void ShowListStack(LinkStack
& s
)
{if (!s
->next
){cout
<< "棧空......退出程序......" << endl
;exit(0);}else{LNode
* p
= new LNode
;p
= s
->next
;cout
<< "鏈棧為:" << endl
;;for (int i
= 1; p
; i
++){if (p
->next
)cout
<< "| " << p
->data
<< " |\n" << endl
;else cout
<< "| " << p
->data
<< " |\n" << "-------\t" << "深度為:" << i
<< endl
;p
= p
->next
;}}
}
void Devide(LinkList
& l
,int n
)
{LinkStack s_big
= new Stack
;s_big
->next
= new LNode
;s_big
->next
= NULL;s_big
->depth
= 0;LinkStack s_small
= new Stack
;s_small
->next
= new LNode
;s_small
->next
= NULL;s_small
->depth
= 0;LNode
* p
= new LNode
;p
= l
->next
;if (p
==NULL || p
->next
==NULL){cout
<< "鏈表長度為0或1,無法分割...." << endl
;exit(0);}while (p
){int temp
= p
->data
;if (temp
>= n
){Push(s_big
, temp
);cout
<< "在元素 大 于"<<n
<<"的棧的位置" << setw(3) << s_big
->depth
<< "添加元素" << p
->data
<< endl
;p
= p
->next
;}else{Push(s_small
, temp
);cout
<< "在元素 小 于" << n
<< "的棧的位置" << setw(3) << s_small
->depth
<< "添加元素" << p
->data
<< endl
;p
= p
->next
;}}ShowListStack(s_big
);ShowListStack(s_small
);LNode
* p_
= new LNode
;p_
= l
->next
;while (s_small
->next
){p_
->data
= Pop(s_small
);cout
<< "從元素 小 于"<<n
<<"的棧中彈出元素 " << p_
->data
<< setw(20) << "此時 小 棧深度為" << s_small
->depth
<< endl
;p_
= p_
->next
;}while (s_big
->next
){p_
->data
= Pop(s_big
);cout
<< "從元素 大 于" << n
<< "的棧中彈出元素 " << p_
->data
<< setw(20) << "此時 大 棧深度為" << s_big
->depth
<< endl
;p_
= p_
->next
;}
}
int main()
{srand(unsigned(time
));LinkList l
= new LNode
;l
->next
= NULL;for (int i
= 0; i
< 10; i
++){LNode
* p
= new LNode
;p
->data
= rand() % 900 + 100;Insert_LinkList(l
, p
, i
+ 1);}Show_LinkList(l
);Devide(l
, 300);Show_LinkList(l
);
cout
<<"分割已完成。。。";
(2) 假設一個字符串中可以包含三種括號:( )[ ]{},且這三種括號可以按任意次序嵌套使用(如:“…[…{…}…[…]…]…(…)” 為合法嵌套,“…[…{… )…[…]…]…(…)”為不合法嵌套)。編寫判別給定表達式中所含括號是否正確配對出現的算法,如果是合法嵌套則返回為true,如果是不符合法嵌套則返回為false。
#include<iostream>
using namespace std
;typedef struct SNode
{char ch
;int depth
;SNode
* next
;
}SNode
, *Stack
;void InitStack(Stack
& s
)
{s
= new SNode
;s
->next
= NULL;s
->ch
= NULL;s
->depth
= 0;
}bool
StackEmpty(Stack s
)
{if (s
->next
== NULL){return true
;cout
<< "棧空..." << endl
;}else return false
;
}void Push(Stack
& s
, char ch
)
{SNode
* p
= new SNode
;p
->next
= NULL;p
->ch
= ch
;p
->next
= s
->next
;s
->next
= p
;s
->depth
++;cout
<< "在棧中放入字符 \"" << ch
<< "\" \t棧深度為:" << s
->depth
<< endl
;
}void Pop(Stack
& s
)
{SNode
* p
= new SNode
;p
= s
->next
;cout
<< "彈出元素" << p
->ch
<< endl
;s
->next
= p
->next
;delete p
;
}char GetTop(Stack s
)
{return s
->next
->ch
;
}bool
Matching()
{Stack s
;InitStack(s
);cout
<< "請鍵入字符...按#退出\t";char ch
;int flag
= 1;cin
>> ch
;while (ch
!='#'&& flag
){switch (ch
){case'(':case'[':case'{':{Push(s
, ch
);cout
<< "請繼續鍵入字符...按#退出\t";}; break;case')':if (!StackEmpty(s
) && GetTop(s
) == '(')Pop(s
);else{flag
= 0;cout
<< "括號" << ch
<< "不匹配";}; break;case']':if (!StackEmpty(s
) && GetTop(s
) == '[')Pop(s
);else{flag
= 0;cout
<< "括號" << ch
<< "不匹配";}; break;case'}':if (!StackEmpty(s
) && GetTop(s
) == '{')Pop(s
);else{flag
= 0;cout
<< "括號" << ch
<< "不匹配";}; break;}cin
>> ch
;}if (StackEmpty(s
) && flag
) return 1;else return 0;
}int main()
{if (Matching()) cout
<< "匹配成功";else cout
<< "匹配失敗";
}}
(3)用隊列求解迷宮問題的最短路徑
代碼雛形出來了,但是調試了四個小時還是沒解決那個關鍵bug……
總結
以上是生活随笔為你收集整理的实验一 线性表、堆栈和队列的操作与实现的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。