Stack_in_c
Stack_in_c
Stacks are linear Data Structures which are based on the principle of
Last-In-First-Out (LIFO) where data which is entered last will be the first to
get accessed. It is built using the array structure and has operations
namely, pushing (adding) elements, popping (deleting) elements and
accessing elements only from one point in the stack called as the TOP. This
TOP is the pointer to the current position of the stack. Stacks are
prominently used in applications such as Recursive Programming, reversing
words, undo mechanisms in word editors and so forth.
In the above image, although item was kept last, it was removed first - so it
follows the Last In First Out(LIFO) principle.
Basic Operations of Stack
A stack is an object (an abstract data type - ADT) that allows the following
operations:
if stack is full
return null
endif
top = top + 1
stack[top] = data
end procedure
if stack is empty
return null
endif
data ← stack[top]
top ← top - 1
return data
end procedure
Program1: - Implemening Stack using structure in c
/*
* C program to implement Stack. Stack is a LIFO data structure.
* Stack operations: PUSH(insert operation), POP(Delete operation)
* and Display Stack.
*/
#include <stdio.h>
#include<conio.h>
#define MAX 10
struct Stack
{
int Item[MAX];
int Top;
};
Init(&S);
do
{ clrscr();
printf ("Stack OPERATION\n");
printf ("------------------------------------------\n");
printf (" 1 --> PUSH \n");
printf (" 2 --> POP \n");
printf (" 3 --> DISPLAY \n");
printf (" 4 --> EXIT \n");
printf ("------------------------------------------\n");
Here we have written '+' operator between the operands A and B, and the -
operator in between the C and D operand.
CH Expression Stack
( (
( ((
A A ((
* A ((*
( A ((*(
B AB ((*(
+ AB ((*(+
D ABD ((*(+
) ABD+ ((*
/ ABD+ ((*/
E ABD+*E ((/
) ABD+*E/ (
- ABD+*E/ (-
( ABD+*E/ (-(
F ABD+*E/F (-(
* ABD+*E/F (-(*
( ABD+*E/F (-(*(
G ABD+*E/FG (-(*(
+ ABD+*E/FG (-(*(+
H ABD+*E/FGH (-(*(+
/ ABD+*E/FGH (-(*(+/
K ABD+*E/FGHK (-(*(+/
) ABD+*E/FGHK/+ (-(*
) ABD+*E/FGHK/+* (-
) ABD+*E/FGHK/+*-
Postfix Evaluation Algorithm
Reverse Of Infix : ) ) ) K / H + G ( * F ( - ) E / ) D + B ( * A ( (
Modified Expression: ( ( ( K / H + G ) * F ) - ( E / ( D + B ) * A ) )
CH Expression Stack
( (
( ((
( (((
K K (((
/ K (((/
H KH (((/
+ KH/ (((+
G KH/G (((+
) KH/G+ ((
* KH/G+ ((*
F KH/G+F ((*
) KH/G+F* (
- KH/G+F* (-
( KH/G+F* (-(
E KH/G+F*E (-(
/ KH/G+F*E (-(/
( KH/G+F*E (-(/(
D KH/G+F*ED (-(/(
+ KH/G+F*ED (-(/(+
B KH/G+F*EDB (-(/(+
) KH/G+F*EDB+ (-(/
* KH/G+F*EDB+ (-(/*
A KH/G+F*EDB+A (-(/*
) KH/G+F*EDB+A*/ (-
) KH/G+F*EDB+A*/- Empty
-/*A+BDE*F+G/HK Reverse and Display
Expression
Algorithm for Prefix to Infix:
CH Expression Stack
K K
H KH
/ (H/K) (H/K)
G (H/K),G
+ G+(H/K) G+(H/K)
F G+(H/K),F
* F*(G+(H/K)) F*(G+(H/K))
E F*(G+(H/K)),E
D F*(G+(H/K)),E,D
B F*(G+(H/K)),E D,B
+ (B+D) F*(G+(H/K)),E,(B+D)
A F*(G+(H/K)),E,( B+D),A
* A*( B+D), F*(G+(H/K)),E,( A*( B+D))
/ ( A*( B+D))/E F*(G+(H/K)),( ( A*( B+D))/E)
- ( ( A*( B+D))/E)- F*(G+(H/K)) ( ( A*( B+D))/E)- F*(G+(H/K))
struct Stack
{
char Item[MAX];
int Top;
};
int IsOperator(char c)
{
if(c=='+'||c=='-'||c=='*'||c=='/'||c=='^')
return 1;
else
return 0;
}
int Priority(char c)
{
if(c=='^')
return 3;
else if(c=='*'||c=='/')
return 2;
else if(c=='+'||c=='-')
return 1;
else
return 0;
}
void main ()
{
struct Stack S;
char Infix[30],ch,ans;
int i,j;
do
{
Init(&S);
char Postfix[30];j=-1;
clrscr();
printf("Infix String :");
scanf("%s",&Infix);
for(i=0;Infix[i]!='\0';i++)
{
ch=Infix[i];
if(ch=='(')
Push(&S,ch);
else if(IsOperator(ch))
{
while(Priority(S.Item[S.Top])>=Priority(ch))
{
//printf("%c",Pop(&S));
Postfix[++j]=Pop(&S);
}
Push(&S,ch);
}
else if(ch==')')
{
while(S.Item[S.Top]!='(')
{
//printf("%c",Pop(&S));
Postfix[++j]=Pop(&S);
}
Pop(&S);
}
else
//printf("%c",ch);
Postfix[++j]=ch;
}
while( !IsEmpty(&S))
{
//printf("%c",Pop(&S));
Postfix[++j]=Pop(&S);
}
Postfix[++j]='\0';
printf("\nPostfix Notation = %s",Postfix);
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define MAX 30
struct Stack
{
int Item[MAX];
int Top;
};
int IsOperator(char c)
{
if(c=='+'||c=='-'||c=='*'||c=='/'||c=='^')
return 1;
else
return 0;
}
void main ()
{
struct Stack S;
char Postfix[30],ch,ans;
int exp,op1,op2,i;
Init(&S);
do
{
clrscr();
printf("Postfix String :");
scanf("%s",&Postfix);
printf("\nInfix String = ");
for(i=0;Postfix[i]!='\0';i++)
{
ch=Postfix[i];
if(IsOperator(ch))
{
op2=Pop(&S);
op1=Pop(&S);
switch(ch)
{
case '+': exp=op1+op2; break;
case '-': exp=op1-op2; break;
case '*': exp=op1*op2; break;
case '/': exp=op1/op2; break;
case '^': exp=pow(op1,op2); break;
}
Push(&S,exp);
}
else
Push(&S,ch-48);
}
printf("%d",Pop(&S));
printf("\n Press Y to Continue...");
scanf("%s",&ans);
}while(ans=='y'||ans=='Y');
}
/* program to implement Infix to Prefix Using Stack. */
#include<stdio.h>
#include<conio.h>
#include<string.h>
#define MAX 30
struct Stack
{
char Item[MAX];
int Top;
};
int IsOperator(char c)
{
if(c=='+'||c=='-'||c=='*'||c=='/'||c=='^')
return 1;
else
return 0;
}
int Priority(char c)
{
if(c=='^')
return 3;
else if(c=='*'||c=='/')
return 2;
else if(c=='+'||c=='-')
return 1;
else
return 0;
}
void main ()
{
struct Stack S;
char Infix[30],ch,ans;
int i,j;
do
{
Init(&S);
char Prefix[30];j=-1;
clrscr();
printf("Infix String :");
scanf("%s",&Infix);
strrev(Infix);
for(i=0;Infix[i]!='\0';i++)
{
ch=Infix[i];
if(ch==')')
Push(&S,ch);
else if(IsOperator(ch))
{
while(Priority(S.Item[S.Top])>Priority(ch))
{
//printf("%c",Pop(&S));
Prefix[++j]=Pop(&S);
}
Push(&S,ch);
}
else if(ch=='(')
{
while(S.Item[S.Top]!=')')
{
//printf("%c",Pop(&S));
Prefix[++j]=Pop(&S);
}
Pop(&S);
}
else
//printf("%c",ch);
Prefix[++j]=ch;
}
while( !IsEmpty(&S))
{
//printf("%c",Pop(&S));
Prefix[++j]=Pop(&S);
}
Prefix[++j]='\0';
printf("\nPrefix Notation = %s",strrev(Prefix));
#include <stdio.h>
#include<conio.h>
#define MAX 10
class Stack
{
int Item[MAX];
int Top;
public :
S.Init();
do
{
clrscr();
printf ("Stack OPERATION\n");
printf ("------------------------------------------\n");
printf (" 1 --> PUSH \n");
printf (" 2 --> POP \n");
printf (" 3 --> DISPLAY \n");
printf (" 4 --> EXIT \n");
printf ("------------------------------------------\n");