C Program to implement QUEUE operations using Linked Lists
Source: Dr. G T Raju, Professor & Head, Dept. of CSE, RNSIT
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | #include <stdlib.h> typedef struct node { int data; struct node *link; }NODE; void Insert(int); int Delete(); void Display(); NODE *front,*rear; /* Global Declarations */ main() { /* Main Program */ int opn,elem; front=rear=NULL; do { clrscr(); printf("\n ### Linked List Implementation of QUEUE Operations ### \n\n"); printf("\n Press 1-Insert, 2-Delete, 3-Display,4-Exit\n"); printf("\n Your option ? "); scanf("%d",&opn); switch(opn) { case 1: printf("\n\nRead the Element to be Inserted ?"); scanf("%d",&elem); Insert(elem); break; case 2: elem=Delete(); if(elem != -1) printf(" Deleted Node(From Front)with the Data: %d\n",elem); break; case 3: printf("Linked List Implementation of Queue: Status:\n"); Display(); break; case 4: printf("\n\n Terminating \n\n"); break; default: printf("\n\nInvalid Option !!! Try Again !! \n\n"); break; } printf("\n\n\n\n Press a Key to Continue . . . "); getch(); }while(opn != 4); } void Insert(int info) { NODE *temp; temp=(NODE *)malloc(sizeof(NODE)); if( temp == NULL) printf(" Out of Memory !! Overflow !!!"); else { temp->data=info; temp->link=NULL; if(front == NULL) { front = rear = temp; } /* First Node? */ else { rear->link=temp; rear = temp; } /* Insert End */ printf(" Node has been inserted at End Successfully !!"); } } int Delete() { int info; NODE *t; if( front == NULL) { printf(" Underflow!!!"); return -1; } else { t=front; info=front->data; if(front == rear) rear=NULL; front=front->link; t->link=NULL; free(t); return(info); } } void Display() { NODE *t; if( front == NULL) printf("Empty Queue\n"); else { t=front; printf("Front->"); while(t) { printf("[%d]->",t->data); t=t->link; } printf("Rear\n"); } } |
Description :
This is the one stop educational site for all Electronic and Computer students. If you want to learn something new then we are here to help. We work on Microcontroller projects, Basic Electronics, Digital electronics, Computer projects and also in basic c/c++ programs.
#Home #Sitemap #Resources #Terms of Use
Copyright©2012 electrofriends.com All Rights Reserved
Contact:[email protected]
Thanks!! Very useful! but there should be return 0; after the }while(opn != 4);