Preview

04 - Linked Lists

 1. A linked list is exactly the same as an ordinary list - there is absolutely no difference.

  FALSE

  TRUE

 2. Linked lists are used to implement several other common abstract data types, including lists, stacks, queues, associative arrays, and S-expressions

  TRUE

  FALSE

 3. Read the following excerpt and see if you can fill in the blanks.
Linked lists were developed in 1955–1956 
by Allen Newell, Cliff Shaw and Herbert 
A. Simon at RAND Corporation as the 
primary data structure for their 
Information Processing Language. 

IPL was used by the authors to develop 
several early ________________________
programs, including the Logic Theory 
Machine, the General Problem Solver, 
and a computer chess program.
randcorporation.png

  nuclear weapons

  government global policing

  general purpose

  artificial intelligence

 4. Linked lists have elements or nodes in them. Each element (node) __________________________

  points to another node in the linked list

  ends the linked list

  is held in isolation

  is deleted upon retreival

 5. Each node in a ___________ has two parts: the node item itself, and a single "pointer". The pointer tells the program where the next associated node is located.

  singly linked list

  zero based linked list

  simple (or non linear) list

  doubly linked list

 6. A _______________will have two pointers. One will reference the location of the next node and the other points to the previous node

  singly linked list

  doubly linked list

  zero based linked list

  simple (or non linear) list

 7. In a doubly linked list if the _______ node is NULL, then that is the start of the list.

  duplicated

  final

  previous

  next

 8. if the _____________ pointer is NULL, then that would indicate the end of the list.

  next

  final

  previous

  duplicated

 9. In a linked list, it is the pointers and the links they make that makes addition and deletion from a list simple - and __________________________.

  the order is entirely changed, which is useful

  the order is not broken

  the order is deleted and created again at random

  there is no need for any order whatsoever

 10. If an item is added or removed from a linked list then_______________________________

  the item is first duplicated and appended on to the end of a second list

  everything has to be deleted and initialised from scratch

  the pointers of its neighbors are updated

  the list is duplicated, deleted and then copied

 11. The main benefit of a linked list over a conventional array is that the list elements can be easily inserted or removed without reorganization of the entire structure because ______________________
Note: You may want to look up and understand the term "contiguously"

  the data items can all be stored contiguously in memory or on disk

  the data items are all exactly the same size

  the data items are all smaller than 4 bits

  the data items need not be stored contiguously in memory or on disk

 12. Linked lists have several advantages over dynamic arrays. Fill in the blanks for this excerpt that explains an advantage.
Insertion or deletion of 
an element at a specific point of a list, 
assuming that we have indexed a pointer 
to the node (before the one to be removed,
 or before the insertion point) already, 
is a ____________________________ (otherwise 
without this reference it is O(n)), whereas
 insertion in a dynamic array at random 
locations will require moving half of the 
elements on average, and all the elements 
in the worst case. 

  linear-time operation

  constant-time operation

  non-linear time operation

  O(3)

 13. On the other hand, dynamic arrays (as well as fixed-size array data structures) allow constant-time random access, while linked lists allow only ________________to elements.

  completely random access

  access via the last node

  sequential access

  limited access

 14. Singly linked lists can be traversed in only one direction. This makes linked lists highly suitable for applications where it's useful to look up an element by its index quickly, such as heapsort.

  FALSE

  TRUE

 15. A disadvantage of linked lists is the __________________________which often makes them impractical for lists of small data items such as characters or boolean values

   extra storage needed for references,

  small amount of storage needed for references

  poor organisation and need for re-allocation of nodes

  excessive speed at which the nodes point to one another

 16. The following diagram shows that inserting a node before an existing one _______________________; instead, one must keep track of the previous node and insert a node after it.
addlinkedlistpicture.png

  can be done directly

  can be done by changing the index numbers and their orders

  cannot be done directly

  can be done instantly and with no need to change references or pointers

 17. In the following code, removeBeginning() sets list.firstNode to __________________________________
 function removeAfter(Node node) // remove node past this one
     obsoleteNode := node.next
     node.next := node.next.next
     destroy obsoleteNode
 function removeBeginning(List list) // remove first node
     obsoleteNode := list.firstNode
     list.firstNode := list.firstNode.next // point past deleted node
     destroy obsoleteNode

deletelinkedlistpicture.png

  point to the next node in the linked list

  become the last node in the list (e.g. with a referenece that points to the next available NULL pointer)

  ZERO when adding the last node to the linked list

  NULL when removing the last node in the list

 18. In the following linked list (python) implementation, what is happening on line 18?
class Node:
    def __init__(self, dataval=None):
        self.dataval = dataval
        self.nextval = None

class SLinkedList:
    def __init__(self):
        self.headval = None

list1 = SLinkedList()

list1.headval = Node("Mon")

a2 = Node("Tue")
a3 = Node("Wed")


list1.headval.nextval = a2


a2.nextval =a3

print(a2.dataval)
print(a3.dataval)

  We are linking the second node to the third node

  We are linking the third node to the second node

  We are linking all the nodes with the last node

  We are linking the first node to the second node

 19. In the following linked list (python) implementation, what is happening on line 21?
class Node:
    def __init__(self, dataval=None):
        self.dataval = dataval
        self.nextval = None

class SLinkedList:
    def __init__(self):
        self.headval = None

list1 = SLinkedList()

list1.headval = Node("Mon")

a2 = Node("Tue")
a3 = Node("Wed")


list1.headval.nextval = a2


a2.nextval =a3

print(a2.dataval)
print(a3.dataval)

  We are linking the third node to the second node

  We are deleting the last node and setting its value to NULL

  We are linking the second node to the third node

  We are creating a first node and deleting the rest of the list

 20. When the following code is executed, what would the output be?
class Node:
    def __init__(self, dataval=None):
        self.dataval = dataval
        self.nextval = None

class SLinkedList:
    def __init__(self):
        self.headval = None

# Print the linked list
    def listprint(self):
        printval = self.headval
        while printval is not None:
            print (printval.dataval)
            printval = printval.nextval
    def AtBegining(self,newdata):
        NewNode = Node(newdata)

# Update the new nodes next val to existing node
        NewNode.nextval = self.headval
        self.headval = NewNode

list = SLinkedList()
list.headval = Node("Mon")
e2 = Node("Tue")
e3 = Node("Wed")

list.headval.nextval = e2
e2.nextval = e3

list.AtBegining("Sun")

list.listprint()

  Sun

  Mon, Tues, Wed

  Sun, Mon, Tues, Wed

  Mon, Tues, Wed, Sun