数据结构之堆栈(顺序存储)——浙大数据结构公开课代码整理

浙江大学数据结构陈越老师视频课程

链表代码整理

  1. 链表结构头文件
//structStack.h
#include <iostream>

#define MaxSize 10
#define ERROR -0.000000006

typedef struct SNode *Stack;
struct SNode{
    double Data[MaxSize];
    int Top=0;
};

//in stack
void Push(Stack PtrS, double item)
{
    if(PtrS->Top==MaxSize-1){
        printf("Stack is full.\n"); return;
    }else {
        PtrS->Data[++(PtrS->Top)]=item;
    }
}

//out stack
double Pop(Stack PtrS)
{
    if(PtrS->Top==-1){
        printf("Stack is empty.\n");
        return ERROR;
    }else
    {
        return(PtrS->Data[(PtrS->Top)--]);
    }
    
}
  1. 主函数,调用链表结构头文件,实现链表一系列具体操作。
//main.cpp
#include "structStack.h"
using namespace std;

int main()
{
    int i=0;
    Stack mystack=(Stack)malloc(sizeof(struct SNode));

    cout<<"Push elements into the stack."<<endl;
    for(i=1;i<11;i++)
    {
        double vluEle=3.0*i;
        cout<<"Push "<<vluEle<<" into the stack."<<endl;
        Push(mystack,vluEle);
    }

    cout<<"The length of stack is "<<mystack->Top<<endl;

    cout<<"Pop elements from the stack:"<<endl;
    double eleMent;
    eleMent=Pop(mystack);
    if(eleMent==ERROR){
        cout<<"The stack is empty."<<endl;
    }else{
        cout<<"Pop "<<eleMent<<endl;
    }

    eleMent=Pop(mystack);
    if(eleMent==ERROR){
        cout<<"The stack is empty."<<endl;
    }else{
        cout<<"Pop "<<eleMent<<endl;
    }

    cout<<"Push new element into the stack"<<endl;
    Push(mystack,9.9);
}
  1. 结果:
    在这里插入图片描述

一个数组实现两个堆栈:

  1. 链表结构头文件
//structStack.h
#include <iostream>

#define MaxSize 10
#define ERROR -0.000000006
//an array realize two stacks
typedef struct DStack *Stack;
struct DStack{
    double Data[MaxSize];
    int Top1;
    int Top2;
};

//push into stack, Tag: 1-> stack 1; 2-> stack 2
void Push(struct DStack *PtrS, double item, int Tag)
{
    if(PtrS->Top2 - PtrS->Top1 ==1){
        printf("The stack is full.\n"); return;
    }
    if(Tag==1)
    {
        PtrS->Data[++(PtrS->Top1)]=item;
    }        
    else
    {
        PtrS->Data[--(PtrS->Top2)]=item;
    }    
}
//pop from stack
double Pop(struct DStack *PtrS, int Tag)
{
    if(Tag==1){
        if(PtrS->Top1==-1){
            return ERROR;
        }else return PtrS->Data[(PtrS->Top1)--];
    }else{
        if(PtrS->Top2==MaxSize){
            return ERROR;
        }else return PtrS->Data[(PtrS->Top2)++];
    }
}
  1. 主函数,调用链表结构头文件,实现链表一系列具体操作。
//main.cpp
#include "structStack.h"
using namespace std;

int main()
{
    int i=0;
    Stack mystack = (Stack)malloc(sizeof(struct DStack));
    mystack->Top1=-1;
    mystack->Top2=MaxSize;

    // cout<<"Push elements into the stack."<<endl;
    for(i=1;i<11;i++)
    {
        double vluEle=3.0*i;
        if(i%2==1){
            cout<<"Push "<<vluEle<<" into stack 1."<<endl;
            Push(mystack,vluEle, 1);
        }
        else{ 
            cout<<"Push "<<vluEle<<" into stack 2."<<endl;
            Push(mystack,vluEle, 2);
        }
    }

    int lengthStack1= mystack->Top1 + 1;
    int lengthStack2=MaxSize - mystack->Top2;
    cout<<"The length of stack 1 is: "<<lengthStack1<<endl;
    cout<<"The length of stack 2 is: "<<lengthStack2<<endl;

    cout<<"Pop element from stack 1"<<endl;
    double eleMent;
    eleMent = Pop(mystack,1);
    if(eleMent==ERROR){
        cout<<"Stack 1 is empty."<<endl;
    }else{
        cout<<"Pop "<<eleMent<<endl;
    }
    
    cout<<"Pop element from stack 2"<<endl;
    eleMent = Pop(mystack,2);
    if(eleMent==ERROR){
        cout<<"Stack 2 is empty."<<endl;
    }else{
        cout<<"Pop "<<eleMent<<endl;
    }

    cout<<"Push element into stack 2"<<endl;
    Push(mystack, 9.6, 2);
    cout<<"Push element into stack 2"<<endl;
    Push(mystack, 3.6, 2);

    lengthStack1=mystack->Top1 + 1;
    lengthStack2=MaxSize - mystack->Top2;
    cout<<"The length of stack 1 is: "<<lengthStack1<<endl;
    cout<<"The length of stack 2 is: "<<lengthStack2<<endl;

    cout<<"Pop element from stack 1"<<endl;
    while (mystack->Top1!=-1)
    {
        eleMent=Pop(mystack,1);
        if(eleMent==ERROR)
        {
            cout<<"Stack 1 is empty."<<endl;
        }
        else
        {
            cout<<"Pop "<<eleMent<<" from stack 1."<<endl;
        }       
        
    }

    eleMent=Pop(mystack,1);
    if(eleMent==ERROR)
    {
        cout<<"Stack 1 is empty."<<endl;
    }
    else
    {
        cout<<"Pop "<<eleMent<<" from stack 1."<<endl;
    }      

    cout<<"Pop element from stack 2"<<endl;
    eleMent=Pop(mystack,2);
    if(eleMent==ERROR)
    {
        cout<<"Stack 2 is empty."<<endl;
    }
    else
    {
        cout<<"Pop "<<eleMent<<" from stack 2."<<endl;
    }       
  1. 结果:
    在这里插入图片描述
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值