Data Structure and Algorithm Design - Implementation of Stack

Data Structure and Algorithm Design - Implementation of Stack

#include <iostream>
using namespace std;
typedef struct Link{
    
    
    int date;
    struct Link* next;
}ink,*pLink;
typedef struct Stack{
    
    
    pLink top;
    pLink bottom;
}Stack,*pStack;
void initStack(Stack);
void push(Stack *);
void traverse(Stack*,int);

void initStack(Stack* stack)
{
    
    
    pLink begin = (pLink)malloc(sizeof(Link));
    begin = NULL;
    stack->top= begin;
    stack->bottom= begin;
}
bool push(Stack * stack,int value)
{
    
    
    pLink newnum = (pLink)malloc(sizeof(Link));
    newnum->date = value;
    newnum->next = stack->top;
    stack->top = newnum;
    return true;
}
void traverse(Stack* stack)
{
    
    
    pLink ptr = stack->top;
    while(ptr!=NULL)
    {
    
    
        cout<<ptr->date<<endl;
        ptr = ptr->next;
    }
}
int main(int argc, char const *argv[])
{
    
    
    Stack stack;
    initStack(&stack);
    int input=0;
    while(1)
    {
    
    
        cin>>input;
        if(input==-1)
            break;
        push(&stack,input);
    }
    traverse(&stack);
    return 0;
}

Guess you like

Origin blog.csdn.net/qq_46061085/article/details/120532864