数据结构实验2-栈和队列

实现顺序栈的基本运算

实验要求

编写一个程序,实现顺序栈的各种基本运算,并在此基础上设计一个主程序完成如下功能:
(1)初始化栈s;
(2)判断栈s是否为空;
(3)依次进栈元素-1,2,10,-3,5;
(4)判断栈s是否为空;
(5)输出栈长度;
(6)输出从栈顶到栈底的元素;
(7)输出出栈序列;
(8)判断栈s是否为空。

程序代码

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
typedef int ElemType;
typedef int Status;
typedef int Boolean;
typedef struct{
    ElemType *base;
    ElemType *top;
    int stacksize;
}Sqstack;
Status InitStack(Sqstack & S)
{
    S.base = (ElemType *) malloc(STACK_INIT_SIZE * sizeof(ElemType));
    if(!S.base) exit(OVERFLOW);
    S.top = S.base;
    S.stacksize = STACK_INIT_SIZE;
    return OK;
}
Status GetTop(Sqstack S,ElemType & e)
{
    if(S.top == S.base) return ERROR;
    e = *(S.top -1);
    return OK;
}
Status Push(Sqstack & S, ElemType e)
{
    if(S.top - S.base >= S.stacksize){
        S.base = (ElemType *) realloc(S.base,(S.stacksize + STACKINCREMENT) * sizeof(ElemType));
        if(!S.base) exit(OVERFLOW);
        S.top = S.base + S.stacksize;
        S.stacksize += STACKINCREMENT;
    }
    *S.top ++ = e;
    return OK;
}
Status Pop (Sqstack & S, ElemType & e)
{
    if(S.top == S.base) return ERROR;
    e =  * -- S.top;
    return OK;
}
Status IsEmpty(Sqstack S)
{
    if(S.top == S.base) return TRUE;
    else return FALSE;
}
Status GetLength(Sqstack S)
{
    return (S.top - S.base);
}
Status Traverse(Sqstack S)
{
    ElemType * p = S.top;
    bool isfirst = true;
    while(1){
        p--;
        if(isfirst){
            printf("%d", *p);
            isfirst = false;
        }else{
            printf(" %d", *p);
        }
        if(p == S.base) break;
    }
    printf("\n");
    return OK;
}
Status Paint()
{
    printf("|"); printf("      The Experiment Of Stack      ") ;printf("|\n");
    printf("|"); printf("          Sequential Stack         ") ;printf("|\n");
    printf("|"); printf("        Author: Luo Peng Fei       ") ;printf("|\n");
    printf("|"); printf("           Date:2017/4/5           ") ;printf("|\n");
    return OK;
}
int main()
{
    Sqstack S;
    InitStack(S);
    Paint();
    printf("             当前栈%s\n",IsEmpty(S)?"空":"不空");
    printf("|"); printf("        请输入数字的数量           ") ;printf("|\n");
    int n,temp;
    scanf("%d",&n);
    printf("|"); printf("       请依次输入%d个数字           ",n) ;printf("|\n");

    for(int i = 0; i<n;++i){
        scanf("%d",&temp);
        Push(S,temp);
    }
    printf("            当前栈%s\n",IsEmpty(S)?"空":"不空");
    printf("|"); printf("         当前栈的长度是%d           ",GetLength(S)) ;printf("|\n");
    printf("|"); printf("         当前栈的内容是            ") ;printf("|\n");
    Traverse(S);
    printf("|"); printf("          出栈的顺序是             ") ;printf("|\n");
    for(int i = 0; i<n; ++i){
        Pop(S,temp);
        printf("%d ",temp);
    }
    printf("\n");
    printf("            当前栈%s\n",IsEmpty(S)?"空":"不空");;
    return 0;
}

运行结果

这里写图片描述

实现链栈基本运算

实验要求

编写一个程序,实现链栈的各种基本运算,并在此基础上设计一个主程序完成如下功能:
(1)初始化栈s;
(2)判断栈s是否为空;
(3)依次进栈元素10,-2,10,-3,15,12;
(4)判断栈s是否为空;
(5)输出栈长度;
(6)输出从栈顶到栈底的元素;
(7)输出出栈序列;
(8)判断栈s是否为空。

程序代码

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
typedef int ElemType;
typedef int Status;
typedef int Boolean;
typedef struct node{
    ElemType Data;
    struct node* next;
}Node;
typedef struct{
    Node * top;
    Node * bottom;
}Linkstack;
Status init(Linkstack &S)
{
    Node * p = (Node *)malloc(sizeof(Node));
    if(!p) return ERROR;
    S.bottom = S.top = p;
    p->next = NULL;
    return OK;
}
Status IsEmpty(Linkstack S)
{
    if(S.bottom == S.top) return TRUE;
    else return FALSE;
}
Status Push(Linkstack & S, ElemType e)
{
    Node * p = (Node *)malloc(sizeof(Node));
    if(!p) return ERROR;
    p->Data = e;
    p->next = S.top;
    S.top =  p;
    return OK;
}
Status Pop( Linkstack & S,ElemType & e)
{
    if(S.bottom == S.top) return ERROR;
    Node * p = S.top;
    e = p->Data;
    S.top = p->next;
    free(p);
    return OK;
}
Status GetTop(Linkstack S, ElemType &e)
{
    if(S.bottom == S.top) return ERROR;
    else e = S.top->Data;
    return OK;
}

Status GetLength(Linkstack S)
{
    Node * p = S.top;
    int cnt = 0;
    while(p != S.bottom){
        cnt++;
        p = p->next;
    }
    return cnt;
}
Status Traverse(Linkstack S)
{
    if(S.top == S.bottom) return ERROR;
    Node * p = S.top;
    while(p != S.bottom){
        printf("%d ",p->Data);
        p = p->next;
    }
    printf("\n");
    return OK;
}
Status Paint()
{
    printf("|"); printf("      The Experiment Of Stack      ") ;printf("|\n");
    printf("|"); printf("          Link       Stack         ") ;printf("|\n");
    printf("|"); printf("        Author: Luo Peng Fei       ") ;printf("|\n");
    printf("|"); printf("           Date:2017/4/5           ") ;printf("|\n");
    return OK;
}
int main()
{
    Linkstack S;
    init(S);
    Paint();
    printf("             当前栈%s\n",IsEmpty(S)?"空":"不空");
    printf("|"); printf("        请输入数字的数量           ") ;printf("|\n");
    int n,temp;
    scanf("%d",&n);
    printf("|"); printf("       请依次输入%d个数字           ",n) ;printf("|\n");
    for(int i = 0; i<n;++i){
        scanf("%d",&temp);
        Push(S,temp);
    }
    printf("            当前栈%s\n",IsEmpty(S)?"空":"不空");
    printf("|"); printf("         当前栈的长度是%d           ",GetLength(S)) ;printf("|\n");
    printf("|"); printf("         当前栈的内容是            ") ;printf("|\n");
    Traverse(S);
    printf("|"); printf("          出栈的顺序是             ") ;printf("|\n");
    for(int i = 0; i<n; ++i){
        Pop(S,temp);
        printf("%d ",temp);
    }
    printf("\n");
    printf("            当前栈%s\n",IsEmpty(S)?"空":"不空");
    return 0;
}

运行结果

这里写图片描述

实现循环队列的基本运算

实验要求

编写一个程序,实现循环队列的各种基本运算,并在此基础上设计一个主程序完成如下功能:
(1)初始化队列q;
(2)判断队列q是否为空;
(3)依次进队列元素-1,2,10;
(4)出队一个元素,并输出该元素;
(5)输出队列的长度(元素个数);
(6)依次进队元素-3,12,10;
(7)输出出队序列;
(8)判断队列q是否为空。

程序代码

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
#define MAXSIZE 100
typedef int ElemType;
typedef int Status;
typedef int Boolean;
typedef struct{
    ElemType * base;
    int front;
    int rear;
}SqQueue;
Status InitQueue(SqQueue & Q)
{
    Q.base = (ElemType *) malloc(MAXSIZE * (sizeof (ElemType)));
    if(!Q.base) exit(OVERFLOW);
    Q.front = Q.rear = 0;
    return OK;
}
Status QueueLength(SqQueue Q)
{
    return (Q.rear - Q.front + MAXSIZE) % MAXSIZE;
}
Status Enqueue(SqQueue & Q, ElemType e)
{
    if((Q.rear+1) % MAXSIZE == Q.front) return ERROR;
    Q.base[Q.rear] = e;
    Q.rear = (Q.rear + 1) % MAXSIZE;
    return OK;
}
Status Dequeue(SqQueue & Q, ElemType & e)
{
    if(Q.front == Q.rear) return ERROR;
    e = Q.base[Q.front];
    Q.front = ( Q.front + 1) % MAXSIZE;
    return OK;
}
Status IsEmpty(SqQueue Q)
{
    if(Q.front == Q.rear) return TRUE;
    else return FALSE;
}
Status Paint()
{
    printf("|"); printf("      The Experiment Of Queue      ") ;printf("|\n");
    printf("|"); printf("          Circular   Queue         ") ;printf("|\n");
    printf("|"); printf("        Author: Luo Peng Fei       ") ;printf("|\n");
    printf("|"); printf("           Date:2017/4/5           ") ;printf("|\n");
    return OK;
}
int main()
{
    SqQueue Q;
    Paint();
    InitQueue(Q);
    printf("            当前队列%s\n",IsEmpty(Q)?"空":"不空");
    printf("|"); printf("        请输入数字的数量           ") ;printf("|\n");
    int n,temp;
    scanf("%d",&n);
    printf("|"); printf("       请依次输入%d个数字           ",n) ;printf("|\n");
    for(int i = 0; i<n;++i){
        scanf("%d",&temp);
        Enqueue(Q,temp);
    }
    Dequeue(Q,temp);
    printf("|"); printf("          现在%d出队列              ",temp) ;printf("|\n");
    printf("|"); printf("         当前队列的长度是%d           ",QueueLength(Q)) ;printf("|\n");
    printf("|"); printf("        请输入数字的数量           ") ;printf("|\n");
    scanf("%d",&n);
    printf("|"); printf("       请依次输入%d个数字           ",n) ;printf("|\n");
    for(int i = 0; i<n;++i){
        scanf("%d",&temp);
        Enqueue(Q,temp);
    }
    printf("|"); printf("          出队的顺序是             ") ;printf("|\n");
    while(!IsEmpty(Q)){
        Dequeue(Q,temp);
        printf("%d ",temp);
    }
    printf("\n");
    printf("            当前队列%s\n",IsEmpty(Q)?"空":"不空");
    return 0;
}

运行结果

这里写图片描述

实现链队列的基本运算

实验要求

编写一个程序,实现链队列的各种基本运算,并在此基础上设计一个主程序完成如下功能:
(1)初始化队列q;
(2)判断队列q是否为空;
(3)依次进队列元素1,12,-10;
(4)出队一个元素,并输出该元素;
(5)输出队列的长度(元素个数);
(6)依次进队元素13,-12,10;
(7)输出队列长度;
(8)输出出队序列

程序代码

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
#define MAXSIZE 100
typedef int ElemType;
typedef int Status;
typedef int Boolean;
typedef struct Qnode{
    ElemType data;
    struct Qnode * next;
}QNode,*pQueue;
typedef struct{
    pQueue front;
    pQueue rear;
}LinkQueue;
Status InitQueue(LinkQueue & Q)
{
    Q.rear = (pQueue)malloc(sizeof(QNode));
    Q.front =  Q.rear;
    if(!Q.front) exit(OVERFLOW);
    Q.front->next = NULL;
    return OK;
}
Status IsEmpty(LinkQueue &Q)
{
    if(Q.front->next == NULL) return TRUE;
    else return FALSE;
}
Status QueueLength(LinkQueue &Q)
{
    if(IsEmpty(Q)) return 0;
    pQueue p = Q.front->next;
    int cnt = 0;
    while(p !=NULL){
        cnt++;
        p = p->next;
    }
    return cnt;
}
Status DestotyQueue(LinkQueue & Q)
{
    while(Q.front){
        Q.rear = Q.front->next;
        free(Q.front);
        Q.front = Q.rear;
    }
    return OK;
}
Status Enqueue(LinkQueue & Q, ElemType e)
{
    QNode * p =(pQueue) malloc(sizeof(QNode));
    if(!p) exit(OVERFLOW);
    p->data = e;
    p->next = NULL;
    Q.rear->next = p;
    Q.rear = p;
    return OK;
}
Status Dequeue(LinkQueue & Q, ElemType & e)
{
    if(Q.front == Q.rear) return ERROR;
    pQueue p = Q.front->next;
    e = p->data;
    Q.front->next = p->next;
    if(Q.rear == p) Q.rear = Q.front;
    free(p);
    return OK;
}
Status Paint()
{
    printf("|"); printf("      The Experiment Of Queue      ") ;printf("|\n");
    printf("|"); printf("           Link      Queue         ") ;printf("|\n");
    printf("|"); printf("        Author: Luo Peng Fei       ") ;printf("|\n");
    printf("|"); printf("           Date:2017/4/5           ") ;printf("|\n");
    return OK;
}
int main()
{
    LinkQueue Q;
    InitQueue(Q);
    Paint();
    printf("            当前队列%s\n",IsEmpty(Q)?"空":"不空");
    printf("|"); printf("        请输入数字的数量           ") ;printf("|\n");
    int n,temp;
    scanf("%d",&n);
    printf("|"); printf("       请依次输入%d个数字           ",n) ;printf("|\n");
    for(int i = 0; i<n;++i){
        scanf("%d",&temp);
        Enqueue(Q,temp);
    }
    Dequeue(Q,temp);
    printf("|"); printf("          现在%d出队列              ",temp) ;printf("|\n");
    printf("|"); printf("         当前队列的长度是%d           ",QueueLength(Q)) ;printf("|\n");
    printf("|"); printf("        请输入数字的数量           ") ;printf("|\n");
    scanf("%d",&n);
    printf("|"); printf("       请依次输入%d个数字           ",n) ;printf("|\n");
    for(int i = 0; i<n;++i){
        scanf("%d",&temp);
        Enqueue(Q,temp);
    }
    printf("|"); printf("          出队的顺序是             ") ;printf("|\n");
    while(!IsEmpty(Q)){
        Dequeue(Q,temp);
        printf("%d ",temp);
    }
    printf("\n");
    printf("            当前队列%s\n",IsEmpty(Q)?"空":"不空");
    return 0;
}

运行结果

这里写图片描述

  • 7
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值