Skip to content

Commit

Permalink
Dynamic queue (#23)
Browse files Browse the repository at this point in the history
* Rename Dynamic_Queue.cpp to Dynamic_Queue/Dynamic_Queue.cpp

* Update dq.cpp

* Added dynamic queue

* Update and rename Dynamic_Queue.cpp to User_Friendly_Dynamic_Queue.cpp

* Update and rename Dynamic_Queue/User_Friendly_Dynamic_Queue.cpp to User_Friendly_Dynamic_Queue/User_Friendly_Dynamic_Queue.cpp
  • Loading branch information
sidgorey authored and jainaman224 committed Jun 29, 2016
1 parent 8dfbf33 commit 22915ab
Showing 1 changed file with 109 additions and 0 deletions.
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 << "->!!!";
}

0 comments on commit 22915ab

Please sign in to comment.