Pages

Custom Search

Friday, November 4, 2011

Program to implement dynamically allocated Queue using linked list.



 #include <stdio.h>  
 #include <stdlib.h>  
 struct tag  
 {  
     int data;  
     struct tag *link;  
 };  
 typedef struct tag * NODE;  
 void ins_rnode();  
 void del_fnode();  
 void display();  
 int main()  
 {  
     int ch, element, dnum;  
     NODE front = NULL;  
     NODE rear = NULL;  
     do  
     {  
         printf("\nOPERATIONS ON QUEUE: ");  
         printf("\n1.) INSERT");  
         printf("\n2.) DELETE");  
         printf("\n3.) DISPLAY");  
         printf("\n4.) EXIT");  
         printf("\nEnter your choice: ");  
         scanf("%d", &ch);  
         switch(ch)  
         {  
             case 1: printf("\nEnter element to insert into the queue: ");  
                   scanf("%d", &element);  
                   ins_rnode(&front, &rear, element);  
                   break;  
             case 2: del_fnode(&front);  
                   break;  
             case 3: display(&front);  
                   break;  
             case 4: break;  
             default: printf("\nWrong choice!! Enter choice again.");  
         }  
     }while(ch!=4);  
 }  
 void ins_rnode(NODE *front, NODE *rear, int ele)  
 {  
     NODE newnode, temp;  
     temp = *front;  
     newnode = (NODE)malloc(sizeof(struct tag));  
     if(newnode == NULL)  
     {  
         printf("\nInsufficient memory.");  
         return;  
     }   
     newnode->data = ele;  
     newnode->link = NULL;  
     if(*front == NULL && *rear == NULL)  
     {  
         *front = newnode;  
         *rear = newnode;  
     }  
     else  
     {  
         while(temp->link != NULL)  
         {  
             temp = temp->link;  
         }  
         temp->link = newnode;  
         *rear = newnode;  
     }  
 }  
 void del_fnode(NODE *front)  
 {  
     NODE c;  
     if(*front == NULL)  
     {  
         printf("\nQueue is empty");  
         return;  
     }  
     c = *front;  
     printf("\nDeleted node: %d\n", c->data);  
     *front = c->link;  
     free(c);  
 }  
 void display(NODE *front)  
 {  
     NODE temp;  
     temp = *front;  
     if(*front == NULL)  
     {  
         printf("\nQueue is empty");  
         return;  
     }  
     printf("\nQueue is: ");  
     while(temp != NULL)  
     {  
      printf("%d ", temp->data);  
         temp = temp->link;  
     }  
 }  

No comments:

Custom Search