• No results found

PRIORITY QUEUES

In document algorithms made easy (Page 161-168)

The Queues A queue is logically a first in first out (FIFO or first come first serve) linear data struc-

DELETE AN ELEMENT FROM THE LEFT SIDE OF THE DE-QUEUE 1 If (left == – 1)

10 NULL TOP

5.13. PRIORITY QUEUES

Priority Queue is a queue where each element is assigned a priority. In priority queue, the elements are deleted and processed by following rules.

1. An element of higher priority is processed before any element of lower priority. 2. Two elements with the same priority are processed according to the order in

which they were inserted to the queue.

For example, Consider a manager who is in a process of checking and approving files in a first come first serve basis. In between, if any urgent file (with a high priority) comes, he will process the urgent file next and continue with the other low urgent files.

Fig. 5.35. Priority queue representation using arrays

Above Fig. 5.35 gives a pictorial representation of priority queue using arrays after adding 5 elements (10,14,12,60,13) with its corresponding priorities (9,10,17,30,46). Here the priorities of the data(s) are in ascending order. Always we may not be pushing the data in an ascending order. From the mixed priority list it is difficult to find the highest priority element if the priority queue is implemented using arrays. Moreover, the implementation of priority queue using array will yield n comparisons (in liner search), so the time com- plexity is O(n), which is much higher than the other queue (ie; other queues takes only O(1) ) for inserting an element. So it is always better to implement the priority queue using linked list - where a node can be inserted at anywhere in the list - which is discussed in this section.

A node in the priority queue will contain DATA, PRIORITY and NEXT field. DATA field will store the actual information; PRIORITY field will store its corresponding priority of the DATA and NEXT will store the address of the next node. Fig. 5.36 shows the linked list representation of the node when a DATA (i.e., 12) and PRIORITY (i.e., 17) is inserted in a priority queue.

Fig. 5.36. Linked list representation of priority queue

When an element is inserted into the priority queue, it will check the priority of the element with the element(s) present in the linked list to find the suitable position to insert. The node will be inserted in such a way that the data in the priority field(s) is in ascending order. We do not use rear pointer when it is implemented using linked list, because the new nodes are not always inserted at the rear end. Following figures will illustrate the push and pop operation of priority queue using linked list.

Front

10 9 12 17

Fig. 5.37. push(DATA =10, PRIORITY = 9)

Fig. 5.38. push(DATA = 60, PRIORITY = 30)

Fig. 5.39. push(DATA = 13, PRIORITY = 46)

Front

12 17 60 30 13 46 14 10

Fig. 5.41. x = pop() (i.e., 10)

Front

12 17 60 30 13 46

Fig. 5.42. x = pop() (i.e., 14)

PROGRAM 5.11

//PROGRAM TO IMPLEMENT PRIORITY QUEUE USING LINKED LIST //CODED AND COMPILED USING TURBO C

#include<conio.h> #include<stdio.h> #include<malloc.h> #include<process.h>

//A structure is created for a node struct node

{

int priority; int info;

struct node *link; };

typedef struct node *NODE;

//This function will insert a data and its priority NODE insert(NODE front)

{

NODE tmp,q;

int added_item,item_priority; //New node is created

tmp = (NODE)malloc(sizeof(struct node));

printf("\nInput the item value to be added in the queue:"); scanf("%d",&added_item);

printf("\nEnter its priority:"); scanf("%d",&item_priority); tmp->info = added_item; tmp->priority = item_priority;

/*Queue is empty or item to be added has priority more than first item*/ if(front == NULL || item_priority < front->priority)

{ tmp->link = front; front = tmp; } else { q = front;

while(q->link != NULL && q->link->priority <= item_priority) q=q->link; tmp->link = q->link; q->link = tmp; }/*End of else*/ return(front); }/*End of insert()*/

//Following function is to delete a node from the priority queue NODE del(NODE front)

{ NODE tmp; if(front == NULL) printf("\nQueue Underflow\n"); else { tmp = front;

printf("\nDeleted item is %d\n",tmp->info); front = front->link;

free(tmp); }

return(front); }/*End of del()*/

void display(NODE front) {

NODE ptr; ptr = front; if(front == NULL) printf("\nQueue is empty\n"); else { printf("\nQueue is:\n"); printf("\nPriority Item\n"); while(ptr != NULL) { printf("%5d %5d\n",ptr->priority,ptr->info); ptr = ptr->link; } }/*End of else */ }/*End of display() */ void main() { int choice; NODE front=NULL; while(1) { clrscr(); //Menu options printf("\n1.Insert\n"); printf("2.Delete\n"); printf("3.Display\n"); printf("4.Quit\n");

printf("\nEnter your choic"); scanf("%d", &choice); switch(choice) { case 1: front=insert(front); break; case 2: front=del(front); getch(); break; case 3: display(front);

getch(); break; case 4: exit(1); default : printf("\nWrong choice\n"); }/*End of switch*/ }/*End of while*/ }/*End of main()*/

SELF REVIEW QUESTIONS

1. Write an algorithm to count the number of nodes in a singly linked list?

[Calicut - APR 1997 (BTech), MG - MAY 2000 (BTech)] 2. Write an algorithm to insert a node in a linked list? [MG - MAY 2004 (BTech)] 3. Describe how a polynomial is represented using singly linked lists. Write an algorithm to

add two polynomials represented using linked list?

[MG - MAY 2004 (BTech), MG - MAY 2003 (BTech),

MG - NOV 2003 (BTech), MG - NOV 2002 (BTech), MG - MAY 2002 (BTech), MG - MAY 2000 (BTech) CUSAT - DEC 2003 (MCA)]

4. What is doubly linked list? Write an algorithm to add and delete a node from it? [MG - NOV 2004 (BTech), MG - MAY 2000 (BTech)

ANNA - DEC 2003 (BE), CUSAT - DEC 2003 (MCA) ANNA - MAY 2004 (MCA), ANNA - DEC 2004 (BE) KERALA - MAY 2003 (BTech)]

5. Explain the array representation of a priority queue? Write an algorithm for deleting and inserting in a priority queue? Mention its Applications?

[MG - NOV 2004 (BTech), MG - MAY 2003 (BTech),

MG - NOV 2003 (BTech), CUSAT - NOV 2002 (BTech), MG - NOV 2002 (BTech)]

6. Discuss the advantages and disadvantages of singly linked list?

[MG - MAY 2003 (BTech)] 7. Explain the structure of a doubly linked list. Write a general algorithm for inserting and deleting nodes in the middle? [MG - MAY 2003 (BTech), MG - NOV 2002 (BTech)] 8. Compare and distinguish between singly linked lists and doubly linked lists?

[CUSAT - APR 1998 (BTech), MG - NOV 2003 (BTech)

KERALA - MAY 2001 (BTech), CUSAT - JUL 2002 (MCA)]

9. Discuss the advantages and disadvantages of linked list over arrays?

10. Write down the steps to invert a singly-linked circular linked list?

[CUSAT - MAY 2000 (BTech), MG - MAY 2002 (BTech) 11. How do you use circular queue in programming? [MG - MAY 2000 (BTech)] 12. How doubly linked list can be used for dynamic storage? [MG - MAY 2000 (BTech)] 13. What is a Priority queue? How is it represented in the memory?

[ANNA - DEC 2004 (BE), CUSAT - APR 1998 (BTech)] 14. What is a linked list? What are its advantages over array? How is linked list implemented

in the memory? [CUSAT - APR 1998 (BTech)]

15. Develop an algorithm to insert a node to the right of kth node of a singly linked linear list. [CUSAT - APR 1998 (BTech)] 16. Evaluate the complexity of the algorithm to add two polynomials in the form of linked

lists [CUSAT - MAY 2000 (BTech)]

17. Compare linear data structures with linked storage data types.

[CUSAT - MAY 2000 (BTech)] 18. Explain the representation of polynomials using linked lists.

[CUSAT - NOV 2002 (BTech)] 19. Write algorithms for adding and deleting elements from a circular queue implemented as

linked list. [CUSAT - DEC 2003 (MCA)]

20. Explain any three operations on a linked list. Write algorithms for these operations. [ANNA - DEC 2003 (BE)] 21. Discuss the advantage of circular queue with examples. [ANNA - MAY 2004 (MCA)] 22. Explain how pointers are used to implement linked list structures.

[ANNA - MAY 2004 (BE)] 23. Differentiate singly linked list and circularly linked list.

[KERALA - DEC 2003 (BTech), ANNA - MAY 2003 (BE)] 24. Write an algorithm to traverse elements in a singly linked list

[KERALA - DEC 2004 (BTech)] 25. Explain about frequency count and give an example. Write procedures for circular queue

operations. [KERALA - JUN 2004 (BTech)]

26. Write procedure for searching an element in a singly linked list.

[KERALA - JUN 2004 (BTech)] 27. What is the advantage of a doubly linked list compared to a singly linked list?

Sorting Techniques

In document algorithms made easy (Page 161-168)