/* STACK*/
#include<stdio.h>
#include<conio.h>
#define STACKSIZE 100
#define TRUE 1
#define FALSE 0
FILE *fp;
struct stack
{
int
items[STACKSIZE];
int top;
};
struct stack s,s1;
void push(struct stack *ps);
void push1(struct stack *ps,int x);
void pop(struct stack *ps);
int pop1(struct stack *ps);
int isempty(struct stack *ps);
void display(struct stack *ps);
void reverse(struct stack *ps);
void filestack(struct stack *ps, FILE *fp);
void duplicate(struct stack *ps);
void secondStack(struct stack *ps);
int IsStackequal(struct stack *ps,struct stack *ps1);
int main()
{
fp=fopen("data.txt","w");
int choice,x;
s.top=s1.top=-1;
do
{
printf("\n
Press 1 to push a element:");
printf("\n
Press 2 to pop a element:");
printf("\n
Press 3 to test for empty:");
printf("\n
Press 4 to display the elements of stack in order of insertion:");
printf("\n
Press 5 to display the elements of stack in reverse order:");
printf("\n
Press 6 to read from a file and print in reverse order:");
printf("\n
Press 7 to duplicate the top element:");
printf("\n
Press 8 to enter elements in the second array:");
printf("\n
Press 9 to display the second stack:");
printf("\n
Press 10 to compare stack one and two:");
printf("\n
Enter choice:");
printf("\n
Type 7 to exit:");
scanf("%d",&choice);
switch(choice)
{
case
1:
push(&s);
break;
case
2:
pop(&s);
case
3:
x=isempty(&s);
if(x==TRUE)
printf("\n
Stack is empty:\n");
else
printf("\n
Stack is not empty");
break;
case
4:
display(&s);
break;
case
5:
reverse(&s);
break;
case
6:
filestack(&s,fp);
break;
case
7:
duplicate(&s);
break;
case
8:
secondStack(&s1);
break;
case
9:
display(&s1);
break;
case
10:
x=IsStackequal(&s,&s1);
if(x==TRUE)
printf("\n
Both Stacks are equal:\n");
else
printf("\n
Stacks are unequal:\n");
break;
default:
break;
}
}
while(1<=choice
&& choice<=10);
return 0;
}
int IsStackequal(struct stack *ps,struct stack *ps1)
{
int
flag=1,k=ps->top,i;
if(ps->top !=
ps1->top)
return
(FALSE);
else
{
for(i=0;i<k;i++)
if(ps->items[i]==ps1->items[i])
continue;
else
{
flag=0; break;}
}
if(flag=1)
return
(TRUE);
else
return (FALSE);
}
void secondStack(struct stack *ps)
{
int x=1;
printf("\n
Type -999 to exit enetering:");
while(x!=-999 ||
ps->top==STACKSIZE-1)
{
printf("\n
Enter the element:");
scanf("%d",&x);
if(x==-999)
break;
push1(&s1,x);
}
}
void duplicate(struct stack *ps)
{
int x;
if(ps->top==STACKSIZE-1
|| isempty(&s))
printf("\n
Element cannot be duplicated:");
else
{
x=pop1(&s);
push1(&s,x);
push1(&s,x);
}
}
int pop1(struct stack *ps)
{
return(ps->items[ps->top--]);
}
void push1(struct stack *ps,int x)
{
if(ps->top==STACKSIZE-1)
printf("\n
Stack Overflow:");
else
ps->items[++(ps->top)]=x;
}
void filestack(struct stack *ps, FILE *fp)
{
int n,i,x;
printf("\n
Enter the number of elements to be entered in file:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("\n
Enter the %d# elemet:",i);
scanf("%d",&x);
fprintf(fp,"%d
",x);
}
fclose(fp);
fp=fopen("data.txt","r");
for(i=1;i<=n;i++)
{
fscanf(fp,"%d",&x);
// printf("%d",x);
push1(&s,x);
}
printf("\n
Data from file in reverse order:");
while(!isempty(&s))
printf("%d ",pop1(&s));
fclose(fp);
}
int isempty(struct stack *ps)
{
if(ps->top==-1)
return(TRUE);
else
return(FALSE);
}
void reverse(struct stack *ps)
{
int d;
d=ps->top+1;
while(d--!=0)
printf("%d
",ps->items[d]);
}
void pop(struct stack *ps)
{
if(ps->top==-1)
{
printf("\n
Stack is empty, element cannot be popped:\n");
}
else
{
printf("\n
Element popped out is %d\n",ps->items[ps->top--]);
}
}
void push(struct stack *ps)
{
int x;
if(ps->top==STACKSIZE-1)
{
printf("\n
Stack Overflow:");
}
else
{
printf("\n
Enter the element to be inserted:");
scanf("%d",&x);
ps->items[++(ps->top)]=x;
}
}
void display(struct stack *ps)
{
printf("\n");
int i;
printf("\n
Elements of the stack are:");
for(i=0;i<=ps->top;i++)
{
printf("%d
",ps->items[i]);
}
}
No comments:
Post a Comment