The need:
This program shows how one can implement concept of queue in linked lists. This is extension of previous program. This program is analogous to program75 of this blog.
The code:
------------------------------------------------------
This program shows how one can implement concept of queue in linked lists. This is extension of previous program. This program is analogous to program75 of this blog.
The code:
------------------------------------------------------
/* This is a program showing the queue implimentation on linked list program writtem by RP Singh compiled and tested on C-free4.0 standard */ #include<stdio.h> #include<stdlib.h> #include<string.h> struct node1 { int item; struct node1 *next; }; typedef struct node1 node; //defining datatype node struct q1 { node *front; node *rear; int length; }; typedef struct q1 queue; //defining queue datatype void enqueue(node *, queue *); node *dequeue(queue *); int queue_length(queue *); void view_queue(queue *); node *create_node(void); void fill_node(node *); int main() { int i,j; node *current_node; queue que; //local queue que.front=NULL; que.rear=NULL; que.length=0; printf("This is a demo program to show working of queues"); anchor: //anchor is a label printf("\n\nyou have following options\n"); printf("1. enqueue an item\n2. dequeue an item\n3. view queue\n"); printf("4. count items in queue\n5. exit program\n\n"); scanf("%d",&j); switch(j) { case 1: printf("\nEnter a number to be enqueued =>\t"); current_node=create_node(); fill_node(current_node); enqueue(current_node, &que); goto anchor; case 2: current_node = dequeue(&que); if(current_node) printf("The item %d dequeued successfully\n",current_node->item); goto anchor; case 3: view_queue(&que); goto anchor; case 4: printf("total items in the queue are %d\n",que.length); goto anchor; case 5: printf("Thank you\n"); exit(0); goto anchor; default: printf("Invalid choice...!!!\n try choosing again\n\n"); goto anchor; } return 0; } void enqueue(node *p,queue *q) //definition of enqueue function { if(q->rear==NULL&&q->front==NULL) { q->rear=p; q->front=p; } else { q->rear->next=p; q->rear=p; } q->length++; printf("item %d enqueued successfully.\n",p->item); } node *dequeue(queue *q) //definition of dequeue function { node *temp=NULL; if(q->rear==NULL&&q->front==NULL) //this is the case when queue is empty { printf("queue is empty hence can not be dequeued\n"); return temp; } else if(q->rear==q->front&&q->front!=NULL) //this is the case when queue has only one node { temp=q->front; q->front=NULL; q->rear=NULL; } else { temp=q->front; q->front=q->front->next; } q->length--; return temp; } void view_queue(queue *q) { node *temp_front; temp_front=q->front; if(q->length==0) { printf("\nThe queue is empty...!!!\n"); return; } printf("The queue is\n"); while(temp_front!=NULL) { printf("%d -> ",temp_front->item); temp_front=temp_front->next; } printf("\n"); return; } node *create_node() //function to create a blank node { node *temp; temp=(node*)malloc(sizeof(node)); temp->next=NULL; return temp; } void fill_node(node *p) //function to fill a blank node with values taken from user { int i; scanf("%d",&i); //this is the value taken from user p->item=i; }
------------------------------------------------------
No comments:
Post a Comment
feel free to ask your doubts... if any
you can post your doubts on
www.facebook.com/programsimply