Linear queue



There might be some problem of implementing linear queue though it is very simple. Here is your solution, you can just read the article and you will get the answer for your questions. Here every single step is explained which is done in program coding with snap shots of the program itself.


“Linear” means in sequence or to follow one definite path, so linear queue follows this criteria and does not allow backtracking, i.e. once you reach second address you can not come back or insert to previous address even if it is empty.
There are two operation of linear queue that is described in the program is PUSH and POP.
PUSH :-  This operation is use to insert elements to the queue.
POP :-  This operation is use to popping(retrieve) element from the queue.
These operations are explained by taking reference of the program.
  •  Q is an array.
  •  F is a pointer which is use for popping element.
  •  R is a pointer which is use for inserting element.






Here we have made a function name PUSH.

first step is to check whether the queue is overflow or not? , so condition is  if  F [*f==0] point to first element and R [*r=n-1] points to last element OR F points to element after the R pointer pointing [*f=*r+1]. If this condition satisfies the queue will be overflow.
Now if queue is not overflow then we can insert elements = Q[*r], this step will insert element to queue, but before inserting we have to reset pointers.
Initially F and R pointer will be set to -1. So we have to reset F pointer to 0 that is first address of an array where we can insert element. By incrementing the R pointer [*r=*r+1] and then insert the element.
Similarly, here is a function of POP.









       Here we check the condition for underflow it means no elements are there in the queue. 
       If F pointer is set next to the R pointer.
       Now to delete or pop the element just increment the pointer F [*f=*f+1].  

       To observe the output after performing different operations on queue we created display function.






       Now the MAIN function in which PUSH and POP functions are called as per you select the option.












        Here, we have array of size Q[5], and to choose different operations we have use SWITCH case.
1      is for PUSH operation in which we have passed array and F & R pointer as parameters.
2      is for POP operation in which same parameters are passed.
3      is display.
        This switch case will repeat upto we enter last option that is for exit. for looping we have use while loop.

         NOTE:  The program attached to this link is done with the help of gcc compiler, so you need to change like getch(), clrscr(), etc if you are using another compiler.
   
         LINK (Download from here)




{ 0 comments ... read them below or add one }

Post a Comment