Sunday, August 24, 2014

Que - Data Structure C++

// Que.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <conio.h>

using namespace std;

class Node{
public:
int data;
Node *next;

Node(){
data=0;
next=NULL;
}
};

class Que{
public:
Node *first;
Node *last;
Node *current;

Que(){
first = NULL;
last=NULL;
current=NULL;
}

void enque(int n){
if(last==NULL){
Node *temp=new Node;
temp->data=n;
temp->next=NULL;
first=temp;
last=temp;
delete(temp);
}
else{
Node *temp= new Node;;
temp->data=n;
temp->next=NULL;
last->next=temp;
last=temp;
delete(temp);
}
}
void deque(){
if(last==NULL){
cout<<"\nThe Que is empty\n ";
}
else{
current=first;
first=first->next;
cout<<"Data dequeued is : "<<current->data<<endl;
delete(current);
}
}
void display(){
current=first;
while(current!=last){
cout<<current->data;
current=current->next;
}
cout<<current->data;
}
};

int _tmain(int argc, _TCHAR* argv[])
{
Que q;
int option,n;

cout<<"Please select your que operation:\n1: Enque()\n2:Deque\n3: Display\n Enter 'e' to exit\n";
cin>>option;
while(option!='e'){
switch(option){
case 1:
cout<<"\nEnter the data you want to enque: ";
cin>>n;
q.enque(n);
break;
case 2:
q.deque();
break;
case 3:
q.display();
break;
}
cout<<"\nPlease select your que operation:\n1: Enque()\n2:Deque\n3: Display\n Enter 'e' to exit\n";
cin>>option;
}
return 0;
}

No comments:

Post a Comment