實驗三 棧和隊列的操作 (數據結構實驗C++編寫)

1.實驗目的

(1)掌握棧的順序存儲結構、鏈式存儲結構及其基本操作;
(2)掌握隊列的順序存儲結構、鏈式存儲結構及其基本操作。

2.實驗內容

(1)編程實現棧的以下基本操作:建棧,取棧頂元素,入棧,出棧。
(2)編程實現隊列的以下基本操作:建隊列,取隊頭元素,入隊,出隊。

3.實驗步驟

(1) 編寫程序框架,利用while循環輸入操作對象,利用switch選擇語句對輸入的代碼進行判斷,並進行相應操作。
(2) 編寫順序棧的相關操作。
(3)編寫鏈棧的相關操作。
(4)編寫循環隊列的相關操作。
(5)編寫鏈隊的相關操作。
(6)執行代碼,測試數據。

4.實驗代碼

#include <iostream>

using namespace std;
#define MAXSIZE 100
typedef int ElemType;
//順序棧的定義
typedef struct
{
    ElemType *base;
    ElemType *top;
    int stacksize;
}SqStack;

//鏈棧的定義
typedef struct Node
{
    ElemType data;
    struct Node *next;
}Node,*LinkStack,*QueuePtr;

//循環隊列的定義
typedef struct
{
    ElemType *base;
    int frontQ;
    int rearQ;
}SqQueue;

//鏈隊的定義
typedef struct
{
    QueuePtr frontQ;
    QueuePtr rearQ;
}LinkQueue;

void SqStackOpra();
void LinkStackOpra();
void SqQueueOpra();
void LinkQueueOpra();

void TipS(string str);
void TipQ(string str);
//創建順序棧
void InitStack(SqStack &S);
//順序棧入棧
void Push(SqStack &S,ElemType e);
//順序棧出棧
void Pop(SqStack &S,ElemType &e);
//取順序棧頂元素
void GetTop(SqStack S);
//顯示順序棧元素
void DisplayStack(SqStack S);

//創建鏈棧
void InitStack(LinkStack &S);
//鏈棧入棧
void Push(LinkStack &S,ElemType e);
//鏈棧出棧
void Pop(LinkStack &S,ElemType &e);
//取鏈棧頂元素
void GetTop(LinkStack S);
//顯示鏈棧元素
void DisplayStack(LinkStack S);

//創建循環隊列
void InitQueue(SqQueue &Q);
//循環隊列入隊
void EnQueue(SqQueue &Q,ElemType e);
//循環隊列出隊
void DeQueue(SqQueue &Q,ElemType e);
//取循環隊頭元素
void GetHead(SqQueue Q);
//顯示循環隊列元素
void DisplayStack(SqQueue S);

//創建鏈隊
void InitQueue(LinkQueue &Q);
//鏈隊入隊
void EnQueue(LinkQueue &Q,ElemType e);
//鏈隊出隊
void DeQueue(LinkQueue &Q,ElemType e);
//取鏈隊頭元素
void GetHead(LinkQueue Q);
//顯示鏈隊元素
void DisplayStack(LinkQueue S);

int main()
{
    char ch;
    cout << "a ---------------- 順序棧操作" << endl;
    cout << "b ---------------- 鏈棧操作" << endl;
    cout << "c ---------------- 循環隊列操作" << endl;
    cout << "d ---------------- 鏈隊操作" << endl;
    cout << "------------- 輸入其它退出程序!" << endl;
    do
    {
        cout << "請選擇操作對象:" ;
        cin >> ch;
        switch(ch)
        {
            case 'a':
                SqStackOpra();
                break;
            case 'b':
                LinkStackOpra();
                break;
            case 'c':
                SqQueueOpra();
                break;
            case 'd':
                LinkQueueOpra();
                break;
        }
    }while(ch=='a' || ch=='b' || ch=='c' || ch=='d');

    return 0;
}
void SqStackOpra()
{
    TipS("順序棧");
    int index;
    ElemType e;
    SqStack SS;
    do
    {
        cout << "請輸入操作代碼:";
        cin >> index;
        switch(index)
        {
            case 1:
                InitStack(SS);
                break;
            case 2:
                GetTop(SS);
                break;
            case 3:
                cout << "請輸入入棧元素的值:" ;
                cin >> e;
                Push(SS,e);
                break;
            case 4:
                Pop(SS,e);
                break;
            case 5:
                cout << "順序棧中的元素爲:";
                DisplayStack(SS);
                break;
        }
        cout << endl;
    }while(index > 0);
}
void LinkStackOpra()
{
    TipS("鏈棧");
    ElemType e;
    int index;
    LinkStack SL;
    do
    {
        cout << "請輸入操作代碼:";
        cin >> index;
        switch(index)
        {

            case 1:
                InitStack(SL);
                break;
            case 2:
                GetTop(SL);
                break;
            case 3:
                cout << "請輸入入棧元素的值:" ;
                cin >> e;
                Push(SL,e);
                break;
            case 4:
                Pop(SL,e);
                break;
            case 5:
                cout << "鏈棧中的元素爲:";
                DisplayStack(SL);
                break;
        }
    }while(index > 0);
}
void SqQueueOpra()
{
    TipQ("循環隊列");
    ElemType e;
    int index;
    SqQueue SQ;
    do
    {
        cout << "請輸入操作代碼:";
        cin >> index;
        switch(index)
        {
            case 1:
                InitQueue(SQ);
                break;
            case 2:
                GetHead(SQ);
                break;
            case 3:
                cout << "請輸入入隊元素的值:" ;
                cin >> e;
                EnQueue(SQ,e);
                break;
            case 4:
                DeQueue(SQ,e);
                break;
            case 5:
                cout << "循環隊列中的元素爲:";
                DisplayStack(SQ);
                break;
        }
    }while(index > 0);
}
void LinkQueueOpra()
{
    TipQ("鏈隊");
    ElemType e;
    int index;
    LinkQueue LQ;
    do
    {
        cout << "請輸入操作代碼:";
        cin >> index;
        switch(index)
        {
            case 1:
                InitQueue(LQ);
                break;
            case 2:
                GetHead(LQ);
                break;
            case 3:
                cout << "請輸入入隊元素的值:" ;
                cin >> e;
                EnQueue(LQ,e);
                break;
            case 4:
                DeQueue(LQ,e);
                break;
            case 5:
                cout << "鏈隊中的元素爲:";
                DisplayStack(LQ);
                break;
        }
    }while(index > 0);
}
void TipS(string str)
{
    cout << "###### 1812050030-戴琦 ######" << endl;
    cout << "1 ---------------- 創建" << str << endl;
    cout << "2 ---------------- 取" << str << "棧頂元素" << endl;
    cout << "3 ---------------- " << str << "入棧" << endl;
    cout << "4 ---------------- " << str << "出棧" << endl;
    cout << "5 ---------------- 顯示"<< str << "元素"  << endl;
    cout << "------------- 輸入其它退出程序!" << endl;
}
void TipQ(string str)
{
    cout << "###### 1812050030-戴琦 ######" << endl;
    cout << "1 ---------------- 創建" << str << endl;
    cout << "2 ---------------- 取" << str << "隊頭元素" << endl;
    cout << "3 ---------------- " << str << "入隊" << endl;
    cout << "4 ---------------- " << str << "出隊" << endl;
    cout << "5 ---------------- 顯示"<< str << "元素"  << endl;
    cout << "------------- 輸入其它退出程序!" << endl;
}
void InitStack(SqStack &S)
{
    S.base = new ElemType[MAXSIZE];
    if(!S.base)
        cout << "創建失敗!" << endl;
    else
    {
        S.top = S.base;
        S.stacksize = MAXSIZE;
        cout << "創建成功!" << endl;
    }
}
void Push(SqStack &S,ElemType e)
{
    if(S.top-S.base == S.stacksize)
        cout << "棧已滿,無法入棧!" << endl;
    else
    {
        *S.top++ = e;
        cout << "入棧成功!";
        cout << "此時順序棧爲:";
        DisplayStack(S);
    }
}
void Pop(SqStack &S,ElemType &e)
{
    if(S.base == S.top)
        cout << "棧爲空,無法出棧!" << endl;
    else
    {
        e = *--S.top;
        cout << "出棧成功!";
        cout << "此時順序棧爲:";
        DisplayStack(S);
    }

}
void GetTop(SqStack S)
{
    if(S.base != S.top)
        cout << "棧頂元素爲:" << *(S.top-1) << endl;
    else
        cout << "棧爲空!" << endl;
}
void DisplayStack(SqStack S)
{
    ElemType *l = S.base;
    while(l != S.top)
        cout << *l++ << " ";
    cout << endl;
}

void InitStack(LinkStack &S)
{
    S = NULL;
    cout << "創建成功!" << endl;
}
void Push(LinkStack &S,ElemType e)
{
    LinkStack p = new Node;
    p->data = e;
    p->next = S;
    S = p;
    cout << "入棧成功!";
    cout << "此時鏈棧中的元素爲:" ;
    DisplayStack(S);
}
void Pop(LinkStack &S,ElemType &e)
{
    if(S == NULL)
        cout << "棧爲空!" << endl;
    else
    {
        LinkStack p = new Node;
        e = S->data;
        p = S;
        S = S->next;
        delete p;
        cout << "出棧成功!";
        cout << "此時鏈棧中的元素爲:" ;
        DisplayStack(S);
    }
}
void GetTop(LinkStack S)
{
    if(S != NULL)
        cout << "棧頂元素爲:" << S->data << endl;
    else
        cout << "棧爲空!" << endl;
}
void DisplayStack(LinkStack S)
{
    ElemType e;
    while(S != NULL)
    {
        LinkStack p = new Node;
        e = S->data;
        p = S;
        S = S->next;
        cout << e << " ";
        delete p;
    }
    cout << endl;
}

void InitQueue(SqQueue &Q)
{
    Q.base = new ElemType[MAXSIZE];
    if(!Q.base)
        cout << "創建失敗!" << endl;
    else
    {
        Q.frontQ = Q.rearQ = 0;
        cout << "創建成功!" << endl;
    }
}
void EnQueue(SqQueue &Q,ElemType e)
{
    if((Q.rearQ+1) % MAXSIZE == Q.frontQ)
        cout << "循環隊列已滿!" << endl;
    else
    {
        Q.base[Q.rearQ] = e;
        Q.rearQ = (Q.rearQ+1) % MAXSIZE;
        cout << "入隊成功!";
        cout << "此時循環隊列中的元素爲:" ;
        DisplayStack(Q);
    }
}
void DeQueue(SqQueue &Q,ElemType e)
{
    if(Q.rearQ == Q.frontQ)
        cout << "循環隊列爲空!" << endl;
    else
    {
        e = Q.base[Q.frontQ];
        Q.frontQ = (Q.frontQ+1) % MAXSIZE;
        cout << "出隊成功!" ;
        cout << "此時循環隊列中的元素爲:" ;
        DisplayStack(Q);
    }
}
void GetHead(SqQueue Q)
{
    if(Q.frontQ != Q.rearQ)
        cout << "隊頭元素爲:" << Q.base[Q.frontQ] << endl;
}
void DisplayStack(SqQueue Q)
{
    while(Q.frontQ != Q.rearQ)
    {
        cout << Q.base[Q.frontQ] << " ";
        Q.frontQ++;
    }
    cout << endl;
}

void InitQueue(LinkQueue &Q)
{
    Q.frontQ = Q.rearQ = new Node;
    Q.frontQ->next = NULL;
    cout << "創建成功!" << endl;
}
void EnQueue(LinkQueue &Q,ElemType e)
{
    QueuePtr p = new Node;
    p->data = e;
    p->next = NULL;
    Q.rearQ->next = p;
    Q.rearQ = p;
    cout << "入隊成功!";
    cout << "此時循環隊列中的元素爲:" ;
    DisplayStack(Q);
}
void DeQueue(LinkQueue &Q,ElemType e)
{
    if(Q.rearQ == Q.frontQ)
        cout << "循環隊列爲空!" << endl;
    else
    {
        QueuePtr p = Q.frontQ->next;
        e = p->data;
        Q.frontQ->next = p->next;
        if(Q.rearQ == p)
            Q.rearQ = Q.frontQ;
        delete p;
        cout << "出隊成功!";
        cout << "此時循環隊列中的元素爲:" ;
        DisplayStack(Q);
    }
}
void GetHead(LinkQueue Q)
{
    if(Q.frontQ != Q.rearQ)
        cout << "隊頭元素爲:" << Q.frontQ->next->data << endl;
}
void DisplayStack(LinkQueue Q)
{
    while(Q.frontQ != Q.rearQ)
    {
        cout << Q.frontQ->next->data << " ";
        Q.frontQ = Q.frontQ->next;
    }
    cout << endl;
}

5.實驗總結

(1) 進一步體會了棧的後出先進原則。數據元素入棧時要先將其壓入棧頂,再將棧頂指針加1;數據元素出棧時要先將棧頂指針減1,再將棧頂元素賦給e。
(2) 判斷循環隊列是否爲空條件Q.front == Q.rear;
判斷循環隊列是否已滿條件(Q.rear+1)%MAXZISE == Q.front。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章