#include <stdio.h>
#include <stdlib.h>
#define STACK_ERROR 60000
typedef int element;
typedef struct stacknode{
element data;
struct stacknode* link;
}StackNode;
typedef struct{
int numOfNode;
StackNode* top;
}LinkedStack;
LinkedStack* createStack(){
LinkedStack *s=(LinkedStack*)malloc(sizeof(LinkedStack));
s->numOfNode=0;
s->top=NULL;
return s;
}
int isEmpty(LinkedStack *s){
if(s->top==NULL) return 1;
else return 0;
}
int push(LinkedStack *s, element data){
StackNode *newNode=(StackNode*)malloc(sizeof(StackNode));
if(newNode==NULL){
printf("stack overflow\n");
return STACK_ERROR;
}
newNode->data=data;
newNode->link=s->top;
s->top=newNode;
s->numOfNode++;
return 1;
}
element pop(LinkedStack *s){
StackNode *temp;
element data;
temp=s->top;
if(isEmpty(s)){
printf("stack underflow\n");
return STACK_ERROR;
}
s->top=temp->link;
s->numOfNode--;
data=temp->data;
free(temp);
return data;
}
element peek(LinkedStack *s){
if(isEmpty(s)){
printf("Stack is Empty\n");
return STACK_ERROR;
}
return s->top->data;
}
void printStack(LinkedStack *s){
StackNode *temp;
temp=s->top;
while(temp!=NULL){
printf("%d\n",temp->data);
temp=temp->link;
}
printf("\n");
}
int main(){
LinkedStack *s1, *s2;
s1=createStack();
s2=createStack();
push(s1,10);
push(s2,10);
printStack(s1);
push(s1,100);
printStack(s1);
pop(s1);
printStack(s1);
pop(s1);
return 0;
}