You are on page 1of 44

Data Structures and Algorithms

Hansana Ranaweera
201138

1.
By making Push costly

Push (s,x) //x is the element to be pushed


and s is stack
1. Declare two Queues q1 and q2.
2. Enqueue x to q2.
3. While q1 not empty ,One by one
dequeue everything from q1 and
enqueue to q2.
4. Swapping the names of q1 and q2
//swapping of names is done to avoid one more
movement of all elements.
//from q2 to
q1 Pop(s)
1.dequeue an item from q1 and return it
By making Pop costly
Push(s,x)
1.Enqueue x to q1(assuming size of q1 is
unlimit) Pop(s)
1) One by one dequeue everything except the
last element from q1 and enqueue to q2.
2) Dequeue the last item of q1, the
dequeued item is result, store it.
3) Swap the names of q1 and q2
4) Return the item stored in step 2.
// Swapping of names is done to avoid
one more movement of all elements
// from q2 to q1.
 Describe how the pseudo code algorithm
works

Push costly
Soppose you want to
Push(4) , Push(1) ,push(5), pop() ,push(3)

Step

1 q1

q2

Stack
Step 2
push(
4)
Enqueue 4 to
q2. q1

q2

Stack

Step 3
Push(
1)
Swap q2 to q1.Enqueue 1 to q2
q1

q2

Stack

4 1

Step 4
Push(
5)
dequeue everything from q1 and enqueue to q2.after enque
5 to q1 q1

q2

1 4
Stack

4 1 5

Step 5
Pop()
dequeue first element in q1 and

return it q1

q2

1 4

Stack

4 1

Out put:5
Step 6
Push(
3)
Now q1 is empty.(goto step 4).swap q2 to q1.then enqueue
3 to q2 q1

1 4

q2

Stack

4 1 3

Step 7
Dequeue every thing from q1 and enqueue

to q2 q1
q2

3 1 4

Stack
3
4 1

Pop costly
Suppose you want,
Push(4),push(5),pop()

Step 1

q1

q2
Stack

Step 2
Push(
4)
Enqueue 4 to
q1 q1

q2

Stack

Step 3
Push(5)
Enqueue 5 to
q1 q1

4 5

q2

Stack

4 5

Step 4
Pop()
Enqueue all element from q1 to q2.and dequeue the last
element q1

q2
4

Stack

Out put :5

 Convert the above pseudo-code into c


code

#include
<stdio.h>
#include
<stdlib.h>
#define Max 10

int
Q1[Max];
int
Q2[Max];
int q1_f,q1_r = -
1; int q2_f,q2_r
= -1;
void insert_Q1(int val){
if(q1_r == Max - 1){
printf("Queue
Overflow\n");
}else if(q1_f == -1 && q1_r ==
-1){ q1_f=q1_r=0;
}else{
q1_r = q1_r + 1;
}
Q1[q1_r] = val;
}

int
delete_Q1()
{ int val;
if(q1_f == -1 || q1_f>q1_r){
printf("Queue
Underflow\n");
}else{
val =
Q1[q1_f];
q1_f++;
if(q1_f>q1_r)
{
q1_f=q1_r=-1;
}
}
return val;
}
void insert_Q2(int
val){ if(q2_r ==
Max - 1){
printf("Queue Overflow\n");
}else if(q2_f == -1 && q2_r ==
-1){ q2_f=q2_r=0;
}else{
q2_r = q2_r + 1;
}
Q2[q2_r] = val;
}

int
delete_Q2()
{ int val;
if(q2_f == -1 || q2_f>q2_r){
printf("Queue
Underflow\n");
}else{
val =
Q2[q2_f];
q2_f++;
if(q2_f>q2_r)
{
q2_f=q2_r=-1;
}
}
return val;
}

//Stack

implementation void

push(int val){

if(q1_f == -1 || q1_f >


q1_r){
insert_Q1(val);

}else{
while(q1_f != -1){
insert_Q2(delete_Q1());
}

insert_Q1(val);

while(q2_f != -

1){
insert_Q1(delete_Q2());
}
}
}

int
pop(){
int
val;
if(q1_f == -1 || q1_f >
q1_r){ printf("Stack
Empty\n");
}else{
val = delete_Q1();
}
return val;
}

int peek(){
int val;
if(q1_f == -1 || q1_f > q1_r){
printf("Stack is Empty");
}else{
val = Q1[q1_f];
}
return val;
}
int main() {

int
option;
int val;
do{
printf("1. Push | 2. Pop | 3. Peek | 4.
Exit\nChoice : "); scanf("%d",&option);

switch(option){

case 1:
printf("Enter Value :
");
scanf("%d",&val);
push(val);
break;
case 2:
printf("Pop value =
%d\n",pop()); break;
case 3:
printf("Peek Value = %d\n",peek());

break;
default:
printf("\nEnd\n"
); return 0;
}

}while(option !=

4);

printf("\nEnd\n")

return 0;
}

output
2.I will describe this using Push costly method
1.Push(‘a’)

One by one Enqueue a to q2

q1

q2
a

Stack

4.push(‘b’)
Now q1 is empty .then swap the q2
to q1. enqueue ‘b’ to q2
q1
a

q2

Stack

a b

5. Push(‘c’)
Dequeue ‘a’ and enqueue to q2.then swap q1 and
q2.after enqueue c to q2
q1

b a

q2
c

Stack

a b c

4.pop
Dequeue ‘b’ ,’a’ from q1 and enqueue to q2.after swap
q1 and q2.then .dequeue front in q1
q1 : b a
q2 :
stack:a
b
5.Push(‘d’)
q1 : ba
q2 : d
stack: a b
d
6. Pop
q1 : ba
q2 :
stack: a
b
7.pop
Dequeue item from
q1 q1 : a
q2 :
stack: a
8.pop
Dequeue item from
q1 q1 :
q2 :
stac
k:

9. Push(‘e’)

q2

Stack
e

3. pseudo code algorithm to reverse a


set of numbers stored in a queue(s).
(use a stack as an intermediate
container) Step 1 :start
Step 2: declare a empty stack,empty
queue q1 and empty q2
Step 3: push all elememt to stack
Step 4:while stack is not empty,
One by one pop and enqueue to q1.
Step 5:while q1 is not
empty
One by one dequeue from q1 and print
 Convert the above pseudo code algorithm into a C
program

#include
<stdio.h>
#include
<conio.h>
#define MAX 100
int insert(int k);
int remove();
int
stack[100];
int q1[MAX];
int top=-1;
int
front=0,rear=0;
int val,like,i,k,m;
int main()
{
printf("how many number do you like to
enter:"); scanf("%d",&like);
for(i=0;i<like;i++)
{
printf("Enter :\n");
scanf("%d",&m
);
stack[++top]=
m;
}
while(top!=-1)
{k=stack[top
]; top--;
insert(k)
;
}
while(front<rear)
{
printf("%d",remove());
}
}

int insert(int k)
{
q1[rear]=k
; rear++;
}
int remove()
{
val=q1[front]
; front++;
return val;
}

 Input and out puts


how many number do you like to enter:5
Enter :
1
Enter :
2
Enter :
3
Enter :
4
Enter :
5
5 4 3 2 1

Process exited after 14.58 seconds with return value 0


Press any key to continue
4.
for (k = 1; k ≤ 7; k++)

{Q.enqueue(k);}
final contents of the Q after the following code segment is
executed

9
0

8 3 4 1

2 5
7 6 2
1

6 7
3

5 4
5.
a.print same input
b.out put: 34, 12,18,23,55,11,9

6. pseudo code algorithm to


generate binary numbers between 0 to
n using a queue

Step 1:start
Step 2:declare a empty queue as q1 ,and empty
stack Declare a integer variable y,k
Step 3:Get user input and
scan it Set user input as
variable x
Step 4: set a variable n = 0
While n<=x repeat below steps 5,6 and
7,8 Step 5: Set y = n
Step 6:while y !=0
K= y%2
push k in to stack
Y= y/2
[End while loop]
Step 7:pop element in stack and insert them
to q1 Step 8:while(front<rear)
dequeue q1 and
print [end while
loop]
[End while
loop] Step
9:end

C programe

#include
<stdio.h>
#include
<conio.h>
#define MAX 100
int insert(int k);
int remove();
int
stack[100];
int q1[MAX];
int top=-1;
int front=0,rear=0;
int
val,x,i,k,m,n,y;
int main()
{
printf("Enter
x:\n");
scanf("%d",&x);
printf("\n");
for(n=1;n<=x;n+
+)
{y=n;
while(y!=0
)
{
k=y%
2;
y=y/2;
stack[++top]=k;
}
while(top!=-1)
{
k=stack[top
]; top--;
insert(k);
}
while(front<rear)
{
printf("%d",remove());
}
printf("\n");
}
}
int insert(int k)
{
q1[rear]=k
; rear++;
}
int remove()
{
val=q1[front]
; front++;
return val;
}
Output
7. algorithm to insert an element
into
a queue
Step 1: if rear = max
-1 Print
OVERFLOW
[end of if]
Step 2: if front = -1 and
rear=-1 Set front =
rear=0
else
set rear = rear+1
[end of if]
Step 3:set queue[rear] =
num Step4:exit
8. two problems associated with
the linear queue
1. Time consuming
Linear time to be spent in shifting the elements to
the beginning of the queue.
2. Signaling (indicating) queue full:
even if the queue is having vacant position.
9. algorithm to delete an element
from a circular queue.
Step 1:if front = -1 or front
>rear Print
UNDERFLOW
ELSE
Set val =
queue[front]+1 [end of if]
STEP 2:Exit

10. pseudo code algorithm to Implement a


Queue using two Stacks
EnQueue Costly

enQueue(q, x)
1) While stack1 is not empty, push everything
from stack1 to stack2.
2) Push x to stack1 (assuming size of
stacks is unlimited).
3) Push everything back to
stack1. Here time complexity
will be O(n)

deQueue(q)
1) If stack1 is empty then error
2) Pop an item from stack1 and
return it Here time complexity will be
O(1)

DeQueue Costly
enQueue(q, x)
1) Push x to stack1 (assuming size of
stacks is unlimited).
Here time complexity will be O(1)

deQueue(q)
1) If both stacks are empty then error.
2) If stack2 is empty
While stack1 is not empty, push everything
from stack1 to stack2.
3) Pop the element from stack2 and
return it. Here time complexity will be O(n)

You might also like