Major 1 - Llinked Lists and Queues - Type of Topics and Questions
Major 1 - Llinked Lists and Queues - Type of Topics and Questions
( B ) Some coding tasks with linked list (Mostly fill in the blanks).
Questions like below would be partially filled up for you (You would have to do
something like fill in the blanks, but also may need to write two or three lines of
code in a certain part of the function).
Q) Write a function, removeThird(), that removes the third node in a linked list.
You have ‘front’. You can assume that the linked list has more than 3 nodes, so
that you don’t have to consider any exceptional cases.
Q) Suppose you are given a linked list where nodes are storing integers. Write a
function removeNode(int x), that removes the node storing x. For instance,
removeNode(17), would remove the node storing 17. (No need to consider
exceptional cases).
Q) Suppose you are given a linked list where nodes are storing integers. Write a
function removeAfter(int x), that removes the node immediately after the node x.
(No need to consider exceptional cases).
Q) Write code to return the element at index t of the linked list (Already there in
the slides).
( C ) Some questions without code (testing your understanding and O
notation):
(Q) Why do we need a tail in a tailed linked list?
(Q) What are the advantages/disadvantages of a linked list compared to an
array?
(Q) Suppose we have a tailed linked list. Suppos we write a function to remove
the third node. Do you think this code would run in time O(1) or O(n)? Why?
(Q) Do you think the code to remove the 2 nd last node from the linked list run in
time O(1) or O(n) with a tailed linked list?
( D ) I can give a node class, or a linked list class etc., and ask you to
rewrite it to make it generic.
( E ) Queues
Remember, queues are first in first out.
(Q) Suppose we have made a Queue2 class, which is a queue and has
implemented the enqueue, dequeue and empty functions. What will be printed
by the following lines of code.
(Btw, I can give a similar question, but replacing queue with a stack).
(Q) Suppose we have implemented a queue (with the enqueue and dequeue
function). Write a function to delete the middle item (using only enqueue and
dequeue). (Again, the function would be partially filled up for you, and you would
have to write the missing code).
(Q) Suppose we are given the following list of number: 1, 5, 8, 9, 2, 17, 8. Let’s
say we have stored these numbers in a queue. If we are enqueuing at the end
where ‘8’ is stored, what would the list look like after dequeuing twice?