/*Serial No.153 [swami135.cpp]*/
#include<stdio.h>
#include<conio.h>
#define max 5
void push(int *S, int *top)
{
int ele;
if(*top<max-1)
{
(*top)++;
printf("Enter the element to insert= ");
scanf("%d",&ele);
*(S+*top)=ele;
}
else
printf("\nStack is full.\n");
}
void display_stack(int *S, int top)
{
printf("\nElements in the stack are:\n");
if(top==-1)
printf("\nStack is empty.\n");
else
{
for(int i=0;i<=top;i++)
{
printf("%d ",*(S+i));
}
}
}
void pop(int *S, int *top)
{
if(*top==-1)
printf("\nStack is Empty.You can't pop.\n");
else
{
int value=*(S+*top);
(*top)--;
printf("\nElement poped= %d\n",value);
}
}
void main()
{
int choice,tos=-1,stack[max];
clrscr();
do
{
printf("\n\t[1] Push");
printf("\n\t[2] Pop");
printf("\n\t[3] Display");
printf("\n\t[7] Exit");
printf("\nEnter your choice= ");
scanf("%d",&choice);
switch(choice)
{
case 1: push(stack,&tos);
break;
case 2: pop(stack,&tos);
break;
case 3: display_stack(stack,tos);
}
}while(choice!=7);
getch();
}
Showing posts with label Stack. Show all posts
Showing posts with label Stack. Show all posts
Tuesday, July 19, 2011
Implementation of stack using two Queues {using pointers}
/*Serial No.152 [swami134.cpp]*/
/*VERSION 2*/
#include<stdio.h>
#include<conio.h>
#define maxsize 4
int front1=-1,rear1=-1,front2=-1,rear2=-1;
int queue1[maxsize],queue2[maxsize];
void display()
{
if(front1==-1&&rear1==-1)
printf("\nStack is empty.");
else
{
printf("\nElements in Stack in reversed order are \n");
for(int i=front1;i<=rear1;i++)
{
printf("%d ",queue1[i]);
}
}
}
void enqueue(int *Q,int ele,int *f, int *r)
{
if(*r>=maxsize-1)
{
printf("\nQueue is full. Value can't be added.");
return;
}
if(*f==-1)
{
(*f)++;
}
(*r)++;
*(Q+*r)=ele;
}
int dequeue(int *Q,int *f, int *r)
{
int ele;
if(*f==*r)
{
ele=*(Q+*f);
*f=-1;
*r=-1;
}
else
{
ele=*(Q+*f);
(*f)++;
}
return(ele);
}
void main()
{
int choice,ele,temp;
clrscr();
printf("\n\t[1] Push");
printf("\n\t[2] Pop");
printf("\n\t[3] Display");
printf("\n\t[7] Exit");
do
{
printf("\nEnter your choice= ");
scanf("%d",&choice);
switch(choice)
{
case 1:
if(rear1>=maxsize-1)
{
printf("\nStack is full. Value can't be added.");
break;
}
printf("\nEntere the element to insert= ");
scanf("%d",&ele);
if(front1==-1) /*for 1st element of stack*/
{
front1=rear1=0;
queue1[rear1]=ele;
break;
}
else
{
enqueue(queue2,ele,&front2,&rear2);
/*transfer elements of queueu1 to queue2*/
while((front1<=rear1)&&(front1!=-1))
{
temp=dequeue(queue1,&front1,&rear1);
enqueue(queue2,temp,&front2,&rear2);
}
/*transfer elements of queueu2 to queue1*/
while((front2<=rear2)&&(front2!=-1))
{
temp=dequeue(queue2,&front2,&rear2);
enqueue(queue1,temp,&front1,&rear1);
}
}
break;
case 2: if(front1==-1&&rear1==-1)
printf("\nStack is empty. Value can't be deleted.");
else
{
ele=dequeue(queue1,&front1,&rear1);
printf("\nDeleted Element from stack= %d",ele);
}
break;
case 3: display();
}
}while(choice!=7);
getch();
}
/*VERSION 2*/
#include<stdio.h>
#include<conio.h>
#define maxsize 4
int front1=-1,rear1=-1,front2=-1,rear2=-1;
int queue1[maxsize],queue2[maxsize];
void display()
{
if(front1==-1&&rear1==-1)
printf("\nStack is empty.");
else
{
printf("\nElements in Stack in reversed order are \n");
for(int i=front1;i<=rear1;i++)
{
printf("%d ",queue1[i]);
}
}
}
void enqueue(int *Q,int ele,int *f, int *r)
{
if(*r>=maxsize-1)
{
printf("\nQueue is full. Value can't be added.");
return;
}
if(*f==-1)
{
(*f)++;
}
(*r)++;
*(Q+*r)=ele;
}
int dequeue(int *Q,int *f, int *r)
{
int ele;
if(*f==*r)
{
ele=*(Q+*f);
*f=-1;
*r=-1;
}
else
{
ele=*(Q+*f);
(*f)++;
}
return(ele);
}
void main()
{
int choice,ele,temp;
clrscr();
printf("\n\t[1] Push");
printf("\n\t[2] Pop");
printf("\n\t[3] Display");
printf("\n\t[7] Exit");
do
{
printf("\nEnter your choice= ");
scanf("%d",&choice);
switch(choice)
{
case 1:
if(rear1>=maxsize-1)
{
printf("\nStack is full. Value can't be added.");
break;
}
printf("\nEntere the element to insert= ");
scanf("%d",&ele);
if(front1==-1) /*for 1st element of stack*/
{
front1=rear1=0;
queue1[rear1]=ele;
break;
}
else
{
enqueue(queue2,ele,&front2,&rear2);
/*transfer elements of queueu1 to queue2*/
while((front1<=rear1)&&(front1!=-1))
{
temp=dequeue(queue1,&front1,&rear1);
enqueue(queue2,temp,&front2,&rear2);
}
/*transfer elements of queueu2 to queue1*/
while((front2<=rear2)&&(front2!=-1))
{
temp=dequeue(queue2,&front2,&rear2);
enqueue(queue1,temp,&front1,&rear1);
}
}
break;
case 2: if(front1==-1&&rear1==-1)
printf("\nStack is empty. Value can't be deleted.");
else
{
ele=dequeue(queue1,&front1,&rear1);
printf("\nDeleted Element from stack= %d",ele);
}
break;
case 3: display();
}
}while(choice!=7);
getch();
}
Program for stack using Array {push, pop, display}
/*Serial No.140 [swami122.cpp]*/
#include<stdio.h>
#include<conio.h>
int top=-1,stack_arr[5];
void push()
{
if(top<4)
{
top++;
printf("Enter the element to insert= ");
scanf("%d",&stack_arr[top]);
}
else
printf("\nStack is full.\n");
}
void display_stack()
{
printf("\nElements in the stack are:\n");
if(top==-1)
printf("\nStack is empty.\n");
else
{
for(int i=0;i<=top;i++)
{
printf("%d ",stack_arr[i]);
}
}
}
void pop()
{
if(top==-1)
printf("\nStack is Empty.You can't pop.\n");
else
{
int value=stack_arr[top];
top--;
printf("\nElement poped= %d\n",value);
}
}
void main()
{
int choice;
clrscr();
do
{
printf("\n\t[1] Push");
printf("\n\t[2] Pop");
printf("\n\t[3] Display");
printf("\n\t[7] Exit");
printf("\nEnter your choice= ");
scanf("%d",&choice);
switch(choice)
{
case 1: push();
break;
case 2: pop();
break;
case 3: display_stack();
}
}while(choice!=7);
getch();
}
#include<stdio.h>
#include<conio.h>
int top=-1,stack_arr[5];
void push()
{
if(top<4)
{
top++;
printf("Enter the element to insert= ");
scanf("%d",&stack_arr[top]);
}
else
printf("\nStack is full.\n");
}
void display_stack()
{
printf("\nElements in the stack are:\n");
if(top==-1)
printf("\nStack is empty.\n");
else
{
for(int i=0;i<=top;i++)
{
printf("%d ",stack_arr[i]);
}
}
}
void pop()
{
if(top==-1)
printf("\nStack is Empty.You can't pop.\n");
else
{
int value=stack_arr[top];
top--;
printf("\nElement poped= %d\n",value);
}
}
void main()
{
int choice;
clrscr();
do
{
printf("\n\t[1] Push");
printf("\n\t[2] Pop");
printf("\n\t[3] Display");
printf("\n\t[7] Exit");
printf("\nEnter your choice= ");
scanf("%d",&choice);
switch(choice)
{
case 1: push();
break;
case 2: pop();
break;
case 3: display_stack();
}
}while(choice!=7);
getch();
}
Subscribe to:
Posts (Atom)