JAVA, Python, C++ and C programs for students. Here you will also find solutions to boards papers of ISC Computer Science Practical and CBSE Computer Science. ISC and ICSE JAVA PROGRAMS
Showing posts with label push. Show all posts
Showing posts with label push. Show all posts
Friday, April 11, 2014
Thursday, March 28, 2013
C Program #18
/* Implementation of
queue and few basic operations!*/
#include<stdio.h>
#include<stdlib.h>
#define
MAXSIZE 100
#define
TRUE 1
#define
FALSE 0
struct
queue{
int front;
int rear;
int items[MAXSIZE];
};
struct
stack{
int top;
int items[MAXSIZE];
};
struct
stack stk;
struct
queue p,q,temp;
int
pop(struct stack *);
int
remove(struct queue *);
int
Isempty(struct queue *);
void
push(struct stack *,int);
int
isempty_st(struct stack *);
void
insert(struct queue *,int);
void
reverse_queue(struct queue *);
void
display_front_rear(struct queue *);
void
display_rear_front(struct queue *);
void
append(struct queue *,struct queue *);
void
replace(struct queue *,int,int);
void
display_front_rear_unchanged(struct queue *);
void
display_rear_front_unchanged(struct queue *);
int
main()
{
int choice,x,ins,e;
stk.top=-1;
p.front=p.rear=MAXSIZE-1;
q.front=q.rear=MAXSIZE-1;
temp.front=temp.rear=MAXSIZE-1;
do
{
printf("\n Press 1
to add elements in Queue:");
printf("\n Press 2
to remove elements from Queue:");
printf("\n Press 3
to append queue Q at the end of queue P:");
printf("\n Press 4
to print elements of queue P from front to end with P becoming empty:");
printf("\n Press 5
to print elements of queue P from rear to front with P becoming empty:");
printf("\n Press 6
to print elements of queue P from front to end with P remaining same:");
printf("\n Press 7
to print elements of queue P from rear to front with P remaining same:");
printf("\n Press 8
to reverse the queue P:");
printf("\n Press 9
to replace a specific element of a queue:");
printf("\n Type 10
to exit:");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\n
Enter 1 to insert in queue P, 2 to insert in queue Q:");
scanf("%d",&x);
switch(x)
{
case
1:
printf("\n
Enter the element to be inserted:");
scanf("%d",&ins);
insert(&p,ins);
break;
case
2:
printf("\n
Enter the element to be inserted:");
scanf("%d",&ins);
insert(&q,ins);
break;
}
break;
case 2:
printf("\n
Enter 1 to remove in queue P, 2 to remove in queue Q:");
scanf("%d",&x);
switch(x)
{
case
1:
printf("\n
Element removed is: %d\n",remove(&p));
break;
case
2:
printf("\n
Element removed is: %d\n",remove(&p));
break;
}
break;
case 3:
append(&p,&q);
break;
case 4:
display_front_rear(&p);
break;
case 5:
display_rear_front(&p);
break;
case 6:
display_front_rear_unchanged(&p);
break;
case 7:
display_rear_front_unchanged(&p);
break;
case 8:
reverse_queue(&p);
break;
case 9:
printf("\n
Enter the element to be replaced:");
scanf("%d",&e);
printf("\n
Enter the element to be placed:");
scanf("%d",&x);
replace(&p,e,x);
break;
}
}while(choice>=1
&& choice<=9);
return
0;
}
void
replace(struct queue *p,int e,int x)
{
int y;
while(!Isempty(p))
{
y=remove(p);
if(y==e)
insert(&temp,x);
else
insert(&temp,y);
}
printf("\n Elements of the
queue after replacing\n");
while(!Isempty(&temp))
{
x=remove(&temp);
printf("%d
",x);
insert(p,x);
}
}
void
reverse_queue(struct queue *p)
{
int x;
while(!Isempty(p))
{
x=remove(p);
push(&stk,x);
}
printf("\n Elements of the
queue (after reversing)\n");
while(!isempty_st(&stk))
{
x=pop(&stk);
printf("%d
",x);
insert(p,x);
}
}
void
display_rear_front_unchanged(struct queue *p)
{
int x;
while(!Isempty(p))
{
x=remove(p);
push(&stk,x);
insert(&temp,x);
}
printf("\n Elements of the
queue (rear to front)\n");
while(!isempty_st(&stk))
{
printf("%d
",pop(&stk));
x=remove(&temp);
insert(p,x);
}
}
void
display_front_rear_unchanged(struct queue *p)
{
int x;
while(!Isempty(p))
insert(&temp,remove(p));
printf("\n Elements of the
queue (front to rear)\n");
while(!Isempty(&temp))
{
x=remove(&temp);
printf("%d
",x);
insert(p,x);
}
}
void
display_rear_front(struct queue *p)
{
while(!Isempty(p))
push(&stk,remove(p));
printf("\n Elements of the
queue (rear to front)\n");
while(!isempty_st(&stk))
printf("%d
",pop(&stk));
}
int
isempty_st(struct stack *stk)
{
return((stk->top==-1)?TRUE:FALSE);
}
int
pop(struct stack *stk)
{
return(stk->items[(stk->top)--]);
}
void
push(struct stack *stk,int x)
{
stk->items[++stk->top]=x;
}
void
display_front_rear(struct queue *q)
{
printf("\n Elements of the
queue (front to end)\n");
while(!Isempty(q))
printf("%d
",remove(q));
}
void
append(struct queue *p, struct queue *q)
{
while(!Isempty(q))
insert(p,remove(q));
printf("\n
Appended........\n");
}
int
remove(struct queue *q)
{
if(Isempty(q))
{
printf("\n
Queue underflow:");
exit(1);
}
if(q->front==MAXSIZE-1)
q->front=0;
else
(q->front)++;
return(q->items[q->front]);
}
int
Isempty(struct queue *q)
{
return((q->front==q->rear)?TRUE:FALSE);
}
void
insert(struct queue *q,int x)
{
if(q->rear==MAXSIZE-1)
q->rear=0;
else
(q->rear)++;
if(q->front==q->rear)
printf("\n Queue
Overflow...:");
q->items[q->rear]=x;
return;
}
Labels:
algorithm,
Assignment,
jadavpur,
M.C.A,
malloc,
pointers,
postfix,
push,
queue,
stack,
university
Wednesday, March 27, 2013
C Program #16
INFIX TO POSTFIX CONVERSION
The programme doesn’t check whether
the infix input is valid or not. The operators allowed are ‘^’, ’*’, ‘/’, ‘+’, ‘-‘
for exponential, multiplication, division, addition and subtraction
respectively. No brackets are allowed in the infix expression (otherwise
incorrect expression for postfix or run time crash)
#include<stdio.h>
#include<stdlib.h>
#define MAXCOLS 80
#define TRUE 1
#define FALSE 0
struct stack
{
int
top;
char
items[MAXCOLS];
};
void
postfix(char *,char *);
int
isoperand(char);
void
pop_test(struct stack *,char *,int *);
int
precedence(char,char);
void
push(struct stack *,char);
char
pop(struct stack *);
int empty(struct stack *);
int
main()
{
char
infix[MAXCOLS],postx[MAXCOLS],c;
int
pos=0;
printf("\nEnter
the infix expression:");
while((infix[pos++]=getchar())!='\n');
infix[--pos]='\0';
printf("\nThe
infix expression is:\n");
puts(infix);
postfix(infix,postx);
return
0;
}
int precedence(char op1,char op2)
{
switch(op1)
{
case
'^':
if(op2=='^')
return
(FALSE);
else
return
(TRUE);
break;
case
'*':
if(op2=='^')
return
(FALSE);
else
return
(TRUE);
break;
case
'/':
if(op2=='^')
return
(FALSE);
else
return
(TRUE);
break;
case
'+':
if(op2=='^'
|| op2=='*' ||op2=='/')
return
(FALSE);
else
return
(TRUE);
break;
case
'-':
if(op2=='^'
|| op2=='*' ||op2=='/')
return
(FALSE);
else
return
(TRUE);
break;
}
}
int isoperand(char symb)
{
if(symb
=='^' ||symb =='*' ||symb =='/' ||symb =='+' ||symb =='-')
return
(FALSE);
else
return
(TRUE);
}
void pop_test(struct stack *ps, char
*topsymp,int *und)
{
if(empty(ps))
{
*und=TRUE;
return;
}
*und=FALSE;
*topsymp=ps->items[ps->top--];
return;
}
void push(struct stack *ps, char x)
{
ps->items[++(ps->top)]=x;
}
char pop(struct stack *ps)
{
return
(ps->items[ps->top--]);
}
int empty(struct stack *ps)
{
if(ps->top==-1)
return
(TRUE);
else
return
(FALSE);
}
void postfix(char infix[],char postx[])
{
int
position,und;
int
outpos=0;
char
topsymb='+';
char
symb;
struct
stack opstk;
opstk.top=-1;
for(position=0;(symb=infix[position])!='\0';position++)
if(isoperand(symb))
postx[outpos++]=symb;
else
{
pop_test(&opstk,&topsymb,&und);
while(!und
&& precedence(topsymb,symb))
{
postx[outpos++]=topsymb;
pop_test(&opstk,&topsymb,&und);
}
if(!und)
push(&opstk,topsymb);
if(und
|| (symb!=')'))
push(&opstk,symb);
else
topsymb=pop(&opstk);
}
while(!empty(&opstk))
postx[outpos++]=pop(&opstk);
postx[outpos]='\0';
printf("\nThe
coressponding postfix expression is:");
puts(postx);
return;
}
Labels:
algorithm,
Assignment,
C,
infix,
Isempty,
jadavpur,
M.C.A,
pop,
postfix,
push,
stack,
university
Subscribe to:
Posts (Atom)