Last active
November 12, 2016 15:00
-
-
Save mostafa6765/5f3b8e41f4598749ab8c1f2760621bf0 to your computer and use it in GitHub Desktop.
Enqueue ,Dequeue ,Display with linked list
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
using namespace std; | |
struct node | |
{ | |
int data; | |
node *next; | |
}*front = NULL,*rear = NULL,*p = NULL,*np = NULL; | |
void enqueue( int value) | |
{ | |
np=new node; | |
np->data=value; | |
np->next=NULL; | |
if(front==NULL) | |
{ | |
front = rear = np; | |
rear->next = NULL; | |
} | |
else | |
{ | |
rear->next = np; | |
rear = np; | |
rear->next = NULL; | |
} | |
} | |
int dequeue(int value) | |
{ | |
if(front==NULL) | |
{ | |
cout<<"empty queue"; | |
} | |
else | |
{ | |
p = front; | |
value = p->data; | |
front = front->next; | |
delete(p); | |
return(value); | |
} | |
} | |
void display(){ | |
if(front == NULL){ | |
cout<<"\nNothing to Display\n"; | |
}else{ | |
np=front; | |
while(np!=NULL){ | |
cout<<endl<<np->data; | |
np = np->next; | |
} | |
} | |
} | |
int main() | |
{ | |
int value, choice; | |
do{ | |
cout<<"\n 1.enqueue \n 2.dequeue \n 3.display \n 4.exit\n \n input choice: "; | |
cin>>choice; | |
if(choice==1) | |
{ | |
cout<<"\n enter a value"; | |
cin>> value; | |
enqueue(value); | |
} | |
if(choice==2) | |
{ | |
dequeue(value); | |
} | |
if(choice==3) | |
{ | |
display(); | |
} | |
} | |
while(choice!=4); | |
cout<<"\n\n\n Existing......\n"; | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment