#include<stdio.h> #include<stdlib.h> #define MAX 5 void enqueue(); void dequeue(); void display(); struct node { int data; struct node * next; }; struct node *head =NULL; int count = 0,xcount = 1; main() { int choice; do { printf("\n----MENU----\n"); printf("1.enqueue\n2.dequeue\n3.display\n4.EXIT\n"); printf("\nEnter your choice\n"); scanf("%d",&choice); switch(choice) { case 1:enqueue();break; case 2:dequeue();break; case 3:display();break; case 4:printf("\n~~~~Thank You~~~~\n");break; default:printf("\nINVALID INPUT !!!!\n"); } }while(choice!=4); } void enqueue() { int value; struct node *newnode; if(count>=MAX) printf("\nQueue overflow\n"); else { struct node* temp; temp=head; newnode=(struct node* )malloc(sizeof(struct node)); printf("\nEnter element to be inserted\n"); scanf("%d",&value); newnode->data=value; if(head==NULL) { newnode->next=NULL; head=newnode; count++; } else { while(temp->next!=NULL) temp=temp->next; temp->next=newnode; newnode->next=NULL; count++; } display(); } } void dequeue() { if(count==0) printf("\nQueue is empty !!!!"); else { struct node* temp; temp=head; if(count==1||xcount==count) { head=NULL; free(temp); count=0; xcount=0; } else { head=head->next; free(temp); xcount++; } display(); } } void display() { struct node *temp; temp=head; if(head==NULL) printf("\nQueue is empty !!!!"); else { printf("\nElements are :\t"); while(temp!=NULL) { printf("%d\t",temp->data); temp=temp->next; } } }
|
|
|
|
Labels: Data Structure in C