LINKED LISTS Midwestern State University CMPS 1053 Dr. Ranette Halverson 1.

26
LINKED LISTS Midwestern State University CMPS 1053 Dr. Ranette Halverson 1

description

Traverse a Linked List (print) // Print each value of the list Temp = Head; while (Temp != NULL) { cout Num; Temp = Temp -> Next; } 3

Transcript of LINKED LISTS Midwestern State University CMPS 1053 Dr. Ranette Halverson 1.

Page 1: LINKED LISTS Midwestern State University CMPS 1053 Dr. Ranette Halverson 1.

1

LINKED LISTSMidwestern State UniversityCMPS 1053Dr. Ranette Halverson

Page 2: LINKED LISTS Midwestern State University CMPS 1053 Dr. Ranette Halverson 1.

2

Basic LL Nodestruct Node{ int Num;

Node * Next;}Node *Head, *Temp;

Num Next

Page 3: LINKED LISTS Midwestern State University CMPS 1053 Dr. Ranette Halverson 1.

3

Traverse a Linked List (print)

// Print each value of the listTemp = Head;while (Temp != NULL){ cout << Temp -> Num;

Temp = Temp -> Next;}

Page 4: LINKED LISTS Midwestern State University CMPS 1053 Dr. Ranette Halverson 1.

4

Insert Node to Front of Listif (Head == Null) // empty list{ Head = new Node;

Head -> Num = VAL;Head -> Next = NULL; }

else // not empty list{ Temp = new Node;

Temp -> Num = VAL;Temp -> Next = Head;Head = Temp; }

Page 5: LINKED LISTS Midwestern State University CMPS 1053 Dr. Ranette Halverson 1.

5

Insert Node to Front of List - Revisited

Could we eliminate the first case (Head == NULL)?

That is, does the second case work with an empty or non-empty list?

Page 6: LINKED LISTS Midwestern State University CMPS 1053 Dr. Ranette Halverson 1.

6

Insert Node to Front of Listif (Head == Null) // Can we eliminate this case?{ Head = new Node;

Head -> Num = VAL;Head -> Next = NULL; }

else // Test this for empty list: Head == Null{ Temp = new Node;

Temp -> Num = VAL;Temp -> Next = Head;Head = Temp; }

Page 7: LINKED LISTS Midwestern State University CMPS 1053 Dr. Ranette Halverson 1.

7

Remove Front Node From List

if (Head != NULL){ Temp = Head;

Head = Head -> Next; delete Temp;Temp = NULL;

}

Page 8: LINKED LISTS Midwestern State University CMPS 1053 Dr. Ranette Halverson 1.

8

Insert Node to End of ListNode *P, *C; // for previous & currentif (Head == NULL) { } //Use insert to front code;else {P = Head; C=Head -> Next; While (C != NULL) // find end of list

{ P = C; C = C -> Next; } P -> Next = new Node; P -> Next -> Num = VAL; P -> Next -> Next = Null;}

Page 9: LINKED LISTS Midwestern State University CMPS 1053 Dr. Ranette Halverson 1.

9

Remove Last Node in Listif (Head != NULL) // empty listelse if (Head -> Next == NULL) //one Node in list{ delete Head; Nead = Null; } else // find end of list having at least 2 Nodes{ P = Head; C = Head -> Next; While (C -> Next != NULL) // C will point to last Node

{ P = C; C = C -> Next; } delete C; C = Null; P -> Next = NULL; }

Page 10: LINKED LISTS Midwestern State University CMPS 1053 Dr. Ranette Halverson 1.

10

Insert Node (Val) to Ordered List (ascending)

Temp = new Node; Temp -> Num = Val;//Case 1: Empty list – use code to insert to frontif (Head == NULL) { } //Case 2: Val is smaller than first Node – insert to frontelse if (Val <= Head -> Num) { } //Case 3: Find correct location within the listelse { // need to write this code }//Considerations: Search list comparing Val to Nodes already in list AND watching for end of list (NULL)

Page 11: LINKED LISTS Midwestern State University CMPS 1053 Dr. Ranette Halverson 1.

11

Insert Node in Ordered List (p.2)

//Case 3: Find correct location within the list{ P = NULL; C = Head; while (C -> Next != Null && C-> Num < Val) //move down list { P = C; C = C -> Next;}// found location – don’t know which condition stopped the loop if (C -> Num >= Val) // Insert between P & C { Temp -> Next = C; P -> Next = Temp; } else // Insert after C { C -> Next = Temp; Temp->Next = NULL; }

Page 12: LINKED LISTS Midwestern State University CMPS 1053 Dr. Ranette Halverson 1.

12

Remove Node (Val) from Linked List

if (Head == Null) { return false; } // empty listelse if (Head ->Num == Val) // delete first node { Temp = Head; Head = Head -> Next; delete Temp; Temp = Null; return true; }

Page 13: LINKED LISTS Midwestern State University CMPS 1053 Dr. Ranette Halverson 1.

13

Remove Node (Val) from Linked Listelse // must find val in list { P=Null; C = Head; while (C -> Next != Null && C-> Num < Val) { P = C; C = C -> Next;} if ( C -> Num == Val) // remove node { P -> Next = C -> Next;

delete C; C = Null; return true; } else { return false; } // val not in list }

Page 14: LINKED LISTS Midwestern State University CMPS 1053 Dr. Ranette Halverson 1.

14

Linked List Classstruct Node{ int Num;

Node * Next;}class LinkList{ Node *Head; public: // put functions here}

Page 15: LINKED LISTS Midwestern State University CMPS 1053 Dr. Ranette Halverson 1.

15

Constructor

LinkList ( ){ Head = NULL;}

Page 16: LINKED LISTS Midwestern State University CMPS 1053 Dr. Ranette Halverson 1.

16

Insert to Front Functionvoid InsertFront (int Val){ Node * Temp; if (Head == Null) // empty list { Head = new Node;

Head -> Num = VAL;Head -> Next = NULL; }

else // not empty list { Temp = new Node;

Temp -> Num = VAL;Temp -> Next = Head;Head = Temp; }

}

Page 17: LINKED LISTS Midwestern State University CMPS 1053 Dr. Ranette Halverson 1.

17

Remove From Front of Listbool RemoveFront (int &Val){ if (Head == Null) // Empty list

return false; else if (Head != NULL) { Val = Head -> Num;

Temp = Head;Head = Head -> Next; delete Temp;Temp = NULL;return true;

}}

Page 18: LINKED LISTS Midwestern State University CMPS 1053 Dr. Ranette Halverson 1.

18

Calls to Insert & Removeint main ( ){ LinkList MyList ( );

int X = 5, Y = 10, Z; bool B;MyList.InsertFront(X);MyList.InsertFront(Y);B = MyList.RemoveFront (Z);if (B) cout << Z; // only print if actually removed

MyList.PrintList ( );}

Page 19: LINKED LISTS Midwestern State University CMPS 1053 Dr. Ranette Halverson 1.

19

NOW, YOU DEVELOP YOUR OWN LINKED LIST CLASS USING WHAT WE HAVE DONE IN CLASS!!!

Test with your own data. Test all functions thoroughly!!!

Page 20: LINKED LISTS Midwestern State University CMPS 1053 Dr. Ranette Halverson 1.

20

Linked List Application

Consider a linked list consisting of a person’s last & first names & age, ordered by age.• Modify the struct Node• What function do you call to insert a new person to the list?

• How does the code in other member functions change?

Page 21: LINKED LISTS Midwestern State University CMPS 1053 Dr. Ranette Halverson 1.

21

New structstruct Node{ string Lname;

string Fname;int Age; // NumNode * Next;

}• Does the order of the attributes matter?• If we use Num (instead of Age) will have less code changes.

Page 22: LINKED LISTS Midwestern State University CMPS 1053 Dr. Ranette Halverson 1.

22

Code Changes???

•Every place that Num in a node is assigned, also need to assign last name & first name.

Page 23: LINKED LISTS Midwestern State University CMPS 1053 Dr. Ranette Halverson 1.

23

Constructor – any changes needed?

LinkList ( ){ Head = NULL;}

Page 24: LINKED LISTS Midwestern State University CMPS 1053 Dr. Ranette Halverson 1.

24

Traverse to Print

//Print each value of the listTemp = Head;while (Temp != NULL){ cout << Temp-> Lname << Temp-> Fname <<Temp -> Num << endl;

Temp = Temp -> Next;}

Page 25: LINKED LISTS Midwestern State University CMPS 1053 Dr. Ranette Halverson 1.

25

Insert Ordered – (Val, LN, FN)Temp = new Node; Temp -> Num = Val; Temp -> Lname = LN; Temp -> Fname = FN;if (Head == NULL) { // insert to front code here } else if (Val <= Head -> Num) { // insert to front code here } else{ P = NULL; C = Head; while (C -> Next != Null && C-> Num < Val) //move down list { P = C; C = C -> Next;} if (C -> Num >= Val) //insert between P & C { Temp -> Next = C; P -> Next = Temp; } else // Insert after C { C -> Next = Temp; Temp->Next = NULL }}

Page 26: LINKED LISTS Midwestern State University CMPS 1053 Dr. Ranette Halverson 1.

26

Question?

•What happens if there is a “Tie” in the ages? That is, if 2 people have the same age which one is first in the list?