0% found this document useful (0 votes)
6 views3 pages

Major 1 - Llinked Lists and Queues - Type of Topics and Questions

The document discusses coding tasks related to linked lists and queues, including modifying linked lists and writing functions to remove specific nodes. It also poses theoretical questions about the advantages of linked lists over arrays and the time complexity of certain operations. Additionally, it mentions implementing generic classes for nodes and linked lists, as well as operations on queues.

Uploaded by

fahoodies
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)
6 views3 pages

Major 1 - Llinked Lists and Queues - Type of Topics and Questions

The document discusses coding tasks related to linked lists and queues, including modifying linked lists and writing functions to remove specific nodes. It also poses theoretical questions about the advantages of linked lists over arrays and the time complexity of certain operations. Additionally, it mentions implementing generic classes for nodes and linked lists, as well as operations on queues.

Uploaded by

fahoodies
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/ 3

Topic 3: Linked List and Queues

( A ) Write code to change from one state to another


Write a few lines of code to change the linked list looking like the first figure
below, to the linked list looking like the 2 nd figure below.

Ans: As we only have front available to us, we can do the following

Node nd=front . next ;


front . next=nd . next ;
nd . next=nd ;
(By the way, note that lines 2 and 3 cannot be swapped, else we will lose all
reference to the node with 5).
(Sometime you can do the question without introducing a new node variable nd.
But here you should introduce a new variable. You can make a few figures on
your own and practice similar questions too).

( 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?

You might also like