#include<stdio.h>
#include<memory.h>
#include<stdlib.h>
//Data Structure Declaration
struct Element
{
int value;
struct Element *next;
};
struct Element *TOS=NULL,*temp=NULL;
//You are free to create more pointers, if needed
//Stack Size, Show Overflow for > 5
int SIZE=5;
//Function Declaration
void push(int);
void pop();
void peek();
void peep(int );
void displayStack();
int countElement();
//Main Function
int main()
{
int choice,position,value;
//You are free to create more variable, if needed
do
{
system("cls"); // To clear the screen
printMenu();
printf("\nEnter Your Choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
if(SIZE==0)
{
printf("\nStack Overflow...!");
}
else
{
printf("\n Enter Value for Element : ");
scanf("%d",&value);
push(value);
SIZE--;
}
break;
case 2:
if(TOS==NULL)
{
printf("\n STACK is Empty");
}
else
{
pop();
SIZE++;
}
break;
case 3:
if(TOS==NULL)
{
printf("\n STACK is Empty");
}
else
{
peek();
}
break;
case 4:
if(SIZE==5)
{
printf("\nStack is Empty...!");
}
else
{
printf("\n Enter Value for Element : ");
scanf("%d",&position);
if(countElement()>=position && position > 0)
{
peep(position);
}
else
{
printf("\n Less no of elements are in Stack than given Position");
}
}
break;
case 5:
if(TOS==NULL)
{
printf("\n STACK is Empty");
}
else
{
displayStack();
}
break;
case 6:
if(TOS==NULL)
{
printf("\n STACK is Empty");
}
else
{
printf("\n No of Elements in Stack = %d",countElement());
}
break;
default:
printf("Thanks for Using Stack Program");
}
getch(); //To stop output on Screen
}while(choice>0 && choice<7);
return 0;
}
//Function Implementation
void printMenu()
{
printf("\n==============================");
printf("\n STACK OPERATION MENU");
printf("\n==============================");
printf("\n 1 - PUSH");
printf("\n 2 - POP");
printf("\n 3 - PEEK");
printf("\n 4 - PEEP");
printf("\n 5 - DISPLAY");
printf("\n 6 - COUNT NO OF ELEMENT");
printf("\n 7 - EXIT");
printf("\n==============================");
}
void push(int value)
{
temp=(struct Element *)malloc(sizeof(struct Element)*1);
temp->value=value;
temp->next=NULL;
if(TOS==NULL)
{
TOS=temp;
}
else
{
temp->next=TOS;
TOS=temp;
}
}
void pop()
{
temp=TOS;
TOS=temp->next;
printf("\n %d is Poped out from Stack",temp->value);
free(temp);
}
void peek()
{
printf("\n PEEK VALUE = %d",TOS->value);
}
void peep(int pos)
{
temp=TOS;
while(pos>1)
{
temp=temp->next;
pos--;
}
printf("\n Value = %d ",temp->value);
}
void displayStack()
{
temp=TOS;
while(temp!=NULL)
{
printf("%d -> ",temp->value);
temp=temp->next;
}
}
int countElement()
{
int pos=0;
temp=TOS;
while(temp!=NULL)
{
pos++;
temp=temp->next;
}
return pos;
}
#include<memory.h>
#include<stdlib.h>
//Data Structure Declaration
struct Element
{
int value;
struct Element *next;
};
struct Element *TOS=NULL,*temp=NULL;
//You are free to create more pointers, if needed
//Stack Size, Show Overflow for > 5
int SIZE=5;
//Function Declaration
void push(int);
void pop();
void peek();
void peep(int );
void displayStack();
int countElement();
//Main Function
int main()
{
int choice,position,value;
//You are free to create more variable, if needed
do
{
system("cls"); // To clear the screen
printMenu();
printf("\nEnter Your Choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
if(SIZE==0)
{
printf("\nStack Overflow...!");
}
else
{
printf("\n Enter Value for Element : ");
scanf("%d",&value);
push(value);
SIZE--;
}
break;
case 2:
if(TOS==NULL)
{
printf("\n STACK is Empty");
}
else
{
pop();
SIZE++;
}
break;
case 3:
if(TOS==NULL)
{
printf("\n STACK is Empty");
}
else
{
peek();
}
break;
case 4:
if(SIZE==5)
{
printf("\nStack is Empty...!");
}
else
{
printf("\n Enter Value for Element : ");
scanf("%d",&position);
if(countElement()>=position && position > 0)
{
peep(position);
}
else
{
printf("\n Less no of elements are in Stack than given Position");
}
}
break;
case 5:
if(TOS==NULL)
{
printf("\n STACK is Empty");
}
else
{
displayStack();
}
break;
case 6:
if(TOS==NULL)
{
printf("\n STACK is Empty");
}
else
{
printf("\n No of Elements in Stack = %d",countElement());
}
break;
default:
printf("Thanks for Using Stack Program");
}
getch(); //To stop output on Screen
}while(choice>0 && choice<7);
return 0;
}
//Function Implementation
void printMenu()
{
printf("\n==============================");
printf("\n STACK OPERATION MENU");
printf("\n==============================");
printf("\n 1 - PUSH");
printf("\n 2 - POP");
printf("\n 3 - PEEK");
printf("\n 4 - PEEP");
printf("\n 5 - DISPLAY");
printf("\n 6 - COUNT NO OF ELEMENT");
printf("\n 7 - EXIT");
printf("\n==============================");
}
void push(int value)
{
temp=(struct Element *)malloc(sizeof(struct Element)*1);
temp->value=value;
temp->next=NULL;
if(TOS==NULL)
{
TOS=temp;
}
else
{
temp->next=TOS;
TOS=temp;
}
}
void pop()
{
temp=TOS;
TOS=temp->next;
printf("\n %d is Poped out from Stack",temp->value);
free(temp);
}
void peek()
{
printf("\n PEEK VALUE = %d",TOS->value);
}
void peep(int pos)
{
temp=TOS;
while(pos>1)
{
temp=temp->next;
pos--;
}
printf("\n Value = %d ",temp->value);
}
void displayStack()
{
temp=TOS;
while(temp!=NULL)
{
printf("%d -> ",temp->value);
temp=temp->next;
}
}
int countElement()
{
int pos=0;
temp=TOS;
while(temp!=NULL)
{
pos++;
temp=temp->next;
}
return pos;
}
Comments
Post a Comment