Professor Abdul-Quader
Data Structures
null
.Look at the Node class in the starter code
Exercise: Create a “singly” linked list class. It should store a reference to the “head” node. First just implement the following operations:
addFirst
)Test out your class in a main method.
addFirst
method?size
method? Is there any way we can improve it?The implementation details of the class really do matter here. If you design the class well, you can support this operation in constant time. How?
In addition, if we design the class well, we can support the operation of adding to the end to the list in constant time. How?
Implement the following methods. They should run in \(O(1)\) time:
get
methodTo truly have a list structure, we should be able to access any element of the list. For example, if I add \(5, 10, 3, 7\) to my list, I should be able to call \(list.get(1)\) and it should return \(10\).
get
?get
in our linked list class? Pseudocode?In general, a structure in which we can access the \(i^{\text{th}}\) element in constant time is said to support random access.
A tree is a structure with a root node, so that all the nodes have links to child nodes, but there are no cycles. That is, you can’t have a situation where Node A has a child B, and B has a child A. A tree where every node has at most \(2\) children is called a binary tree.
(Binary tree with root 2)