#include <stdio.h>
#include <stdlib.h>
typedef struct Node{
int data;
struct Node* next;
}Node;
Node* initStack(){
Node* Stack=(Node*)malloc(sizeof(Node));
Stack->next=NULL;
Stack->data=0;
return Stack;
}
int isEmpty(Node* Stack){
if(Stack->data==0||Stack->next==NULL){
return 1;
}else{
return 0;
}
}
int push(Node* Stack,int data){
Node* node=(Node*)malloc(sizeof(Node));
node->data=data;
node->next=Stack->next;
Stack->next=node;
Stack->data++;
}
int pop(Node* stack){
if(isEmpty(stack)){
return -1;
}else{
Node* node=stack->next;
stack->next=node->next;
free(node);
}
}
void printStack(Node* Stack){
Node* node=Stack->next;
while(node){
printf("%d->",node->data);
node=node->next;
}
printf("over!");
}
int main() {
Node* stack=initStack();
push(stack,1);
push(stack,2);
push(stack,3);
push(stack,4);
pop(stack);
pop(stack);
//pop(stack);
//pop(stack);
//pop(stack);
printStack(stack);
return 0;
}
数据结构——堆栈
最新推荐文章于 2025-04-18 23:03:00 发布