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

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--];
}

int Stack::peek(int index)


{
if (isEmpty())
return -1;
return arr[index];

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

for (int i = front; i <= rear; i++)


{
cout << arr[i] << " ";
}
}

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 revQueue(Queue *q)


{
if (q->isEmpty())
return -1;
int element = q->deQueue();
revQueue(q);
q->enQueue(element);
return 0;
}

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;

void enQueue(Stack stack, int x)


{
stack.push(x);
}

int deQueue(Stack stack)


{
Stack temp = Stack(stack.size());
while (!stack.isEmpty())
{
temp.push(stack.pop());
}
int x = temp.pop();
while (!temp.isEmpty())
{
stack.push(temp.pop());
}
return x;
}

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 (num > 0)


{
int rem = num % 2;
stack.push(rem);
num = num / 2;
}

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;

Stack duplicateStack(Stack stack, int size)


{
Stack copy = Stack(size);
for (int i = 0; i < size; i++)
{
copy.push(stack.peek(i));
}
return copy;
}

int main()
{
int size = 5;
Stack stack = Stack(size);

stack.push(5);
stack.push(2);
stack.push(8);
stack.push(3);
stack.push(9);

cout << "Original Stack: ";


stack.printStack();

Stack copy = duplicateStack(stack, size);


cout << "\nDuplicate Stack: ";
copy.printStack();
}

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;

sort(arr, arr + n);

for (int i = 0; i < n - 1; i++)


{
if (arr[i] != arr[i + 1])
{
count++;
}
}
cout << "Number of duplicate values is " << count << endl;
}

Q10. Write a program for Matrix multiplication of two matrices having different sizes?

#include <istream>
using namespace std;

void printMatrix(int arr[][])


{
for (int i = 0; i < arr.length(); i++)
{
for (int j = 0; j < i.length(); j++)
{
cout << arr[i][j] << " ";
}
cout << endl;
}
}

int[][] matmul(int m1[][], int m2[][])


{
if (m1[0].length != m2.length)
{
cout << "Matrix multiplication is not possible";
int empty[0][0] = { {} };
return empty;
}

int res[][] = new int[m1.length][m2[0].length];


for (int x = 0; x < m1.length; x++)
{

9 / 10
Assignment_1_cpp.md 9/25/2022

for (int i = 0; i < m2[i].length; i++)


{
int sum = 0;
for (int j = 0; j < m1[i].length; j++)
{
sum += m1[x][j] * m2[j][i];
}
res[x][i] = sum;
}
}
return res;
}

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

You might also like