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

Muneeb Khokhar (051) Data Structure Lab Assign

The document contains code for implementing queue data structures using different methods like stacks, linked lists and arrays. It includes 4 codes - the first code implements a queue using two stacks. The second code implements a stack using two queues. The third code implements a queue using linked lists. The fourth code implements a queue using an array. Each code contains functions for enqueue, dequeue, peek, size and other standard queue operations.

Uploaded by

Waqas Sami
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

Muneeb Khokhar (051) Data Structure Lab Assign

The document contains code for implementing queue data structures using different methods like stacks, linked lists and arrays. It includes 4 codes - the first code implements a queue using two stacks. The second code implements a stack using two queues. The third code implements a queue using linked lists. The fourth code implements a queue using an array. Each code contains functions for enqueue, dequeue, peek, size and other standard queue operations.

Uploaded by

Waqas Sami
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 20

ASSIGNMENT # 03

DATA STRUCTURE & ALGORITHMS

SUBMITTED TO:
Ma’am Ayesha Mubashira

SUBMITTED BY:
Malik Muneeb Rasheed Khokhar
ROLL NO:
SP21-BCS-051
COMSATS UNIVERSITY ISLAMABAD
VEHARI CAMPUS
CODE # 01
Queue Using Stack
#include <bits/stdc++.h>
using namespace std;
struct Queue {
stack<int> s1, s2;

//Making enqueue function


void enQueue(int x)
{

// Move all elements from s1 to s2


while (!s1.empty()) {
s2.push(s1.top());
s1.pop();
}

// Push item into s1


s1.push(x);

// Push everything back to s1


while (!s2.empty()) {
s1.push(s2.top());

s2.pop();
}
}
int deQueue()
{

// if first stack is empty


if (s1.empty()) {
cout << "Q is Empty";
exit(0);
}

// Return top of s1
int x = s1.top();
s1.pop();
return x;
}
};
int main()
{
Queue q;
q.enQueue(1);
q.enQueue(2);
q.enQueue(3);
cout << q.deQueue() << '\n';
cout << q.deQueue() << '\n';
cout << q.deQueue() << '\n';
return 0;
}
Code # 02
Stack using Queue
#include <bits/stdc++.h>
using namespace std;
class Stack {

// Two inbuilt queues


queue<int> q1, q2;
public:
void push(int x)
{

// Push x first in empty q2


q2.push(x);

// Push all the remaining


// elements in q1 to q2.
while (!q1.empty()) {
q2.push(q1.front());
q1.pop();
}

// swap the names of two queues


queue<int> q = q1;
q1 = q2;
q2 = q;
}
void pop()
{

// if no elements are there in q1


if (q1.empty())
return;
q1.pop();

}
int top()
{
if (q1.empty())
return -1;
return q1.front();
}
int size() { return q1.size(); }
};
int main()
{
Stack s;
s.push(1);
s.push(2);
s.push(3);
cout << "current size: " << s.size() << endl;
cout << s.top() << endl;
s.pop();
cout << s.top() << endl;
s.pop();
cout << s.top() << endl;
cout << "current size: " << s.size() << endl;
return 0;
}
CODE # 03
QUEUE USING LINKED LIST
#include <iostream>
using namespace std;

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

Node(int d)
{
this->data = d;
this->next = NULL;
}
};

void push(Node* &head)


{
int d;
cout<<"Insert the element in queue : ";
cin>>d;
Node* temp = new Node(d);
if(head == NULL)
{
head = temp;
}
else
{
Node* temp2 = head;
while(temp2->next != NULL)
{
temp2 = temp2->next;
}
temp2->next = temp;
temp->next = NULL;
}
cout<<d<<" is added in queue."<<endl;
}

void pop(Node* &head)


{
if(head == NULL)
{
cout<<"Queue is empty. Try inserting a element in queue first."<<endl;
}
else
{
Node* temp = head;
cout<<temp->data<<" is deleted from queue."<<endl;
head = temp->next;
delete temp;
}
}

void count(Node* &head)


{
int val, count;
count = 0;
if (head == NULL)
{
cout<<"Queue is Empty "<<endl;
}
else
{
cout<<"Enter value to count in queue: ";
cin>>val;
Node* temp;
temp = head;
while(temp->next != NULL)
{
if(temp->data == val)
{
cout<<val<<" found."<<endl;
temp = temp->next;
count++;
}
else
{
temp = temp->next;
}
}
if(temp->data == val)
{
cout<<val<<" found."<<endl;
count++;
}
cout<<"Total numbers of "<<val<<" in Queue are: "<<count<<endl;
}
}

void frnt(Node* &head)


{
if(head == NULL)
{
cout<<"Queue is Empty."<<endl;
}
else
{
cout<<"Value of front is: "<<head->data<<endl;
}
}

void bck(Node* &head)


{
if(head == NULL)
{
cout<<"Queue is Empty."<<endl;
}
else
{
Node* temp1;
temp1 = head;
while(temp1->next != NULL)
{
temp1 = temp1->next;
}
cout<<"Value of back is: "<<temp1->data<<endl;
}
}

void size(Node* &head)


{
int count=1;
if(head == NULL)
{
cout<<"Queue is Empty."<<endl;
}
else
{
Node* temp;
temp = head;
while(temp->next != NULL)
{
temp = temp->next;
count++;
}
cout<<"Size of Queue is: "<<count<<endl;
delete temp;
}
}

void empty(Node* &head)


{
if(head == NULL)
{
cout<<"True"<<endl;
}
else
{
cout<<"False"<<endl;
}
}

void Display(Node* &head)


{
if(head == NULL)
{
cout<<"Queue is empty."<<endl;
}
else
{
Node* temp;
temp = head;
cout<<"Elements of Queue are: "<<temp->data<<" ";
while(temp->next != NULL)
{
temp = temp->next;
cout<<temp->data<<" ";
}
cout<<endl;
}
}

int main()
{
Node* head;
head = NULL;
int ch;

while(true)
{
cout<<"1) Insert element to queue"<<endl;
cout<<"2) Delete element from queue"<<endl;
cout<<"3) Count element from queue"<<endl;
cout<<"4) Front element from queue"<<endl;
cout<<"5) Back element from queue"<<endl;
cout<<"6) Size of queue"<<endl;
cout<<"7) Check Empty"<<endl;
cout<<"8) Display Queue"<<endl;
cout<<"9) Exit"<<endl;
cout<<endl;
cout<<"Enter your choice : ";
cin>>ch;
switch (ch)
{
case 1: push(head);
break;
case 2: pop(head);
break;
case 3: count(head);
break;
case 4: frnt(head);
break;
case 5: bck(head);
break;
case 6: size(head);
break;
case 7: empty(head);
break;
case 8: Display(head);
break;
case 9: cout<<"Exit"<<endl;
break;
default: cout<<"Invalid choice"<<endl;
}
if(ch == 9)
{
break;
}
else
{
cout<<endl;
continue;
}
}

return 0;
}

CODE # 04
Queue Using Array
#include <iostream>
using namespace std;
int n=100, queue[100], front = - 1, rear = - 1;
void push()
{
int val;
if (rear == n - 1)
cout<<"Queue Overflow"<<endl;
else
{
front = 0;
cout<<"Insert the element in queue : ";
cin>>val;
rear++;
queue[rear] = val;
}
}
void pop()
{
if (front == - 1 || front > rear)
{
cout<<"Queue Underflow "<<endl;
return ;
}
else
{
cout<<"Element deleted from queue is : "<< queue[front] <<endl;
front++;
if(front>rear)
{
front = -1;
rear = -1;
}
}
}
void count()
{
int val, count;
count = 0;
if (front == - 1 || front > rear)
{
cout<<"Queue is Empty "<<endl;
return ;
}
else
{
cout<<"Enter value to count in queue: ";
cin>>val;
for(int i=front; i<=rear; i++)
{
if(queue[i] == val)
{
count++;
}
else
{
continue;
}
}
cout<<"Total numbers of "<<val<<" in Queue are: "<<count<<endl;
}
}
void frnt()
{
if (front == - 1 || front > rear)
{
cout<<"Queue is Empty "<<endl;
return ;
}
else
{
cout<<"Value of front is: "<<queue[front]<<endl;
}
}
void bck()
{
if (front == - 1 || front > rear)
{
cout<<"Queue is Empty "<<endl;
return ;
}
else
{
cout<<"Value of back is: "<<queue[rear]<<endl;
}
}
void size()
{
if (front == - 1 || front > rear)
{
cout<<"Queue is Empty "<<endl;
return ;
}
else
{
int size;
for(int i=front; i<=rear; i++)
{
size++;
}
cout<<"Size of queue is: "<<size<<endl;
}
}
void empty()
{
if (front == - 1 || front > rear)
{
cout<<"True"<<endl;
return ;
}
else
{
cout<<"False"<<endl;
}
}
void Display()
{
if (front == - 1)
cout<<"Queue is empty"<<endl;
else
{
cout<<"Queue elements are : ";
for (int i = front; i <= rear; i++)
cout<<queue[i]<<" ";
cout<<endl;
}
}
int main()
{
int ch;

while(true)
{
cout<<endl;
cout<<"1) Insert element to queue"<<endl;
cout<<"2) Delete element from queue"<<endl;
cout<<"3) Count element from queue"<<endl;
cout<<"4) Front element from queue"<<endl;
cout<<"5) Back element from queue"<<endl;
cout<<"6) Size of queue"<<endl;
cout<<"7) Check Empty"<<endl;
cout<<"8) Display Queue"<<endl;
cout<<"9) Exit"<<endl;
cout<<endl;
cout<<"Enter your choice : ";
cin>>ch;
switch (ch)
{
case 1: push();
break;
case 2: pop();
break;
case 3: count();
break;
case 4: frnt();
break;
case 5: bck();
break;
case 6: size();
break;
case 7: empty();
break;
case 8: Display();
break;
case 9: cout<<"Exit"<<endl;
break;
default: cout<<"Invalid choice"<<endl;
}
if(ch == 9)
{
break;
}
else
{
continue;
}
}

return 0;
}

You might also like