C/C++ 知識回顧 棧的入棧與出棧
阿新 • • 發佈:2019-01-27
#include<iostream> using namespace std; typedef struct student { int data; struct student * next; }node; typedef struct stackqueque { node * top, *zhandi; } Stack; Stack * push(Stack * stack,int num) { node *s = (node *)malloc(sizeof(node)); s->data = num; s->next = NULL; if (stack->top==NULL) { stack->top = s; stack->zhandi = s; } else { stack->top->next = s; stack->top = s; } return stack; } int pop(Stack *stack) { if (stack->top==NULL) { cout << "棧為空" << endl; return -1; } if (stack->top==stack->zhandi) { int x = stack->top->data; stack->top = NULL; stack->zhandi = NULL; return x; } else { node* p = stack->zhandi; while (p->next!=stack->top) { p = p->next; } node *q = stack->top; int x = stack->top->data; p->next = NULL; stack->top = p; free(q); return x; } } int main() { }