Professional Documents
Culture Documents
Assignment 1 CPP
Assignment 1 CPP
md 9/25/2022
Stack Implimentation
#include <iostream>
using namespace std;
class Stack
{
private:
int capacity;
int *arr;
int top;
public:
Stack(int size);
void push(int x);
int pop();
int peek(int index);
int size();
bool isEmpty();
bool isFull();
void printStack();
};
Stack::Stack(int size)
{
capacity = size;
arr = new int[size];
top = -1;
}
void Stack::push(int x)
{
if (isFull())
cout << "Stack Overflow";
else
arr[++top] = x;
}
int Stack::pop()
{
if (isEmpty())
cout << "Stack Underflow";
return arr[top--];
}
1 / 10
Assignment_1_cpp.md 9/25/2022
int Stack::size()
{
return top + 1;
}
bool Stack::isEmpty()
{
return top == -1;
}
bool Stack::isFull()
{
return top == capacity - 1;
}
void Stack::printStack()
{
for (int i = 0; i <= top; i++)
{
cout << arr[i] << " ";
}
}
Queue Implimentation
#include <iostream>
using namespace std;
class Queue
{
private:
int SIZE;
int *arr;
int front, rear;
public:
Queue(int size);
void enQueue(int x);
int deQueue();
bool isFull();
bool isEmpty();
void display();
};
Queue::Queue(int size)
{
SIZE = size;
arr = new int[SIZE];
2 / 10
Assignment_1_cpp.md 9/25/2022
front = -1;
rear = -1;
}
void Queue::enQueue(int x)
{
if (isFull())
{
cout << "Queue is full";
return;
}
if (front == -1)
front = 0;
arr[++rear] = x;
}
int Queue::deQueue()
{
int x;
if (isEmpty())
{
cout << "Queue is empty";
return -1;
}
else
{
x = arr[front];
if (front >= rear)
{
front = -1;
rear = -1;
}
else
{
front++;
}
}
return x;
}
bool Queue::isEmpty()
{
return front == -1;
}
bool Queue::isFull()
{
return (front == 0 && rear == SIZE - 1);
}
void Queue::display()
{
if (isEmpty())
cout << "Queue is empty";
3 / 10
Assignment_1_cpp.md 9/25/2022
Q1. Write a recursive function that takes as input a queue and rearranges it so that it is in reverse order.
Hint: deque() the first element, recursively reverse the queue, and the enqueuer the first element.
#include <iostream>
#include "Queue.cpp"
using namespace std;
int main()
{
int size = 5;
Queue *q = new Queue(size);
q->enQueue(3);
q->enQueue(5);
q->enQueue(8);
q->enQueue(2);
q->enQueue(7);
revQueue(q);
q->display();
return 0;
}
Q2. Write a program from scratch that reverses the words in a sentence.
#include <iostream>
using namespace std;
int main()
{
string s = "Hello this code is written in CPP";
int len = s.length();
int j = 0;
for (int i = 0; i < len; i++)
4 / 10
Assignment_1_cpp.md 9/25/2022
{
if (s[i] == ' ')
{
reverse(s.begin() + j, s.begin() + i);
j = i + 1;
}
else if (i == len - 1)
{
reverse(s.begin() + j, s.end());
}
}
cout << s;
return 0;
}
Q3. Use your stack implementation to implement a queue using the Stack.
#include <iostream>
#include "Stack.cpp"
using namespace std;
int main()
{
int size = 5;
Stack stack = Stack(size);
enQueue(stack, 5);
stack.printStack();
cout << "hello";
}
5 / 10
Assignment_1_cpp.md 9/25/2022
Q4. Write the function transforming a decimal number into a binary number by using stack.
#include <iostream>
#include "Stack.cpp"
using namespace std;
int main()
{
int num;
cin >> num;
Stack stack = Stack(20);
while (!(stack.isEmpty()))
{
cout << stack.pop();
}
return 0;
}
Q5. Write the function that removes all even numbers from the given stack. The mutual order of odd
numbers must stay unchanged. The function returns the number of removed numbers.
#include <iostream>
#include "Stack.cpp"
using namespace std;
int main()
{
Stack stack = Stack(5);
Stack temp = Stack(5);
int removedElements = 0;
stack.push(5);
stack.push(2);
stack.push(8);
stack.push(3);
stack.push(9);
while(!stack.isEmpty())
{
int x = stack.pop();
if (x % 2 == 0)
6 / 10
Assignment_1_cpp.md 9/25/2022
temp.push(x);
else
removedElements++;
}
cout << "Total elements removed: " << removedElements << endl;
}
Q6. Write the function that returns duplicate stack of the given stack. Duplicate stack contains the same
elements as the original stack, and in the same order. The original stack must stay unchanged.
#include <iostream>
#include "Stack.cpp"
using namespace std;
int main()
{
int size = 5;
Stack stack = Stack(size);
stack.push(5);
stack.push(2);
stack.push(8);
stack.push(3);
stack.push(9);
Q7. For each of the use cases below pick a data structure (from the ones you have seen in the lecture) that
is best suited for that use case, and explain your choice. If you think there is more than one suitable data
structure, briefly discuss the trade-offs.
. You want to store the stations of a public transportation line. New stations can be added to both ends
of the line, but not between existing stations. You should be able to traverse the line in both
7 / 10
Assignment_1_cpp.md 9/25/2022
directions.
Ans: We will use Deque which is Double Ended Queue as we can insert and remove data from both ends of
the queue which will be useful in adding tranport data.
. You want to store a phone book, which supports looking up a phone number by name, as well as
adding and removing entries.
Ans: We will use Hash Table as it allows storing data in key value pairs which makes it much easier to map
each phone number with the person's name.
. You are looking for a way out of a maze, and you are not allowed to use recursion. You have to store
the path you are currently exploring, and be able to go back one step whenever you find yourself in a
dead-end and explore a new possibility from there.
. You want to store a sorted list of strings and support the operation of merging two sorted lists into
one, in place (without creating a copy of the lists).
. You are writing software for a call center. When a client calls, his call should be stored until there is a
free operator to pick it up. Calls should be processed in the same order they are received.
Q8. A palindrome is a phrase that reads the same forward and backward (examples: ‘racecarʼ, ‘radarʼ, ‘noonʼ,
or ‘rats live on no evil starʼ). By extension we call every string a palindrome that reads the same from left to
right and from right to left. Develop a recursive algorithm that takes as input a string and decides whether
the string is a palindrome. Write down your algorithm in pseudocode.
#include <iostream>
using namespace std;
bool palindromeChecker(string s)
{
int len = s.length();
if (len < 2)
return true;
if (s[0] != s[len - 1])
return false;
return palindromeChecker(s.substr(1, len - 2));
}
int main()
{
string s = "radar";
int len = s.length();
cout << "Is Palindrome = " << palindromeChecker(s) << endl;
return 0;
}
Q9. Write a program to remove the duplicate elements of a given array and return the new length of the
array. Sample array: [20, 20, 30, 40, 50, 50, 50] After removing the duplicate elements, the program should
return 4 as the new length of the array.
8 / 10
Assignment_1_cpp.md 9/25/2022
#include <iostream>
using namespace std;
int main()
{
int arr[] = {20, 20, 30, 40, 50, 50, 50};
int n = sizeof(arr) / sizeof(arr[0]);
int count = 0;
Q10. Write a program for Matrix multiplication of two matrices having different sizes?
#include <istream>
using namespace std;
9 / 10
Assignment_1_cpp.md 9/25/2022
int main()
{
int m1[2][3] = { { 1, 2, 3 }, { 4, 5, 6 } };
int m2[3][2] = { { 7, 8 }, { 9, 10 }, { 11, 12 } };
return 0;
}
10 / 10