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

Algorithm and Programs in Stack and Queues

The document outlines algorithms for stack, linear queue, dequeue, and circular queue operations, including push and pop methods. It provides step-by-step procedures for handling overflow and underflow conditions for each data structure. Additionally, it includes class implementations in Java for each data structure with methods for pushing, popping, and displaying elements.

Uploaded by

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

Algorithm and Programs in Stack and Queues

The document outlines algorithms for stack, linear queue, dequeue, and circular queue operations, including push and pop methods. It provides step-by-step procedures for handling overflow and underflow conditions for each data structure. Additionally, it includes class implementations in Java for each data structure with methods for pushing, popping, and displaying elements.

Uploaded by

madnani.khushi46
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, 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(intnn)
{ 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(inti=top;i>=0;i--)
System.out.println(s[i]);
}
}
ALGORITHM FOR PUSH IN LINEAR QUEUE:

STEP 1: START
STEP 2: IF REAR = CAPACITY thenQUEUE 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 thenQUEUE UNDERFLOW, exit
Else
STEP 3: FRONT=FRONT + 1
STEP 4: VALUE = QUEUE[FRONT]
STEP 4: END

classLqueue
{ int q[], cap, front,rear;
Lqueue(intnn)
{ 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(inti=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[REAR]
STEP 4: REAR=REAR-1
STEP 4: END
Class Dequeue
{ int q[], cap, front,rear;
Dequeue(intnn)
{ 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(inti=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(intnn)
{ 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()
{ inti=front;
while(i != rear)
{ i=(i+1)%cap;
System.out.println(q[i]);
}
}
}

You might also like