Presentation is loading. Please wait.

Presentation is loading. Please wait.

Linked Lists A formal data structure. Linked Lists Collections of data items “lined up in a row” Inserts and deletes can be done anywhere in the list.

Similar presentations


Presentation on theme: "Linked Lists A formal data structure. Linked Lists Collections of data items “lined up in a row” Inserts and deletes can be done anywhere in the list."— Presentation transcript:

1 Linked Lists A formal data structure

2 Linked Lists Collections of data items “lined up in a row” Inserts and deletes can be done anywhere in the list

3 Self-referential Classes Contains a reference member (a link of some type) Reference member refers to an object of the same class type Necessary to create linked list (amongst others) Linked list consists of nodes Nodes consist of –Data –Link to next node A

4 Self-referential Classes Need “pointer” –Start pointer (so that we know where to start) –End pointer? (so that we know where to stop) –Null reference = end of “chain” The new operator –Allocates appropriate amount of memory –OutOfMemoryException

5 class Node { private int data; private Node next; public Node (int d) { //constructor body } public int Data { get { //get body } set { //set body } public Node Next { get { //get body } set { //set body }

6 The header & fields class Node { private int data; private Node next; Note the field type Node (this is what is meant by self-referential). It is a reference to another object of the SAME class 3 Data Node 4

7 Property Data public int Data { get { //get body } set { //set body } This does not need to be an int, it could be another class A Set (puts data INTO the node) Get (retrieves data FROM the node)

8 Property Next public Node Next { get { //get body } set { //set body } This has to be of type Node, otherwise it would not be self-referential. You can set this value in the constructor, but you must have a get and a set because you will probably need to change the value of Node (adding or deleting nodes from a list, queue, etc.) A Get (retrieves the next node or node position) Set (allows the next node to be positioned or inserted)

9 Linked Lists Collections of data items “lined up in a row” Inserts and deletes can be done anywhere in the list

10 Chain of Nodes Data component “Pointer” or Reference Component Last reference “points” or refers to a NULL

11 The List Class public class LinkedListClass { private StudentNode firstStudent; private StudentNode lastStudent; private string ListName; //a name for the list Linked list has a FIRST node and a LAST node (or start and stop) The name for the list is not necessary (just a bit of extra)

12 The Constructors public LinkedListClass(string n) { ListName = n; firstStudent = lastStudent = null; } //construct an empty list with "list" as its name public LinkedListClass(): this("list") { } Overloaded constructors – one with a list name entered by the user and one with list name “list” by default. Both create empty lists.

13 The Constructors A public LinkedListClass(string n) { ListName = n; firstStudent = lastStudent = null; } First nodeLast node

14 Property: First Node public StudentNode FirstStudent { get { return firstStudent; } Allows you to retrieve the position or the address of the first node This property only has a “get” because we don’t want anything to change this property – except via the operations of inserting and deleting

15 Property: Last Node public StudentNode LastStudent { get { return lastStudent; } Allows the retrieval of the last node What could happen if there was a “set” option with the first and last nodes?

16 Method: Insert at Front public void InsertAtFront(Student insertItem) { lock (this) { if (IsEmpty()) { firstStudent = new StudentNode(insertItem); lastStudent = new StudentNode(insertItem); } else firstStudent = new StudentNode(insertItem, firstStudent); } Inserting a node to the front of the list

17 Method: Insert at Front Inserting into an empty list A First nodeLast node A if (IsEmpty()) { firstStudent = new StudentNode(insertItem); lastStudent = new StudentNode(insertItem); }

18 Method: Insert at Front Inserting into a list that already has nodes B First node Last node ACA firstStudent = new StudentNode(insertItem, firstStudent);

19 Using Linked Lists Step 1: What data should the list store? Step 2: Any special requirements? –Ordered list? Step 3: Where to use the list? –Global or local? Step 4: Instantiate Step 5: Special methods (not inside the list class) –Find average –Find a particular node (specific data) –Find minimum/maximum –Insert a node into the list at the correct place –Remove a specific node (from anywhere in the list) –Sorting a list (perhaps inside list class?)

20 Special Methods Need a current or position node –Keep track of where you are “at the moment” Do not touch the first or last node pointers in the class unless inserting or deleting those nodes Hands Off!!


Download ppt "Linked Lists A formal data structure. Linked Lists Collections of data items “lined up in a row” Inserts and deletes can be done anywhere in the list."

Similar presentations


Ads by Google