#include <stdio.h>
#include<conio.h>
#define MAX 5
int top, flag;
void push (int s[], int data)
{ if (top == (MAX-1))
flag = 0;
else
{ flag = 1;
++top;
s [top] = data;
}
}
int pop (int s[])
{
int ret;
if (top == -1)
{ ret = 0;
flag = 0;
}
else
{ flag = 1;
ret = s [top];
--top;
}
return ret;
}
void display (int s[])
{ int i;
printf ("\nThe s is: ");
if (top == -1)
printf ("empty");
else
{ for (i=top; i>=0; --i)
printf ("\n--------\n|%3d |\n--------",s[i]);
}
printf ("\n");
}
void main()
{
int s [MAX], data;
int ch;
clrscr ();
top = -1;
do
{ do
{ printf ("\nMAIN MENU");
printf ("\n1.PUSH (Insert) into the s");
printf ("\n2.POP (Delete) from the s");
printf ("\n3.Exit.");
printf ("\nEnter Your Choice: ");
scanf ("%d", &ch);
if (ch<1 || ch>3)
printf ("\nInvalid Choice, Please try again");
}while (ch<1 || ch>3);
switch (ch)
{case 1:
printf ("\nEnter the Element to be pushed : ");
scanf ("%d", &data);
printf (" %d", data);
push (s, data);
if (flag)
{ printf ("\nAfter Pushing ");
display (s);
if (top == (MAX-1))
printf ("\nThe s is Full");
}
else
printf ("\ns overflow on Push");
break;
case 2:
data = pop (s);
if (flag)
{ printf ("\nThe Popped data is %d. After Popping: ");
display (s);
}
else
printf ("\ns underflow on Pop");
break;
default:
printf ("\nEnd.");
}
}while (ch != 3);
getch();
}
Friday, 27 April 2012
Another Program for PUSH and POP operations in a Stack using Arrays in C
Labels:
Arrays,
Basic operations on Stacks,
Basic Programs in C,
C,
C Programs for B.Tech CSE,
C Programs for Engineers,
Data Structures and Algorithms in C,
Implementations in C,
Important C Programs for Interviews,
Important Programs in C++,
Interesting Programs in C Language,
Programming for Engineers,
Stack PUSH and POP,
Stacks in C
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment