SOURCE CODE:
#include
#include
#include
struct ListNode{
int data;
struct ListNode *next;
};
struct Stack{
struct ListNode *top;
};
struct Stack *createStack(){
struct Stack *stk;
stk = malloc(sizeof(struct Stack));
stk->top = NULL;
return stk;
}
void push(struct Stack *stk, int data){
struct ListNode *temp;
temp = malloc(sizeof(struct ListNode));
if(!temp){
printf(“nStack/Heap overflow”);
return;
}
temp->data = data;
temp->next = stk->top;
stk->top = temp;
}
int size(struct Stack *stk){
// NOTE: we could improve the performance of the size function by adding a size variable in the
// stack structure and update it when doing push/pop operations
int count = 0;
struct ListNode *temp;
if(isEmpty(stk))
return 0;
temp = stk->top;
while (temp){
count++;
temp = temp->next;
}
return count;
}
int isEmpty(struct Stack *stk){
return stk->top == NULL;
}
int pop(struct Stack *stk){
int data;
struct ListNode *temp;
if(isEmpty(stk))
return INT_MIN;
temp = stk->top;
stk->top = stk->top->next;
data = temp->data;
free(temp);
return data;
}
int peek(struct Stack * stk){
if(isEmpty(stk))
return INT_MIN;
return stk->top->data;
}
void deleteStack(struct Stack *stk){
struct ListNode *temp, *p;
p = stk->top;
while( p) {
temp = p->next;
p = p->next;
free(temp);
}
free(stk);
}
int main(){
int i = 0;
struct Stack *stk = createStack();
for(i = 0; i <= 10; i++){
push(stk, i);
}
printf("Top element is %dn", peek(stk));
printf("Stack size is %dn", size(stk));
for (i = 0; i <= 10; i++){
printf("Popped element is %dn", pop(stk));
}
if (isEmpty(stk))
printf("Stack is empty");
else
printf("Stack is not empty");
deleteStack(stk);
return 0;
}
Place this order or similar order and get an amazing discount. USE Discount code “GET20” for 20% discount