Sunday, 3 April 2016

STL:Stack and queue C/C++ brush up

STL:Standard Temparary Library

In RAM and we have a stack memory !

Now in Programming we store our data in stack or queue . and we implements these
using data structure
1)Array
or
2)Link List
or
3)Trees.

all these data structures are interconnected .you can create array to queue ,queue to array

Stack

Operations on stack


  1. Push
  2. Pop
  3. Is Full
  4. Is Empty

--Exbhibit LIFO (Last in first out)
--Here in stack we have have one pointer only

  1. Top 

Push

S-our stack array
N-size of our stack array
Top-stack pointer
x-element to be pushed


  1. push(S,N,Top,x)
  2. {
  3.    if(Top+1==N)
  4.       {
  5.      printf("Stack Overflow");
  6.      exit;
  7.       }
  8. else
  9.     {
  10.       Top++;
  11.        S[Top]=x;
  12. }

Pop
--here we are only delecting the top most element of stack array S. as here we what to return the elemet which would be de;leted so for that we will use int return type of following function
  1. int pop(S,N,Top)
  2. {
  3.       int y;
  4.       if (Top==-1)
  5.         {
  6.        printf("Stack Underflow");
  7.        exit;
  8.         }
  9. else
  10.         {
  11.        y=S[Top];
  12.        Top--;
  13.        return y;
  14. }


Queue

Operations on queue


  1. Enqueue-Data Insert  <--->Push in stack
  2. Dequeue- Data Delete
  3. Is Full
  4. Is Empty


--Here in an array of queue we have two pointer

  1. front--Tells deletion
  2.  rear--Tells insertion
Enqueue
in below code
Q-Our Array
N-Size of our array
F-Front pointer of our queue
R-Rear pointer of our queue
x-element we want to put
  1. void enqueue(Q,N,F,R,x)
  2. {
  3.      if(R+1==N)  //means if R+1 ==size of an array
  4.        {
  5.         printf("Queue is overflow");
  6.         exit;
  7.         }
  8.    else if
  9.        {
  10.        if(F==R==-1)
  11.          {
  12.            F=R=0;
  13.           }
  14.         else
  15.            {
  16.             R=R+1;
  17.            }
  18. Q[R]=x;
  19. }

Dequeue

Here no x .because our aim is to just delete one element which is always from front and we also want what is the value of deleted array that's why we are returning from our function.


------------------------------------
^                             ^
Front                    Rear(insertion)
(Deletion)


  1. int dequeue(Q,N,F,R)
  2. {
  3.    int y;
  4.    if(F==R==-1)
  5.       {
  6.      prinf("underflow");
  7.      exit;
  8.      }
  9.    else
  10.      {
  11.        y=Q[F]; //delete
  12.         if (F==R)  //means in case if all element is before F is already deleted i.e null and we 
  13.                          //know after R there is nothing So , Both R & F vacant in makes no sense so
  14.                          //to get rid of that Awkward situation we are placing both in front.And
  15.                        
  16.            {
  17.               F=R=-1;
  18.             }
  19.         else
  20.           F=F+1;
  21.        return y;
  22.         }
  23. }

No comments:

Post a Comment