Professional Documents
Culture Documents
SOURCE CODE:
#include<iostream>
struct node
int data;
node* next;
};
class Queue
public:
node* rear,*front;
Queue()
rear = NULL;
front = NULL;
void enqueue(int a)
newnode->data = a;
newnode->next = NULL;
if (front==NULL)
{
front = newnode;
else
rear->next = newnode;
rear = newnode;
int dequeue()
if (IsEmpty())
return 0;
else
node* ptr;
ptr = front;
front = front->next;
ptr = NULL;
delete ptr;
return dat;
}
bool IsEmpty()
if (front == NULL)
return true;
return false;
Queue reverse_Queue(Queue a)
int store;
if (a.IsEmpty())
return a;
else
store = a.dequeue();
a=reverse_Queue(a);
a.enqueue(store);
a.check = true;
return a;
};
int main()
Queue a;
int data,option = 0;
while (option!=5)
if (option==1)
cout << "Enter Data That You Want to Enqueue in Queue : ";
a.enqueue(data);
else if (option == 2)
if (a.IsEmpty())
data = a.dequeue();
}
else
data = a.dequeue();
else if (option == 3)
if (a.IsEmpty())
else
else if (option == 4)
if (a.IsEmpty())
else
{
a = a.reverse_Queue(a);
if (a.check==true)
system("pause");
return 0;
}
CONSOLE SCREEN SHOT:
TASK 2:
SOURCE CODE:
#include<iostream>
struct node
int data;
node* next;
};
class Queue
public:
int count=0;
Queue()
rear = NULL;
front = NULL;
void enqueue(int a)
count++;
newnode->data = a;
newnode->next = NULL;
if (front == NULL)
{
front = newnode;
else
rear->next = newnode;
rear = newnode;
int dequeue()
if (IsEmpty())
return 0;
else
count--;
node* ptr;
ptr = front;
front = front->next;
ptr->next = NULL;
free(ptr);
return dat;
}
void Display()
if (ptr==NULL)
return;
ptr = ptr->next;
int Largest()
int max = 0;
int i = 0;
int* array;
while (!IsEmpty())
{
array[i] = dequeue();
if (array[i]>max)
max = array[i];
i++;
enqueue(array[i]);
return max;
int Smallest()
int min=front->data;
int i = 0;
int* array;
while (!IsEmpty())
array[i] = dequeue();
min = array[i];
i++;
enqueue(array[i]);
return min;
int get_Size()
return count;
bool IsEmpty()
if (front == NULL)
return true;
return false;
}
}
void Perfect()
int i = 0;
int* array;
while (!IsEmpty())
array[i] = dequeue();
if (IsPerfect(array[i]))
i++;
enqueue(array[i]);
DivRes = data % i;
if (DivRes == 0)
SumOfDivRes = SumOfDivRes + i;
if (SumOfDivRes == data)
return true;
else
return false;
void Sort()
int temp = 0;
int i = 0;
int* array;
array[i] = dequeue();
i++;
temp = array[i];
array[i] = array[j];
array[j] = temp;
enqueue(array[i]);
};
int main()
{
Queue a;
while (option != 9)
if (option == 1)
cout << "Enter Data That You Want to Enqueue in Queue : ";
a.enqueue(data);
else if (option == 2)
if (a.IsEmpty())
data = a.dequeue();
else
data = a.dequeue();
else if (option == 3)
{
else if (option == 4)
a.Display();
else if (option == 5)
else if (option == 6)
cout << "Smallest Element in Queue is : " << a.Smallest() << endl;
else if (option == 7)
a.Perfect();
else if (option == 8)
a.Sort();
a.Display();
}
else
exit(1);
system("pause");
return 0;
}
CONSOLE SCREEN SHOT:
TASK 3:
SOURCE CODE:
#include<iostream>
using namespace std;
struct node
int data;
node* Next;
};
class Link
public:
public:
Link()
void Enque(int a)
temp->data = a;
temp->Next = front;
if (front == NULL)
else
{
rear->Next = temp;
rear = temp;
rear->Next = front;
void Deque()
if (ptr == NULL)
return;
//when both head and rear are at same position it means its the only node in queue
else if(front==rear)
delete front;
delete rear;
else
rear->Next=front->Next;
front = front->Next;
ptr = NULL;
delete ptr;
bool IsEmpty()
if (front==NULL)
return true;
else
return false;
void Display()
if (ptr==NULL)
return;
{
cout << ptr->data << "->";
ptr = ptr->Next;
};
int main()
int data,option=0;
Link a;
while (option != 5)
if (option == 1)
system("cls");
a.Enque(data);
else if (option == 2)
system("cls");
if (a.IsEmpty())
else
a.Deque();
a.Display();
else if (option == 3)
system("cls");
if (a.IsEmpty())
else
else if (option == 4)
{
system("cls");
a.Display();
else
exit(1);
system("pause");
return 0;
#include<stack>
class Queue
public:
stack<int>a;
stack<int>b;
int temp;
while (!a.empty() )
temp = a.top();
a.pop();
b.push(temp);
a.push(dat);
while (!b.empty())
b.pop();
a.push(temp);
}
int deque()
int temp;
if (a.empty())
return 0;
temp = a.top();
a.pop();
return temp;
bool Isempty()
if (a.empty())
return true;
else
return false;
}
};
int main()
Queue a;
while (option != 4)
if (option == 1)
a.Enque(data);
else if (option == 2)
if (a.Isempty())
else
}
}
else if(option==3)
if (a.Isempty())
else
system("pause");
return 0;
}
CONSOLE SCREEN SHOT:
TASK 5:
SOURCE CODE:
#include<iostream>
struct node
int data;
int priority;
node* next;
};
class Queue
public:
int count=0;
Queue()
rear = NULL;
front = NULL;
count++;
newnode->data = a;
newnode->priority = p;
newnode->next = NULL;
newnode->next = front;
front = newnode;
else
ptr = ptr->next;
newnode->next = ptr->next;
ptr->next = newnode;
bool IsEmpty()
if (front==NULL)
return true;
else
return false;
int dequeue()
if (IsEmpty())
return 0;
else
count--;
node* ptr;
ptr = front;
front = front->next;
ptr = NULL;
free(ptr);
return dat;
void Display()
if (ptr==NULL)
return;
ptr = ptr->next;
};
class Stack
public:
Queue obj;
node* top;
Stack()
top = NULL;
obj.enqueue(a,1);
int pop()
int temp;
if (IsEmpty())
return 0;
else
temp = obj.front->data;
obj.dequeue();
}
return temp;
bool IsEmpty()
if (obj.IsEmpty())
return true;
else
return false;
};
int main()
Stack a;
while (option != 4)
if (option == 1)
{
a.push(data,1);
else if (option == 2)
if (a.IsEmpty())
else
else if(option==3)
if (a.IsEmpty())
else
system("pause");
return 0;
}
CONSOLE SCREEN SHOT:
TASK 6:
SOURCE CODE: