#include<stdio.h>
#define ERROR 0
#define Status int
#define SUCCESS 1
typedef struct Node{
Node* next;
int data;
}*Stack;
void initStack(Stack &stack){
stack->next = NULL;
}
Status isEmpty(Stack stack){
if(stack->next == NULL){
return 1;
}
return 0;
}
Status push(Stack &stack,int data){
Stack temp = new Node;
temp->data = data;
temp->next = stack->next;
stack->next = temp;
// temp->next = NULL;
return SUCCESS;
}
Status pop(Stack stack,int &data){
if(isEmpty(stack)){
data = -1;
return ERROR;
}
data = stack->next->data;
Stack temp = stack->next;
stack->next = temp->next;
delete temp;
return SUCCESS;
}
int main(){
Stack stack = new Node;
initStack(stack);
push(stack,1);
push(stack,2);
push(stack,3);
int data;
pop(stack,data);
printf("%d\n",data);
pop(stack,data);
printf("%d\n",data);
pop(stack,data);
printf("%d\n",data);
pop(stack,data);
printf("%d\n",data);
}
C/C++语言实现数据结构系列(二)——单链表实现栈
最新推荐文章于 2021-10-07 10:25:09 发布