0% found this document useful (0 votes)
27 views

Queue Via Link List: #Include #Include #Include Using Namespace Class Int Float

The document discusses different implementations of a queue data structure using C++. It first shows a queue implemented using a linked list where each node stores a student object. It includes functions to enqueue, dequeue and display students. It then shows two implementations of a queue using an array - a static array where the size is fixed, and a dynamic array where the size is determined at runtime. Both array implementations include enqueue, dequeue and display functions. The final section begins to discuss an input restricted queue using a dynamic array but is cut off.

Uploaded by

Atif Saeed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Queue Via Link List: #Include #Include #Include Using Namespace Class Int Float

The document discusses different implementations of a queue data structure using C++. It first shows a queue implemented using a linked list where each node stores a student object. It includes functions to enqueue, dequeue and display students. It then shows two implementations of a queue using an array - a static array where the size is fixed, and a dynamic array where the size is determined at runtime. Both array implementations include enqueue, dequeue and display functions. The final section begins to discuss an input restricted queue using a dynamic array but is cut off.

Uploaded by

Atif Saeed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Queue Via Link List

#include "stdafx.h"
#include<iostream>
#include<string>
using namespace std;
class student
{
int id, age;
float cgpa;
string name;
student *n, *s;
public:
student()
{
s=NULL;
}
void Enqueue()
{
student* q=new student;
cout<<"Please enter id of the student\t";
cin>>q->id;
cout<<"Please enter age of the student\t";
cin>>q->age;
cout<<"Please enter cgpa of the student\t";
cin>>q->cgpa;
cout<<"Please enter name of the student\t";
flushall();
getline(cin,q->name);
q->n=NULL;
if(s==NULL)
{
s=q;
}
else
{
student* t=s;
while(t->n!=NULL)
t=t->n;
t->n=q;
}
}
void Dequeue()
{
if(s==NULL)
cout<<"Queue is empty"<<endl;
else
{
cout<<"Student having"<<endl;
cout<<"\t\t"<<s->id<<"\tas id"<<endl;
cout<<"\t\t"<<s->age<<"\tas age"<<endl;
cout<<"\t\t"<<s->name<<"\tas name"<<endl;
cout<<"\t\t"<<s->cgpa<<"\tas cgpa"<<endl;
cout<<"is going to Dequeue from Queue"<<endl;
s=s->n;
}
}
void Display()
{
if(s==NULL)
{
cout<<"Queue is empty"<<endl;
}
else
{
student *t=s;
while(t!=NULL)
{
cout<<"Id\t\t"<<t->id<<endl;
cout<<"Name\t\t"<<t->name<<endl;
cout<<"age\t\t"<<t->age<<endl;
cout<<"cgpa\t\t"<<t->cgpa<<endl;
cout<<"-------------------------------------"<<endl;
t=t->n;
}
}
}
};
int _tmain(int argc, _TCHAR* argv[])
{
int a;
student s;
cout<<"Please press"<<endl;
cout<<"\t\t"<<"1 to Enqueue student in Queue"<<endl;
cout<<"\t\t"<<"2 to D2queue student in Queue"<<endl;
cout<<"\t\t"<<"3 to Display student in Queue"<<endl;
cout<<"\t\t"<<"4 to Exit student in Queue"<<endl;
cin>>a;
while(a!=4)
{
switch(a)
{
case 1:
s.Enqueue();
break;
case 2:
s.Dequeue();
break;
case 3:
s.Display();
break;
default:
cout<<"Invalid Option"<<endl;
}
cout<<"Please press"<<endl;
cout<<"\t\t"<<"1 to Enqueue student in Queue"<<endl;
cout<<"\t\t"<<"2 to D2queue student in Queue"<<endl;
cout<<"\t\t"<<"3 to Display student in Queue"<<endl;
cout<<"\t\t"<<"4 to Exit student in Queue"<<endl;
cin>>a;
}
system("pause");
return 0;
}
Queue via Array
1. Output Restricted Queue and size is static

#include "stdafx.h"
#include<iostream>
#include<string>
using namespace std;
class student
{
public:
int id, age;
float cgpa;
string name;
};
student q[5];
int top=-1;
void Enqueue()
{
if(top<4)
{
top++;
cout<<"Please enter id of the student\t";
cin>>q[top].id;
cout<<"Please enter age of the student\t";
cin>>q[top].age;
cout<<"Please enter cgpa of the student\t";
cin>>q[top].cgpa;
cout<<"Please enter name of the student\t";
flushall();
getline(cin,q[top].name);
}
else
cout<<"Queue is full"<<endl;

}
void Dequeue()
{
if(top<0)
cout<<"Queue is empty"<<endl;
else
{
cout<<"Student having"<<endl;
cout<<"\t\t"<<q[0].id<<"\tas id"<<endl;
cout<<"\t\t"<<q[0].age<<"\tas age"<<endl;
cout<<"\t\t"<<q[0].name<<"\tas name"<<endl;
cout<<"\t\t"<<q[0].cgpa<<"\tas cgpa"<<endl;
cout<<"is going to Dequeue from Queue"<<endl;
for(int i=0; i<top; i++)
q[i]=q[i+1];
top--;
}
}
void Display()
{
if(top<0)
{
cout<<"Queue is empty"<<endl;
}
else
{
for(int i=0; i<=top; i++)
{
cout<<"Id\t\t"<<q[i].id<<endl;
cout<<"Name\t\t"<<q[i].name<<endl;
cout<<"age\t\t"<<q[i].age<<endl;
cout<<"cgpa\t\t"<<q[i].cgpa<<endl;
cout<<"-------------------------------------"<<endl;

}
}
}

int _tmain(int argc, _TCHAR* argv[])


{
int a;

cout<<"Please press"<<endl;
cout<<"\t\t"<<"1 to Enqueue student in Queue"<<endl;
cout<<"\t\t"<<"2 to D2queue student in Queue"<<endl;
cout<<"\t\t"<<"3 to Display student in Queue"<<endl;
cout<<"\t\t"<<"4 to Exit student in Queue"<<endl;
cin>>a;
while(a!=4)
{
switch(a)
{
case 1:
Enqueue();
break;
case 2:
Dequeue();
break;
case 3:
Display();
break;
default:
cout<<"Invalid Option"<<endl;
}
cout<<"Please press"<<endl;
cout<<"\t\t"<<"1 to Enqueue student in Queue"<<endl;
cout<<"\t\t"<<"2 to D2queue student in Queue"<<endl;
cout<<"\t\t"<<"3 to Display student in Queue"<<endl;
cout<<"\t\t"<<"4 to Exit student in Queue"<<endl;
cin>>a;
}
system("pause");
return 0;
}
Output Restricted Queue via dynamic array

#include "stdafx.h"
#include<iostream>
#include<string>
using namespace std;
class student
{
public:
int id, age;
float cgpa;
string name;
};

int top=-1;
void Enqueue(student q[], int s)
{
if(top<s-1)
{
top++;
cout<<"Please enter id of the student\t";
cin>>q[top].id;
cout<<"Please enter age of the student\t";
cin>>q[top].age;
cout<<"Please enter cgpa of the student\t";
cin>>q[top].cgpa;
cout<<"Please enter name of the student\t";
flushall();
getline(cin,q[top].name);
}
else
cout<<"Queue is full"<<endl;

}
void Dequeue(student q[])
{
if(top<0)
cout<<"Queue is empty"<<endl;
else
{
cout<<"Student having"<<endl;
cout<<"\t\t"<<q[0].id<<"\tas id"<<endl;
cout<<"\t\t"<<q[0].age<<"\tas age"<<endl;
cout<<"\t\t"<<q[0].name<<"\tas name"<<endl;
cout<<"\t\t"<<q[0].cgpa<<"\tas cgpa"<<endl;
cout<<"is going to Dequeue from Queue"<<endl;
for(int i=0; i<top; i++)
q[i]=q[i+1];
top--;
}
}
void Display(student q[])
{
if(top<0)
{
cout<<"Queue is empty"<<endl;
}
else
{
for(int i=0; i<=top; i++)
{
cout<<"Id\t\t"<<q[i].id<<endl;
cout<<"Name\t\t"<<q[i].name<<endl;
cout<<"age\t\t"<<q[i].age<<endl;
cout<<"cgpa\t\t"<<q[i].cgpa<<endl;
cout<<"-------------------------------------"<<endl;

}
}
}

int _tmain(int argc, _TCHAR* argv[])


{
int a;
int size;
cout<<"Please enter size of queue"<<endl;
cin>>size;
student *q=new student[size];
cout<<"Please press"<<endl;
cout<<"\t\t"<<"1 to Enqueue student in Queue"<<endl;
cout<<"\t\t"<<"2 to D2queue student in Queue"<<endl;
cout<<"\t\t"<<"3 to Display student in Queue"<<endl;
cout<<"\t\t"<<"4 to Exit student in Queue"<<endl;
cin>>a;
while(a!=4)
{
switch(a)
{
case 1:
Enqueue(q,size);
break;
case 2:
Dequeue(q);
break;
case 3:
Display(q);
break;
default:
cout<<"Invalid Option"<<endl;
}
cout<<"Please press"<<endl;
cout<<"\t\t"<<"1 to Enqueue student in Queue"<<endl;
cout<<"\t\t"<<"2 to D2queue student in Queue"<<endl;
cout<<"\t\t"<<"3 to Display student in Queue"<<endl;
cout<<"\t\t"<<"4 to Exit student in Queue"<<endl;
cin>>a;
}
system("pause");
return 0;
}
Input Restricted Queue via Dynamic Array

#include "stdafx.h"
#include<iostream>
#include<string>
using namespace std;
class student
{
public:
int id, age;
float cgpa;
string name;
};

int top=-1;
void Enqueue(student q[], int s)
{
if(top<s-1)
{
for(int i=top; i>=0; i--)
q[i+1]=q[i];
top++;
cout<<"Please enter id of the student\t";
cin>>q[0].id;
cout<<"Please enter age of the student\t";
cin>>q[0].age;
cout<<"Please enter cgpa of the student\t";
cin>>q[0].cgpa;
cout<<"Please enter name of the student\t";
flushall();
getline(cin,q[0].name);
}
else
cout<<"Queue is full"<<endl;

}
void Dequeue(student q[])
{
if(top<0)
cout<<"Queue is empty"<<endl;
else
{
cout<<"Student having"<<endl;
cout<<"\t\t"<<q[top].id<<"\tas id"<<endl;
cout<<"\t\t"<<q[top].age<<"\tas age"<<endl;
cout<<"\t\t"<<q[top].name<<"\tas name"<<endl;
cout<<"\t\t"<<q[top].cgpa<<"\tas cgpa"<<endl;
cout<<"is going to Dequeue from Queue"<<endl;

top--;
}
}
void Display(student q[])
{
if(top<0)
{
cout<<"Queue is empty"<<endl;
}
else
{
for(int i=top; i>=0; i--)
{
cout<<"Id\t\t"<<q[i].id<<endl;
cout<<"Name\t\t"<<q[i].name<<endl;
cout<<"age\t\t"<<q[i].age<<endl;
cout<<"cgpa\t\t"<<q[i].cgpa<<endl;
cout<<"-------------------------------------"<<endl;

}
}
}

int _tmain(int argc, _TCHAR* argv[])


{
int a;
int size;
cout<<"Please enter size of queue"<<endl;
cin>>size;
student *q=new student[size];
cout<<"Please press"<<endl;
cout<<"\t\t"<<"1 to Enqueue student in Queue"<<endl;
cout<<"\t\t"<<"2 to D2queue student in Queue"<<endl;
cout<<"\t\t"<<"3 to Display student in Queue"<<endl;
cout<<"\t\t"<<"4 to Exit student in Queue"<<endl;
cin>>a;
while(a!=4)
{
switch(a)
{
case 1:
Enqueue(q,size);
break;
case 2:
Dequeue(q);
break;
case 3:
Display(q);
break;
default:
cout<<"Invalid Option"<<endl;
}
cout<<"Please press"<<endl;
cout<<"\t\t"<<"1 to Enqueue student in Queue"<<endl;
cout<<"\t\t"<<"2 to D2queue student in Queue"<<endl;
cout<<"\t\t"<<"3 to Display student in Queue"<<endl;
cout<<"\t\t"<<"4 to Exit student in Queue"<<endl;
cin>>a;
}
system("pause");
return 0;
}
Circular Queue via Array
#include "stdafx.h"
#include<iostream>
#include<string>
using namespace std;
class student
{
public:
int id, age;
float cgpa;
string name;
};
int f=0;
int r=0;
int top=-1;
void Enqueue(student q[], int s)
{
if(f==1&&r-top==1)
cout<<"Queue is full"<<endl;
else
{

top++;
cout<<"Please enter id of the student\t";
cin>>q[top].id;
cout<<"Please enter age of the student\t";
cin>>q[top].age;
cout<<"Please enter cgpa of the student\t";
cin>>q[top].cgpa;
cout<<"Please enter name of the student\t";
flushall();
getline(cin,q[top].name);
if(top==s-1)
{
top=-1;
f=1;
}
}

}
void Dequeue(student q[], int s)
{
if(f==0&&r>top)
cout<<"Queue is empty"<<endl;
else
{

cout<<"Student having"<<endl;
cout<<"\t\t"<<q[r].id<<"\tas id"<<endl;
cout<<"\t\t"<<q[r].age<<"\tas age"<<endl;
cout<<"\t\t"<<q[r].name<<"\tas name"<<endl;
cout<<"\t\t"<<q[r].cgpa<<"\tas cgpa"<<endl;
cout<<"is going to Dequeue from Queue"<<endl;

r++;
if(r==s)
{
r=0;
f=0;
}

}
}
void Display(student q[], int s)
{
if(f==0&&r>top)
{
cout<<"Queue is empty"<<endl;
}
else
{
if(f==0)
{
for(int i=r; i<=top; i++)
{
cout<<"Id\t\t"<<q[i].id<<endl;
cout<<"Name\t\t"<<q[i].name<<endl;
cout<<"age\t\t"<<q[i].age<<endl;
cout<<"cgpa\t\t"<<q[i].cgpa<<endl;
cout<<"-------------------------------------"<<endl;

}
}
else
{
for(int i=r; i<s; i++)
{
cout<<"Id\t\t"<<q[i].id<<endl;
cout<<"Name\t\t"<<q[i].name<<endl;
cout<<"age\t\t"<<q[i].age<<endl;
cout<<"cgpa\t\t"<<q[i].cgpa<<endl;
cout<<"-------------------------------------"<<endl;
}
for(int i=0; i<=top; i++)

{
cout<<"Id\t\t"<<q[i].id<<endl;
cout<<"Name\t\t"<<q[i].name<<endl;
cout<<"age\t\t"<<q[i].age<<endl;
cout<<"cgpa\t\t"<<q[i].cgpa<<endl;
cout<<"-------------------------------------"<<endl;
}

}
}
}

int _tmain(int argc, _TCHAR* argv[])


{
int a;
int size;
cout<<"Please enter size of queue"<<endl;
cin>>size;
student *q=new student[size];
cout<<"Please press"<<endl;
cout<<"\t\t"<<"1 to Enqueue student in Queue"<<endl;
cout<<"\t\t"<<"2 to D2queue student in Queue"<<endl;
cout<<"\t\t"<<"3 to Display student in Queue"<<endl;
cout<<"\t\t"<<"4 to Exit student in Queue"<<endl;
cin>>a;
while(a!=4)
{
switch(a)
{
case 1:
Enqueue(q,size);
break;
case 2:
Dequeue(q, size);
break;
case 3:
Display(q, size);
break;
default:
cout<<"Invalid Option"<<endl;
}
cout<<"Please press"<<endl;
cout<<"\t\t"<<"1 to Enqueue student in Queue"<<endl;
cout<<"\t\t"<<"2 to D2queue student in Queue"<<endl;
cout<<"\t\t"<<"3 to Display student in Queue"<<endl;
cout<<"\t\t"<<"4 to Exit student in Queue"<<endl;
cin>>a;
}
system("pause");
return 0;
}

You might also like