Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Dsa4 102115107

Download as pdf or txt
Download as pdf or txt
You are on page 1of 24

Data Structures And Algorithms

UCS613

Assignment-4

Madhav Goyal
102115107
2NC5
Q1. Implement the following stack operations using arrays:
● Push
● Pop
● IsFull
● isEmpty
● Peek
● stackTop

Code:
#include <iostream>

using namespace std;

class Stack
{
private:
int stack[100], top, size;

public:
Stack()
{
top = -1;
size = 0;
}
bool isEmpty()
{
if (top < 0)
{
return true;
}
return false;
}
bool isFull()
{
if (top == 99)
{
return true;
}
return false;
}
void push(int a)
{
if (!isFull())
{
size++;
top++;
stack[top] = a;
}
}
void pop()
{
if (!isEmpty())
{
size--;
top--;
}
}
int peek()
{
return stack[top];
}
int stackTop()
{
return stack[top];
}
};

int main()
{
Stack s;
int n;
cout << "Enter size" << endl;
cin >> n;
for (int i = 0; i < n; i++)
{
int a;
cin >> a;
s.push(a);
}
cout << "Top Elem: " << s.peek() << endl;
s.pop();
cout << (s.isEmpty() ? "Empty" : "Not empty") << endl;
s.pop();
cout << (s.isFull() ? "Full" : "Not Full (Limit: 100)") << endl;
cout << "Afte 2 Pop(s) Top Elem: " << s.stackTop() << endl;

return 0;
}

Output:
Q2. Implement the following stack operations using linked List:
● Push
● Pop
● IsFull
● isEmpty
● Peek

Code:
#include <iostream>

using namespace std;

class ListNode
{
public:
int val;
ListNode *next;
ListNode(int val = 0, ListNode *next = nullptr)
{
this->val = val;
this->next = next;
}
};

class Stack
{
public:
ListNode *stack;
int top, size;
Stack()
{
top = -1;
size = 0;
}
bool isEmpty()
{
if (top < 0)
{
return true;
}
return false;
}
bool isFull()
{
if (top == 99)
{
return true;
}
return false;
}
void push(int a)
{
if (!isFull())
{
ListNode *current = new ListNode(a, stack);
stack = current;
size++;
top++;
}
}
void pop()
{
if (!isEmpty())
{
ListNode *current = stack;
stack = stack->next;
size--;
top--;
delete current;
}
}
int peek()
{
return stack->val;
}
};

int main()
{
Stack s;
int n;
cout << "Enter size" << endl;
cin >> n;
for (int i = 0; i < n; i++)
{
int a;
cin >> a;
s.push(a);
}
cout << "Top Elem: " << s.peek() << endl;
s.pop();
cout << (s.isEmpty() ? "Empty" : "Not empty") << endl;
s.pop();
cout << (s.isFull() ? "Full" : "Not Full (Limit: 100)") << endl;
cout << "Afte 2 Pop(s) Top Elem: " << s.peek() << endl;
return 0;
}

Output:
Q3. Implement linear queue by writing a class and also
implement the following functions:
● Insert or enqueue
● Remove or dequeue
● Isfull
● Isempty
Write a main function to exemplify the results. Also write a
main function to make the
implementation a “Menu-Driven”.
Code:
#include <iostream>

using namespace std;

class Queue
{
public:
int *queue, size, maxSize;
Queue(int size = 100)
{
queue = new int[size]{0};
maxSize = size;
this->size = 0;
}
void insert(int elem)
{
if (!isFull())
{
queue[this->size] = elem;
this->size++;
}
}
void remove()
{
if (!isEmpty())
{
int i = 1;
while (i < this->size)
{
queue[i - 1] = queue[i];
i++;
}
this->size--;
}
}
bool isEmpty()
{
if (!this->size)
{
return true;
}
return false;
}
bool isFull()
{
if (this->size == maxSize)
{
return true;
}
return false;
}
int front()
{
if (this->size)
{
return queue[0];
}
return -1;
}
};

int main()
{
int choice, elem;
Queue q;
while (true)
{
cout << "1. Insert" << endl;
cout << "2. Remove" << endl;
cout << "3. Front" << endl;
cout << "4. IsEmpty" << endl;
cout << "5. IsFull" << endl;
cout << "6. Exit" << endl;
// print current queue if queue is not empty
if (!q.isEmpty())
{
cout << "Current queue: ";
for (int i = 0; i < q.size; i++)
{
cout << q.queue[i] << " ";
}
cout << endl;
}
cout << "Enter your choice: ";
cin >> choice;
switch (choice)
{
case 1:
cout << "Enter element: ";
cin >> elem;
q.insert(elem);
break;
case 2:

q.remove();
break;
case 3:

cout << q.front() << endl;


break;
case 4:
if (q.isEmpty())
{
cout << "Queue is empty" << endl;
}
else
{
cout << "Queue is not empty" << endl;
}
break;
case 5:
if (q.isFull())
{
cout << "Queue is full" << endl;
}
else
{
cout << "Queue is not full (Limit: 100)" << endl;
}
break;
case 6:
break;
}
}
}

Output:
Insert:
Remove:

Front:

IsEmpty:
IsFull:

Q4. Implement a queue using two stacks only. Name the two as
forward and reverse stacks.
The resultant queue (comprised of two stacks) should perform
the following functions
with minimum time complexity:
i. Enqueue()
ii. Dequeue()
iii. isFull()
iv. isEmpty()

Code:
#include <iostream>

using namespace std;

class Stack
{
private:
int stack[100], top, size;

public:
Stack()
{
top = -1;
size = 0;
}
bool isEmpty()
{
if (top < 0)
{
return true;
}
return false;
}
bool isFull()
{
if (top == 99)
{
return true;
}
return false;
}
void push(int a)
{
if (!isFull())
{
size++;
top++;
stack[top] = a;
}
}
void pop()
{
if (!isEmpty())
{
size--;
top--;
}
}
int peek()
{
return stack[top];
}
};

class Queue
{
public:
Stack forward, reverse;
void Enqueue(int elem)
{
if (!forward.isFull())
{
forward.push(elem);
}
}
int dequeue()
{
if (!forward.isEmpty() || !reverse.isEmpty())
{
if (reverse.isEmpty())
{
while (!forward.isEmpty())
{
int top = forward.peek();
forward.pop();
reverse.push(top);
}
}
int front = reverse.peek();
reverse.pop();
return front;
}
else
{
return -1;
}
}
int front()
{
if (!forward.isEmpty() || !reverse.isEmpty())
{
if (reverse.isEmpty())
{
while (!forward.isEmpty())
{
int top = forward.peek();
forward.pop();
reverse.push(top);
}
}
return reverse.peek();
}
else
{
return -1;
}
}
bool isEmpty()
{
if (reverse.isEmpty())
{
return true;
}
return false;
}
bool isFull()
{
if (forward.isFull())
{
return true;
}
return false;
}
};

int main()
{
Queue q;
int n;
cout << "Enter size" << endl;
cin >> n;
for (int i = 0; i < n; i++)
{
int a;
cin >> a;
q.Enqueue(a);
}
cout << "Top Elem: " << q.front() << endl;
q.dequeue();
cout << (q.isEmpty() ? "Empty" : "Not empty") << endl;
q.dequeue();
cout << (q.isFull() ? "Full" : "Not Full") << endl;
cout << "Afte 2 Dequeues(s) Top Elem: " << q.front() << endl;
}

Output:
Q5. Implement priority queue by writing a class and also
implement the following
functions:
● Insert or enqueue
● Remove or dequeue
● Isfull
● Isempty
Write a main function to exemplify the results. Also write a
main function to make the
implementation a “Menu-Driven”.
Code:
#include <iostream>

using namespace std;

class PriorityQueue
{
public:
int *queue, begin, end, maxSize;
PriorityQueue(int size = 100)
{
queue = new int[size]{0};
maxSize = size;
begin = 0;
end = 0;
}
void insert(int elem)
{
if (!isFull())
{
queue[end] = elem;
end++;
int left = begin, right = end - 1, mid;
while (left <= right)
{
mid = (left + right) / 2;
if (queue[mid] < queue[end - 1])
{
right = mid - 1;
}
else
{
left = mid + 1;
}
}
for (int i = end; i > mid; i--)
{
queue[i] = queue[i - 1];
}
queue[mid] = elem;
}
}
void remove()
{
if (!isEmpty())
{
int size = end - begin;
if (size > (maxSize / 2))
{
int i = 0;
while (end > begin)
{
queue[i] = queue[begin];
begin++;
i++;
}
begin = 0;
end = begin + size;
}
else
{
begin++;
}
}
}
bool isEmpty()
{
int size = end - begin;
if (!size)
{
return true;
}
return false;
}
bool isFull()
{
int size = end - begin;
if (size == maxSize)
{
return true;
}
return false;
}
int front()
{
if (!isEmpty())
{
return queue[begin];
}
return -1;
}
};

int main()
{
int choice, elem;
PriorityQueue q;
bool running = true;
while (running)
{
cout << "1. Insert" << endl;
cout << "2. Remove" << endl;
cout << "3. Front" << endl;
cout << "4. IsEmpty" << endl;
cout << "5. IsFull" << endl;
cout << "6. Exit" << endl;
// print current queue if queue is not empty
if (!q.isEmpty())
{
cout << "Current queue: ";
for (int i = q.begin; i < q.end; i++)
{
cout << q.queue[i] << " ";
}
cout << endl;
}
cout << "Enter your choice: ";
cin >> choice;
switch (choice)
{
case 1:
cout << "Enter element: ";
cin >> elem;
q.insert(elem);
break;
case 2:

q.remove();
break;
case 3:

cout << q.front() << endl;


break;
case 4:

if (q.isEmpty())
{
cout << "Queue is empty" << endl;
}
else
{
cout << "Queue is not empty" << endl;
}
break;
case 5:
if (q.isFull())
{
cout << "Queue is full" << endl;
}
else
{
cout << "Queue is not full (Limit: "<< q.maxSize <<
")" << endl;
}
break;
case 6:
running = false;
break;
}
}
return 0;
}

Output:
Insert:
Remove:

Front:

IsEmpty:
IsFull:

You might also like