0% found this document useful (0 votes)
42 views5 pages

Algorithm and Programs in Stack and Queues

The document describes algorithms for common operations on different data structures: - Push and pop algorithms for a stack store and remove elements from the top of the stack. - Push and pop algorithms for a linear queue add elements to the rear and remove from the front. - A dequeue supports adding and removing from both front and rear. - A circular queue uses modulo to wrap elements around the queue when the front or rear reaches the end.

Uploaded by

Adithyan M Nair
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views5 pages

Algorithm and Programs in Stack and Queues

The document describes algorithms for common operations on different data structures: - Push and pop algorithms for a stack store and remove elements from the top of the stack. - Push and pop algorithms for a linear queue add elements to the rear and remove from the front. - A dequeue supports adding and removing from both front and rear. - A circular queue uses modulo to wrap elements around the queue when the front or rear reaches the end.

Uploaded by

Adithyan M Nair
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

ALGORITHM FOR PUSH IN STACK:

STEP 1: START
STEP 2: IF TOP = CAPACITY then STACK OVERFLOW, exit
Else
STEP 3: TOP = TOP +1
STEP 4: STACK[TOP] = VALUE
STEP 5: END

ALGORITHM FOR POP IN STACK:

STEP 1: START
STEP 2: IF TOP < 0 then STACK UNDERFLOW, exit
Else
STEP 3: VALUE = STACK[TOP]
STEP 4: TOP=TOP - 1
STEP 4: END

class Stack
{ int s[], cap, top;
Stack(int nn)
{ cap=nn;
top= -1;
s = new int[cap];
}
void push(int v)
{ if( top<cap-1)
s[++top]=v;
else
System.out.println("STACK FULL");
}
int pop()
{ if(top>=0)
return(s[top--]);
else
return -9999;
}
void display()
{ for(int i=top;i>=0;i--)
System.out.println(s[i]);
}
}
ALGORITHM FOR PUSH IN LINEAR QUEUE:

STEP 1: START
STEP 2: IF REAR = CAPACITY then QUEUE OVERFLOW, exit
Else
STEP 3: REAR = REAR +1
STEP 4: QUEUE[REAR] = VALUE
STEP 5: END

ALGORITHM FOR POP IN LINEAR QUEUE:

STEP 1: START
STEP 2: IF FRONT=REAR then QUEUE UNDERFLOW, exit
Else
STEP 3: FRONT=FRONT + 1
STEP 4: VALUE = QUEUE[FRONT]
STEP 4: END

class Lqueue
{ int q[], cap, front,rear;
Lqueue(int nn)
{ cap=nn;
front=rear=0;
q = new int[cap];
}
void push(int v)
{ if( rear<cap-1)
q[++rear]=v;
else
System.out.println("QUEUE FULL");
}
int pop()
{ if(front !=rear)
return(q[++front]);
else
return -9999;
}
void display()
{ for(int i=front+1;i<=rear;i++)
System.out.println(q[i]);
}
}
ALGORITHM FOR PUSH FROM FRONT IN DEQUEUE:

STEP 1: START
STEP 2: IF FRONT = 0 then QUEUE OVERFLOW FROM FRONT, exit
Else
STEP 3: QUEUE[FRONT] = VALUE
STEP 4: FRONT=FRONT-1
STEP 5: END

ALGORITHM FOR POP FROM FRONT IN DEQUEUE:

STEP 1: START
STEP 2: IF FRONT=REAR then QUEUE UNDERFLOW FROM FRONT,
exit
Else
STEP 3: FRONT=FRONT+1
STEP 4: VALUE = QUEUE[FRONT]
STEP 5: END

ALGORITHM FOR PUSH FROM REAR IN DEQUEUE:

STEP 1: START
STEP 2: IF REAR = CAPACITY then QUEUE OVERFLOW FROM
REAR, exit
Else
STEP 3: REAR = REAR +1
STEP 4: QUEUE[REAR] = VALUE
STEP 5: END

ALGORITHM FOR POP FROM REAR IN DEQUEUE:

STEP 1: START
STEP 2: IF FRONT=REAR then QUEUE UNDERFLOW FROM REAR,
exit
Else
STEP 3: VALUE = QUEUE[FRONT]
STEP 4: REAR=REAR-1
STEP 4: END
class Dequeue
{ int q[], cap, front,rear;
Dequeue(int nn)
{ cap=nn;
front=rear=0;
q = new int[cap];
}
void pushfront(int v)
{ if(front !=0)
q[front--]=v;
else
System.out.println("QUEUE FULL FROM FRONT");
}
void pushrear(int v)
{ if( rear<cap-1)
q[++rear]=v;
else
System.out.println("QUEUE FULL FROM REAR");
}
int popfront()
{ if(front !=rear)
return(q[++front]);
else
return -9999;
}
int poprear()
{ if(front !=rear)
return(q[rear--]);
else
return -9999;
}
void display()
{ for(int i=front+1;i<=rear;i++)
System.out.println(q[i]);
}
}
ALGORITHM FOR PUSH IN CIRCULAR QUEUE:

STEP 1: START
STEP 2: IF FRONT = (REAR+1) MOD CAPACITY then QUEUE OVERFLOW, exit
Else
STEP 3: REAR = (REAR +1) MOD CAPACITY
STEP 4: QUEUE[REAR] = VALUE
STEP 5: END

ALGORITHM FOR POP IN CIRCULAR QUEUE:

STEP 1: START
STEP 2: IF FRONT=REAR then QUEUE UNDERFLOW, exit
Else
STEP 3: FRONT=(FRONT + 1) MOD CAPACITY
STEP 4: VALUE = QUEUE[FRONT]
STEP 4: END

class Cirqueue
{ int q[], cap, front,rear;
Cirqueue(int nn)
{ cap=nn;
front=rear=0;
q = new int[cap];
}
void push(int v)
{ if( (rear+1)%cap !=front)
{ rear = (rear+1)%cap;
q[rear]=v;
}
else
System.out.println("QUEUE FULL");
}
int pop()
{ if(front !=rear)
{ front=(front+1)%cap;
return(q[front]);
}
else
return -9999;
}
void display()
{ int i=front;
while(i != rear)
{ i=(i+1)%cap;
System.out.println(q[i]);
}
}
}

You might also like