0% found this document useful (0 votes)
12 views13 pages

Exam 2 + Answers

The document contains sample questions and answers related to data structures and algorithms exam. It includes questions on stack operations, queue operations, linked list reversal and expression conversions. Detailed step-by-step explanations are provided for stack, queue and linked list operations based on the given inputs.

Uploaded by

karimahmad.wis
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views13 pages

Exam 2 + Answers

The document contains sample questions and answers related to data structures and algorithms exam. It includes questions on stack operations, queue operations, linked list reversal and expression conversions. Detailed step-by-step explanations are provided for stack, queue and linked list operations based on the given inputs.

Uploaded by

karimahmad.wis
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Exam 2: Data Structures and Algorithm

Question 1:
Show the contents of a stack for the following sequences of operations: push(A), push(B), push(C),
pop(), push(D), push(E), pop(), push(F), push(G). Assume an initial size of 6 for the array
implementation. You also need to indicate the value of top at the end.

Ans 1.
Initially stack is empty so top = -1.

First operation is push(A), this operation push element A at the top of the stack
as:

top = top + 1 = -1 + 1 = 0

stack[0] = A

Second operation is push(B), this operation push element B at the top of the
stack as:

top = top + 1 = 0 + 1 = 1

stack[1] = B

Third operation is push(C), this operation push element C at the top of the stack
as:

top = top + 1 = 1 + 1 = 2

stack[2] = C

C
B

Fourth operation is pop(), this operation pop the top element from the array as:

popped_item = stack[top] = stack[2] = C

top = top - 1 = 2 - 1 = 1

Fifth operation is push(D), this operation push element D at the top of the stack
as:

top = top + 1 = 1 + 1 = 2

stack[2] = D

Sixth operation is push(E), this operation push element E at the top of the stack
as:

top = top + 1 = 2 + 1 = 3

stack[3] = E

D
B

Seventh operation is pop(), this operation pop the top element from the array as:

popped_item = stack[top] = stack[3] = E

top = top - 1 = 3 - 1 = 2

Eighth operation is push(F), this operation push element F at the top of the stack
as:

top = top + 1 = 2 + 1 = 3

stack[3] = F

Nineth operation is push(G), this operation push element G at the top of the
stack as:

top = top + 1 = 3 + 1 = 4

stack[4] = G

G
F

The final value of top is 4.

Question 2:
Assume that stack s has stack type as int, and stack-capacity = 6. Give the
contents of top & all arrays. Data members of s after the code segment is
executed:

stack s;

s.push (123);

s.push (456);

s.pop();

s.push(789);

s.pop();
Question 3:
Assume that stack s has stack type as int, and stack-capacity = 6. Give the
contents of top & all arrays. Data members of s after the code segment is
executed:

stack s;

s.push (101);

int i = s.top();

s.push (218);

s.pop();

s.push (2*4+i);
Question 4:
Assume that stack s has stack type as int, and stack-capacity = 6. Give the
contents of top & all arrays. Data members of s after the code segment is
executed:

stack s;

for (int i = 0; i<=5; i++)

s.push( 2*i+3);

s.pop();

s.pop();

s.push(i*3 + 3);
Question 5:
Convert the following arithmetic expressions from infix to postfix & prefix:

((a * b – c / d * e) + f % g * (h * k))

((a * ~b – c + d – (~e / f * g * h % i))

Question 6:
Show the contents of a circular queue for the following sequences of operations: enqueue(A),
enqueue(B), enqueue(C), dequeue(), enqueue(D), enqueue(E), dequeue(), enqueue(F), enqueue(G).
Assume an initial size of 6 for the array implementation.

Ans 2:

Intially circular queue is empty so front = -1 & rear = -1.

First operation is enqueue(A), Initally queue is empty so this operation enqueue


element A at the first position of the circular queue and first front & rear both
increased by 1 as:

front = front + 1 = -1 + 1 = 0

rear = rear + 1 = -1 + 1 = 0

cqueue[0] = A

Second operation is enqueue(B), this operation enqueue element B at the rear


end of the circular queue as:

front = 0

rear = rear + 1 = 0 + 1 = 1

cqueue[1] = B

A B

Third operation is enqueue(C), this operation enqueue element C at the rear end
of the circular queue as:
front = 0

rear = rear + 1 = 1 + 1 = 2

cqueue[2] = C

A B C

Fourth operation is dequeue(), this operation dequeue element from the front end
of the circular queue as:

dequeued_element = cqueue[0] = A

front = front + 1 = 0 + 1 = 1

B C

Fifth operation is enqueue(D), this operation enqueue element D at the rear end
of the circular queue as:

front = 1

rear = rear + 1 = 2 + 1 = 3

cqueue[3] = D

B C D

Fifth operation is enqueue(E), this operation enqueue element E at the rear end
of the circular queue as:

front = 1

rear = rear + 1 = 3 + 1 = 4

cqueue[4] = E

B C D E

Sixth operation is dequeue(), this operation dequeue element from the front end
of the circular queue as:

dequeued_element = cqueue[1] = B

front = front + 1 = 1 + 1 = 2
C D E

Seventh operation is enqueue(F), this operation enqueue element F at the rear


end of the circular queue as:

front = 2

rear = rear + 1 = 4 + 1 = 5

cqueue[5] = E

C D E F

Eighth operation is enqueue(G), this operation enqueue element G at the rear


end of the circular queue. In this case rear is at the upper bound of the array so
now set the rear = 0 to enqueue element:

front = 2

rear = 0

cqueue[0] = G

G C D E F

Question 7:
Write a C++ code (not a full program) to reverse a linked list. For example, if the list is {4,5,6},then after
reversing, it becomes {6,5,4}. (Hint: you can use a stack)

C++ code to reverse the linked list is :

// Function to reverse linked list


Node *reverseList(Node* head)
{
// Stack to store elements of list
// Use Standard template library to create Stack data structure
stack<Node *> stk;

// Push the elements of list into stack


// Initalize ptr to head
Node* ptr = head;
// Loop until ptr is not NULL
while (ptr->next != NULL)
{
// Push ptr into stack
stk.push(ptr);
// Go to next node
ptr = ptr->next;
}

// Pop from stack and replace current nodes value


// Store ptr into head
head = ptr;
// Loop until stack in not empty
while (!stk.empty())
{
// Store stack top into next of ptr
ptr->next = stk.top();
// Go to next node
ptr = ptr->next;
// Pop the stack
stk.pop();
}

// Put NULL at the next pointer filed of the last node


ptr->next = NULL;

// Return the head of reversed linked list


return head;
}

You might also like