Computer Science

Linear Data Structures

Linked Lists - Intermediate

Doubly linked lists are useful for more than just inserting and removing elements from the head and tail of the list. They can maintain a list of elements that allows for insertion and removal to the interior of the list

Given a node v v of a doubly linked list (which is currently followed by w w ), we want to insert a new node z z immediately after v v . Concretely, we want to write a functioninsert(v)which takes a node, v v , and inserts a new node after v v . The result should be a list where v v 'snextpoints to z z , z z 'snextpoints to w w , w w 'sprevpoints to z z , and z z 'sprevpoints to v v .

If we execute these events in the wrong order, we can erase crucial information that will break the list. How should the following steps be ordered so that they correctly insert a node in-the middle of a doubly linked list?

  • AMake z z 'sprevpoint to v v .
  • BMake v v 'snextpoint to z z .
  • CMake w w 'sprevpoint to z z .
  • DMake z z 'snextpoint to w w .

Consider a circularly linked list. Which of the following methods below removes the node after the cursor? The cursor is a special node which allows us to have a place to start from if we ever need to traverse a circularly linked list.

1 2 3 4 5 6 7 8 9 10 11
nodeA(nodeN){NodeoldNode=cursor.getNext();if(oldNode==cursor)cursor=node.getNext()else{cursor.setPrev(oldNode.getNext());oldNode.setNext(null);}size--;returnoldNode;}

1 2 3 4 5
nodeB(NodeN){NodeoldNode=NoldNode.setNext(null)returnoldNode;}

1 2 3 4 5 6 7 8 9 10 11
nodeC(NodeN){NodeoldNode=cursor.getNext();if(oldNode==cursor)cursor=nullelse{cursor.setNext(oldNode.getNext());oldNode.setNext(null);}size--;returnoldNode;}

1 2 3 4 5 6 7 8 9 10 11
nodeD(NodeN){NodeoldNode=N;if(oldNode==cursor)cursor=nullelse{cursor.setNext(N);oldNode.setNext(null);}size--;returnoldNode;}

Given a singly linked list, write a program the shifts every node k k unit to the left.

For example

When4 \rightarrow 6 \rightarrow 8 \rightarrow 9is shifted 2 2 units left it becomes8 \rightarrow 9 \rightarrow 4 \rightarrow 6

What will the following linked list look like when each node is shifted 4432897427834 4432897427834 to the left.

34 \rightarrow 17 \rightarrow 17 \rightarrow 74 \rightarrow 83 \rightarrow 59 \rightarrow 39

×

Problem Loading...

Note Loading...

Set Loading...