Tag:链栈

Tag (链栈)'s result:

链栈

链栈设了一个栈顶指针,这里我实现了插入,删除,判空,打印操作 #include <stdio.h> #include <stdlib.h> typedef struct Node { int data; struct Node *next; } StackNode; StackNode *top = NULL; void push(int e) { StackNode *p; p = (StackNode *) malloc (sizeof(StackNode)); p->data = e; //头插 p->next = top; top = p; //更新栈顶指针 } bool isEmpty() { return top==NULL ? true:false; } int pop() { StackNode *p; int e; if (isEmpty()) { printf(“The stack is empty, failed to pop!\n”); return NULL; } p = top; e = p->data; top = top->next; free(p); return e; } void print() { StackNode *p; p = top; if (isEmpty()) { printf(“The stack is empty!\n”); return; } printf(“\n”); while (p) { printf(“%d “, p->data); p = p->next; } printf(“\n”); } int main() { int x,n1,n2,n; printf(“请输入入栈元素个数\n”); scanf(“%d”,&n); while (n–) { scanf(“%d”,&x); push(x); } n1 = pop(); n2 = pop(); printf(“%d\n%d”,n1,n2); print(); return 0; }