#include <stdio.h>
#include <stdlib.h>
//Structure containing a Data part & a
//Link part to the next node in the List
struct Node
{
int Data;
struct Node *Next;
}*Head;
//Poping from a Stack
void popStack()
{
struct Node *tmp_ptr=NULL, *cur_ptr=Head;
if(cur_ptr)
{
Head = Head->Next;
free(cur_ptr);
}
else
printf("\nStack is Empty");
}
//Pushing into Stack
void pushIntoStack(int num)
{
struct Node *tmp_ptr=NULL;
if((tmp_ptr=(struct Node *)malloc(sizeof(struct Node))) != NULL)
tmp_ptr->Data=num;
else
printf("\nMemory Allocation Failed");
if(Head)
{
tmp_ptr->Next=Head;
Head=tmp_ptr;
}
else { //List is Empty
Head=tmp_ptr;
Head->Next=NULL;
}
}
//Displaying Stack
void displayStack()
{
struct Node *cur_ptr=Head;
if(cur_ptr)
{
printf("\nElements in Stack:\n");
while(cur_ptr)
{
printf("\t%d\n",cur_ptr->Data);
cur_ptr=cur_ptr->Next;
}
printf("\n");
}
else
printf("\nStack is Empty");
}
int main(int argc, char *argv[])
{
int i=0;
//Set HEAD as NULL
Head=NULL;
while(1)
{
printf("\n####################################################\n");
printf("MAIN MENU\n");
printf("####################################################\n");
printf(" \n1. Push into stack");
printf(" \n2. Pop from Stack");
printf(" \n3. Display Stack");
printf(" \n4. Exit\n");
printf(" \nChoose Option: ");
scanf("%d",&i);
switch(i)
{
case 1:
{
int num;
printf("\nEnter a Number to push into Stack: ");
scanf("%d",&num);
pushIntoStack(num);
displayStack();
break;
}
case 2:
{
popStack();
displayStack();
break;
}
case 3:
{
displayStack();
break;
}
case 4:
{
struct Node *temp;
while(Head!=NULL)
{
temp = Head->Next;
free(Head);
Head=temp;
}
exit(0);
}
default:
{
printf("\nWrong Option choosen");
}
}/* end if switch */
}/* end of while */
}/* end of main */
#include <stdlib.h>
//Structure containing a Data part & a
//Link part to the next node in the List
struct Node
{
int Data;
struct Node *Next;
}*Head;
//Poping from a Stack
void popStack()
{
struct Node *tmp_ptr=NULL, *cur_ptr=Head;
if(cur_ptr)
{
Head = Head->Next;
free(cur_ptr);
}
else
printf("\nStack is Empty");
}
//Pushing into Stack
void pushIntoStack(int num)
{
struct Node *tmp_ptr=NULL;
if((tmp_ptr=(struct Node *)malloc(sizeof(struct Node))) != NULL)
tmp_ptr->Data=num;
else
printf("\nMemory Allocation Failed");
if(Head)
{
tmp_ptr->Next=Head;
Head=tmp_ptr;
}
else { //List is Empty
Head=tmp_ptr;
Head->Next=NULL;
}
}
//Displaying Stack
void displayStack()
{
struct Node *cur_ptr=Head;
if(cur_ptr)
{
printf("\nElements in Stack:\n");
while(cur_ptr)
{
printf("\t%d\n",cur_ptr->Data);
cur_ptr=cur_ptr->Next;
}
printf("\n");
}
else
printf("\nStack is Empty");
}
int main(int argc, char *argv[])
{
int i=0;
//Set HEAD as NULL
Head=NULL;
while(1)
{
printf("\n####################################################\n");
printf("MAIN MENU\n");
printf("####################################################\n");
printf(" \n1. Push into stack");
printf(" \n2. Pop from Stack");
printf(" \n3. Display Stack");
printf(" \n4. Exit\n");
printf(" \nChoose Option: ");
scanf("%d",&i);
switch(i)
{
case 1:
{
int num;
printf("\nEnter a Number to push into Stack: ");
scanf("%d",&num);
pushIntoStack(num);
displayStack();
break;
}
case 2:
{
popStack();
displayStack();
break;
}
case 3:
{
displayStack();
break;
}
case 4:
{
struct Node *temp;
while(Head!=NULL)
{
temp = Head->Next;
free(Head);
Head=temp;
}
exit(0);
}
default:
{
printf("\nWrong Option choosen");
}
}/* end if switch */
}/* end of while */
}/* end of main */