Priority queue implementation

1:  #include <stdio.h>  
2:  #include <stdlib.h>  
3:  #include <dos.h>  
4:  #include <malloc.h>  
5:  struct pqueue  
6:  {  
7:       int key;  
8:       int data;  
9:       struct pqueue*next;  
10:  };  
11:  typedef struct pqueue QUEUE;  
12:  QUEUE*front=NULL;  
13:  void qinsert()  
14:  {  
15:    QUEUE*temp,*flag;  
16:    temp=(QUEUE*)malloc(sizeof(QUEUE));  
17:    printf("Enter Key value: ");  
18:    scanf("%d",&temp->key);  
19:    printf("Enter Data: ");  
20:    scanf("%d",&temp->data);  
21:    if(front==NULL||temp->key<front->key)  
22:    {  
23:          temp->next=front;  
24:          front=temp;  
25:          return;          
26:    }  
27:    flag=front;  
28:    while(flag->next!=NULL&&flag->next->key<temp->key)  
29:    flag=flag->next;  
30:    temp->next=flag->next;  
31:    flag->next=temp;  
32:    return;    
33:  }  
34:  void qdelete()  
35:  {  
36:       QUEUE*temp;  
37:       if(front==NULL)  
38:       {  
39:            printf("QUEUE IS UNDERFLOW\n");  
40:            system("PAUSE");  
41:            return;  
42:       }  
43:       temp=front;  
44:       front=front->next;  
45:       temp->next=NULL;  
46:       printf("Deleted data:%d",temp->data);  
47:       free(temp);  
48:       system("PAUSE");  
49:       return;   
50:  }  
51:  void qdisplay()  
52:  {  
53:    QUEUE*temp;  
54:    if(front==NULL)  
55:    {  
56:          printf("QUEUE IS EMPTY\n");  
57:          system("PAUSE");  
58:          return;  
59:    }  
60:    temp=front;  
61:    printf("QUEUE ELEMENTS ARE: ");  
62:    do  
63:    {  
64:          printf("\nKey:%d data:%d",temp->key,temp->data);  
65:          temp=temp->next;  
66:    }while(temp!=NULL);  
67:    printf("\n");  
68:    system("PAUSE");  
69:        return;  
70:  }  
71:  int main(void)  
72:  {  
73:       int option;  
74:         while(1)  
75:       {  
76:            system("CLS");  
77:            printf("\nQInsert PRESS 1: ");  
78:            printf("\nQDelete PRESS 2: ");  
79:            printf("\nQDisplay PRESS 3: ");  
80:            printf("\nEXIT PRESS   4: ");  
81:            scanf("%d",&option);  
82:            switch(option)  
83:            {  
84:                 case 1: qinsert();  
85:                           break;  
86:                 case 2: qdelete();  
87:                           break;  
88:                 case 3: qdisplay();  
89:                           break;                 
90:                 case 4: free(front);  
91:                     return EXIT_SUCCESS;  
92:                 default: printf("INVALID OPTION\n");  
93:                            system("PAUSE");  
94:            }            
95:       }  
96:  }  

No comments:

Post a Comment