Tausif Nawaz CS251 Assignment 1
Tausif Nawaz CS251 Assignment 1
ASSIGNMENT-1
CS-251 DATA STRUCTURES
QUESTION 1:- Implement a Queue using 2 stacks (write the pseudocode) along with an example. What are the
complexities of enqueue() and dequeue()?
SOLUTION:- A queue can be implemented using two stacks. Let queue to be implemented
be q and stacks used to implement q be stack1 and stack2.
PSEUDO CODE:-
enQueue(q, x):
While stack1 is not empty, push everything from stack1 to stack2.
Push x to stack1 (assuming size of stacks is unlimited).
Push everything back to stack1.
Here the Time Complexity will be O(n).
deQueue(q):
If stack1 is empty then error
Pop an item from stack1 and return it
Here the time Complexity will be O (1).
EXAMPLE:-
CODE:-
// program to implement Queue using
// two stacks
#include <bits/stdc++.h>
Using namespace std;
Struct Queue {
Stack<int> s1, s2;
Void enQueue(int x)
{
S2.push(s1.top());
S1.pop();
}
// Push item into s1
S1.push(x);
// Return top of s1
Int x = s1.top();
S1.pop();
Return x;
}
};
3
// Driver code
Int main()
{
Queue q;
q.enQueue(1);
q.enQueue(2);
q.enQueue(3);
return 0;
}
OUTPUT:-
QUESTION 2:- What does the following function do for a given Linked List with first node as head?
void fun(struct node* head)
{
4
if (head == NULL)
return;
fun(head → next);
printf(“%d”, head → data);
}
QUESTION 3:- Perform (stepwise) postfix expression evaluation with one-digit and two-digit operands
using a stack:
81 5 3 ^ / 12 30 * + 15 16 *-
SOLUTION:-
First three tokens are values, so they are simply pushed. After pushing 8, 2 and 3, the stack is as follows
8, 2, 3
When ^ is read, top two are popped and power(2^3) is calculated
8, 8
When / is read, top two are popped and division(8/8) is performed
1
Next two tokens are values, so they are simply pushed. After pushing 2 and 3, the stack is as follows
1, 2, 3
When * comes, top two are popped and multiplication is performed.
1, 6
QUESTION 4:- If an array holding a queue is not considered circular, then each remove operation must shift
down every remaining element of the queue. An alternative method is to postpone shifting until
rear equals the last index of the array. When that situation occurs and an attempt is made to insert
5
an element into the queue, the entire queue is shifted down, so that the first element of the queue
is in position 0 of the array. What are the advantages of this method over performing a shift at each
remove operation? What are the disadvantages?
SOLUTION:-
ADVANTAGES:-
It will save time by shifting the entire queues at once rather than shifting it one by one each time the
remove operation is performed. In another method the head is incremented every time after deleting
an element, at once shifting is also better than that.
We don’t have to use a for loop, which will again increase the time Complexity.
DISADVANTAGES:-
It will result in waste of memory as the empty indices of the array can’t be reused.
It is less memory efficient.
QUESTION 5:- Why linked lists are not suitable for binary search?
SOLUTION:- Binary search of the data structure link lists are not suitable.
Binary search based on divide and conquer algorithm, determination of middle element is
important. Binary search is usually fast and efficient for arrays because accessing the middle index
between two given indices is easy and fast. But memory allocation for singly linked list is dynamic
and non-contiguous, which makes finding the middle element difficult. One approach could be of
using skip list; one could be traversing the linked list using one pointer.
A linked list is a linear collection of data elements, whose order is not given by their physical
placement in memory. Instead, each element points to the next. It is a data structure consisting of a
collection of nodes which together represent a sequence.
QUESTION 6:- . A priority queue Q is used to implement a stack S that stores characters. PUSH(C) is
implemented as INSERT(Q, C, K) where K is an appropriate integer key chosen by the
implementation. POP is implemented as DELETEMIN(Q). For a sequence of operations, the keys
(K) chosen are in
6
SOLUTION:-
Correct answer is (D) i.e strictly decreasing order
Explanation: We are implementing a STACK using Priority Queue. Note that Stack implementation is always
last in first out (LIFO) order.
As given “POP is implemented as DELETEMIN(Q)” that means Stack returns minimum element always.
So, we need to implement PUSH© using INSERT(Q, C, K) where K is key chosen from strictly-decreasing
order(only this order will ensure stack will return minimum element when we POP an element). That will satify
Last In First Out (LIFO) property of stack.
That is answer, option (D) is true.
Option (A) non-increasing order can not be true because two same (identical) numbers can not have same
priority as priority should be distinguishable for each number
QUESTION 7:- Consider a singly linked list having n nodes. The data items d1, d2, …., dn are stored in the n
nodes. Let Y be a pointer to the jth node (1 ≤ j ≤ n) in which dj is stored. A new data item d storedin a node
with address Y is to be inserted. Give an algorithm to insert d into the list to obtain a list having items d1,
d2, ……, dj-1, d, dj, …..dn in that order without using the header.
SOLUTION:-
Algorithm 1:- insert_mid()
1. Create newnode
2. newnode- >next=y->next
3. newnode->data=y->data. /*Which is dj*/
4. y->=newnode
5. y->data=d
Explanation: Since we didn’t have the address of node which is previous to Y. So insert a newnode after Y.
And copy the data of Y to new node and modify the data of Y to d.
Hence we got the required sequence of data as d1,d2,d3,…………dj-1,d,dj…,…..dn.
7
QUESTION 8:- . Implement a recursive method that prints all permutations of a given string. The program
takes two strings as input. The output is all rearrangements of the letters in first, followed by second.
SOLUTION:-
CODE:
#include <bits/stdc++.h>
#include <string>
using namespace std;
int main()
{
string str1,str2;
8
string answer="";
cout<<"Enter the strings : ";
cin>>str1>>str2;
cout<<"\nAll possible strings are : ";
permute(str1, answer);
permute(str2,answer);
return 0;
}
OUTPUT:-
QUESTION 9:- Let P be a singly linked list. Let Q be the pointer to an intermediate node x in the list. What is
the worst-case time complexity of the best-known algorithm to delete the node x from the list?
SOLUTION:-
Fast solution is to copy the data from the next node to the node to be deleted and delete the next node.
Something like following.
Time Complexity for this is O(1).
9
QUESTION 10:- Circularly linked list is used to represent a Queue. A single variable p is used to access the
Queue. To which node should p point such that both the operations enQueue and deQueue can be
performed in constant time?
SOLUTION:-Answer is(A)Rear Node. Explanation: Answer is not “(b) front node”, as we can not get rear from
front in O(1), but if p is rear we can implement both enQueue and deQueue in O(1) because from rear we can
get front in O(1). Below are sample functions. Note that these functions are just sample are not working. Code
to handle base cases is missing.