• Fundamental storage structures used in Computer Science

• There are 2 fundamentally different data structures used in Computer Science

• Arrays:

• An array = a collection of identical objects that are stored consecutively in memory:

• Access elements in an array:

• The computer must record the location of the first element of the array

• The computer must alos record the size of an array element

• The ith-element of the array is located at:

 ``` start-address + i × size-of-array-elem ```

• An linked data structure = a collection of identical objects that are stored non-consecutively in memory

• There are many ways to organized the linked data structure

Some common forms:

 List = a linear (single way) linked structure Tree = an acyclic (= no cycles) multi-way linked structure Graph = an general (= possibly with cycles) multi-way linked structure

• CS171:

 We will study the linked list now Later, we will study the binary tree If time permits, we will study graphs too.

• What is a linked list:

• Linked list = a linear (single-way) linked data structure used to store information of similar format where:

• The size of list can change easily

• The elements of the list are stored in sequence

• The location of the i+1th-element in the list is stored inside the ith-element of the list

 I.e.: we must consult the information stored in the ith-element of the list to find the i+1th-element of the list !!!

• The user only know the location of the first element of the list

• Schematically:

Note:

• The programmer only has access to the the following variable tagged as:

 ``` user variable This variable is often named: head ```

The variable head contains a reference (= address) of a list element

 The programmer (user) can find the first list element through this user variable head

• A list element will store some useful information, but it also contains one (and sometimes more) variable that contains:

 The reference (= address) of the next list element

• All list elements must be chained in sequence

 The only way to find all list elements is to traverse the chain ---- element by element !!!

• The last list element must contain the null value to indicate that there are no more list elements

• How a linked list is stored inside the computer memory

• The following memory content represents a linked list of 3 list elements:

Explanation:

• Through the variable head, the programmer can find:

 Data stored in the first list element data1 A reference to the second list element

• Through the reference stored in the first list element, the programmer can find:

 Data stored in the second list element data2 A reference to the third list element

• And so on

• Comparing arrays and linked structures

• Flexibility

 User can form extremely complex structures using links

• Efficiency:

• Every element in a linked structure contains meaningful data

 An array may contain some empty elements that do not store meaningful data

• Linked structures are often used for organizing information used by the program

• Speed

 User can access an arbitrary array element a[i] immediately (In contrast, the user must navigate (traverse) a linked structure --- this may require the user to access many elements before reaching the desired one.)

Arrays are excellent data structure for sorting algorithms

• The empty list

• Recall: a list with 3 nodes:

• To represent an empty list (= a list with no nodes, we set:

 ``` head = null; ```

Graphically:

• The node structure of a linked element

• Node:

• Node = one element of a linked data structure

Graphically:

• The structure of an element of a linked structure is defined as follows:

 ``` public class Node { /* ================================== Variables section ================================== */ variables used to store information Node next; // Reference variable used to link nodes together /* ================================== Methods section ================================== */ Methods used to manipulate information } ```

Example:

 ``` public class Node { private int SSN; // Variables used to store info. on a person private String name; private String address; private Node next; // The link var. is commonly called "next" in a list } ```

• Dictactical note:

• Regardless of the information contained in a node, the procedure to maintain the linkage between different nodes are identical

 The gist of these lectures are to teach you how to maintain the linkage between different nodes

• Therefore:

• I will use the follow very simple data fields in a node to the lectures:

 ``` public class Node { public double value; // Variables used to store info public Node next; // The linkage var /* ========================== Constructor ========================== */ public Node( double x ) { value = x; next = null; // It's good idea to set it to null } } ```

• A link from some node x to another node y created by:

 Assigning the address of node y to the reference variable next in the node x

• Example:

• Create 2 nodes:

 ``` Node x = new Node( 4 ); Node y = new Node( 9 ); ```

Grpahically:

• How to make a link from node x to node y:

 ``` x.next = y; // y = address of Node object ```

Result:

• The class that define a linked list

• Name of the class that defines a linked list:

 We will call the class: List

• A class has

 variables            methods

(The class that defines a linked list is no different from other classes

Do not get intimidated by the linked list (or any other fancy data structures) --- keep applying what you have learned)

• Variables in the List class:

• Recall that:

Therefore:

 The type of the variable head is: Node

The variables in the List class:

 ``` public class List { Node head; // Head is used to access the nodes // in the linked list // The variable head is the only necessary variable. // Depending on other features you want to add, // you can add more variables // E.g: if you want to know the number of nodes // in the list, you can add a "count" variable // But you will need to keep all variable uptodate !!! } ```

• Methods in the List class:

• Constructor:

 ``` public class List { Node head; // Head is used to access the nodes // in the linked list /* ================================================ Constructor: initialize head to represent an empty list ================================================= */ public List() { head = null; // null means: no valid nodes } } ```

• Other useful methods

 put(x): add/insert a new node to the linked list that contains the value x remove(): delete a node from the linked list toString(): return a string containing the values in all nodes in the linked list Method used to find some values in the linked list Etc., etc.

• So the List class will look like this:

 ``` public class List { Node head; // Head is used to access the nodes // in the linked list /* ================================================ Constructor: initialize head to represent an empty list ================================================= */ public List() { head = null; // null means: no valid nodes } public void put( double x ) { // insert a Node that contains the value x into // the list that starts at "head" } public ... remove( ) { // Delete SOME element from the list // (that starts at "head" // -- There are many "delete policies" .... } public String toString() { // This method will return a string representing // ALL values stored in the Nodes in the list } } ```