实验三 栈和队列的操作 (数据结构实验C++编写)

文章目录

    • 1.实验目的
    • 2.实验内容
    • 3.实验步骤
    • 4.实验代码
    • 5.实验总结

1.实验目的

(1)掌握栈的顺序存储结构、链式存储结构及其基本操作;
(2)掌握队列的顺序存储结构、链式存储结构及其基本操作。

2.实验内容

(1)编程实现栈的以下基本操作:建栈,取栈顶元素,入栈,出栈。
(2)编程实现队列的以下基本操作:建队列,取队头元素,入队,出队。

3.实验步骤

(1) 编写程序框架,利用while循环输入操作对象,利用switch选择语句对输入的代码进行判断,并进行相应操作。
(2) 编写顺序栈的相关操作。
(3)编写链栈的相关操作。
(4)编写循环队列的相关操作。
(5)编写链队的相关操作。
(6)执行代码,测试数据。

4.实验代码

#include 

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。

你可能感兴趣的:(课程复习,数据结构)