首页 > 解决方案 > 堆栈操作(主函数)

问题描述

所以我在 C++ 中实现了 Stack 的基本操作。我编写了函数,但我不知道如何实现 main 函数以便从堆栈中查看值。

头文件

#ifndef HEADER_H_
#define HEADER_H_
#define DIM 15
typedef int Atom;

struct Element {
    Atom data;
    Element *link;
};

typedef Element* LinkedStack;

void initS(LinkedStack &S);
void push(LinkedStack &S, Atom a);
bool isEmpty2(LinkedStack &S);
void pop2(LinkedStack &S);
Atom top2(Stack &S);
#endif

函数文件

void initS(LinkedStack &S)
{
    S = nullptr;
}

void push(LinkedStack &S, Atom a)
{
    Element*nou = new Element;
    nou->data = a;
    nou->link = S;
    S = nou;
}

bool isEmpty2(LinkedStack &S)
{
    if (S == 0)
        return true;
    else return false;
}

void pop2(LinkedStack &S)
{
    LinkedStack aux = S;
    S = S->link;
    delete(aux);
}

Atom top2(LinkedStack &S)
{
    if (isEmpty2(S))
        return Atom();
    return S->data;
}

我是如何实现主要功能的。我不知道如何查看值,例如如果我写 cout<

#include <iostream>
#include "header.h"
using namespace std;

int main()
{
    LinkedStack S;
    initS(S);
    push(S, 2);
    push(S, 4);
    push(S, 6);
    push(S, 7);
    push(S, 10);
    return 0;

}

After I compile the program I don't see nothing in console.How to see the values from stack?

标签: c++data-structuresstack

解决方案


推荐阅读