The Below program performs Stack operations like push and pop on a 1)Linked List Stack and 2)Array Stack. This is a part of Mumbai University MCA Colleges Data Structure C-Program MCA Sem 2
#include<stdio.h>
#include<conio.h>
#define MAX 5
void Sarray();
void push1();
void pop1();
void display1();
int top1 = -1;
int stack_arr[MAX];
void Slink();
void push();
void pop();
void display();
struct node
{
int info;
struct node *link;
} *top=NULL;
void main()
{
int n;
clrscr();
while(1)
{
printf("\n\n--------------------------- MENU FOR STACK -----------------------------\n");
printf("\n1.STACK USING ARRAY.\n\n");
printf("2.STACK USING LINKED LIST.\n\n");
printf("3.EXIT.\n\n");
printf("Enter Your Choice : ");
scanf("%d",&n);
switch(n)
{
case 1:
Sarray();
break;
case 2:
Slink();
break;
case 3:
exit(1);
default:
printf("\nINVALID CHOICE TRY AGAIN!!!!!!!!!!!!!\n");
}
}
}
void Slink()
{
int choice;
printf("\n\n----------------------- STACK USING LINKED LIST ----------------------\n");
while(1)
{
printf("\n\n-------------------------- MAIN MENU -----------------------\n");
printf("\n\n1.Push.\n\n");
printf("2.Pop.\n\n");
printf("3.Display.\n\n");
printf("4.Quit.\n\n");
printf("Enter your choice : ") ;
scanf("%d", &choice);
switch(choice)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
exit(1);
default :
printf("INVALID CHOICE TRY AGAIN!!!!!!!!!!!!!!\n");
}
}
}
void push()
{
struct node *tmp;
int pushed_item;
tmp = (struct node *)malloc(sizeof(struct node));
printf("\n\nEnter the item to be pushed in stack : ");
scanf("%d",&pushed_item);
tmp->info=pushed_item;
tmp->link=top;
top=tmp;
}
void pop()
{
struct node *tmp;
if(top == NULL)
{
printf("\n\n-------------------------------------------------------------\n\n");
printf("Stack is empty..............there is no element to be popped\n");
printf("\n\n-------------------------------------------------------------\n\n");
}
else
{
tmp=top;
printf("\n\n-------------------------------------------------------------\n\n");
printf("Popped item is %d\n",tmp->info);
printf("\n\n-------------------------------------------------------------\n\n");
top=top->link;
free(tmp);
}
}
void display()
{
struct node *ptr;
ptr=top;
if(top==NULL)
{
printf("\n\n---------------------------------------------------------------------\n\n");
printf("Stack is empty...............there is no element to be displayed\n");
printf("\n\n---------------------------------------------------------------------\n\n");
}
else
{
printf("\n--------------------- Stack elements -------------------------\n\n");
while(ptr!= NULL)
{
printf("%d\n\n",ptr->info);
ptr = ptr->link;
}
}
}
void Sarray()
{
int choice;
printf("\n\n--------------------------- STACK USING ARRAY ----------------------\n\n");
while(1)
{
printf("\n------------------------------ MAIN MENU ----------------------\n\n");
printf("1.Push.\n\n");
printf("2.Pop.\n\n");
printf("3.Display.\n\n");
printf("4.Quit.\n\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1 :
push1();
break;
case 2:
pop1();
break;
case 3:
display1();
break;
case 4:
exit(1);
default:
printf("\nINVALID CHOICE TRY AGAIN!!!!!!!!!!!!!!!!\n");
}
}
}
void push1()
{
int pushed_item;
if(top1 == (MAX-1))
{
printf("\n\n-----------------------------------------------------------------\n\n");
printf("Stack Overflow.............don't push anymore elements");
printf("\n\n-----------------------------------------------------------------\n\n");
}
else
{
printf("\nEnter the item to be pushed in stack : ");
scanf("%d",&pushed_item);
top1=top1+1;
stack_arr[top1] = pushed_item;
}
}
void pop1()
{
if(top1 == -1)
{
printf("\n\n-----------------------------------------------------------------\n\n");
printf("Stack Underflow..............there is no element to be popped\n");
printf("\n\n-----------------------------------------------------------------\n\n");
}
else
{
printf("\n\n-----------------------------------------------------------------\n\n");
printf("Popped element is : %d\n",stack_arr[top1]);
printf("\n\n-----------------------------------------------------------------\n\n");
top1=top1-1;
}
}
void display1()
{
int i;
if(top1 == -1)
{
printf("\n\n-----------------------------------------------------------------\n\n");
printf("Stack is empty..............there is no ekement to be displayed\n");
printf("\n\n-----------------------------------------------------------------\n\n");
}
else
{
printf("\n----------------------------Stack elements----------------------------\n\n");
for(i = top1; i >=0; i--)
printf("%d\n\n", stack_arr[i] );
}
}
Hope this Program is useful to you in some sense or other. Keep on following this blog for more Mumbai University MCA College Programs. Happy Programming and Studying.
Hope this Program is useful to you in some sense or other. Keep on following this blog for more Mumbai University MCA College Programs. Happy Programming and Studying.
No comments:
Post a Comment