Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic queue #23

Merged
merged 7 commits into from
Jun 29, 2016
Merged
109 changes: 109 additions & 0 deletions User_Friendly_Dynamic_Queue/User_Friendly_Dynamic_Queue.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// Dynamic Queue
#include <iostream>

using namespace std;

struct Node{
int info;
Node *link;
};

void Enqueue(int data, Node**, Node**);
void Dequeue(Node**, Node**);
void Display(Node*, Node*);

int main()
{
Node *First, *Last;
First = NULL;
Last = NULL;
int data;
int ch;

do
{
cout << "\n\tMENU";
cout << "\n1.Enqueue";
cout << "\n2.Dequeue";
cout << "\n3.Display";
cout << "\n4.Exit";
cout << "\nEnter choice...";
cin >> ch;
switch(ch)
{
case 1: cout << "\nEnter new data:";
cin >> data;
Enqueue(data, &First, &Last);
break;

case 2: Dequeue(&First, &Last);
break;

case 3:Display(First, Last);
break;
}
} while(ch >= 1 && ch <= 3);

return 0;
}

void Enqueue(int data, Node **F, Node **L)
{
Node *temp = new Node;
temp -> info = data;

if(*L == NULL)
{
temp -> link = NULL;
*F = temp;
}
else
temp -> link = *L;

(*L) = temp;
}

void Dequeue(Node **F, Node **L)
{
if(*F == NULL)
{
cout << "\nUnderflow...";
return;
}
else if(*F == *L)
{
*F = NULL;
*L = NULL;
return;
}

Node *temp = NULL;
temp = *L;

while(temp -> link != (*F))
temp = temp -> link;

temp -> link = NULL;
*F = temp;
}

void Display(Node *F, Node *L)
{
Node *temp = NULL;

if(L == NULL)
{
cout << "\nUnderflow";
return ;
}

temp = L;

while(temp -> link != NULL)
{
cout << temp -> info << "->";
temp = temp -> link;
}

cout << temp -> info << "->!!!";
}