260 likes | 271 Views
Linked Lists. Midwestern State University CMPS 1053 Dr. Ranette Halverson. Basic LL Node. struct Node { int Num ; Node * Next; } Node *Head, *Temp;. Num Next. Traverse a Linked List (print). // Print each value of the list Temp = Head; while (Temp != NULL)
E N D
Linked Lists Midwestern State University CMPS 1053 Dr. Ranette Halverson
Basic LL Node struct Node { intNum; Node * Next; } Node *Head, *Temp; Num Next
Traverse a Linked List (print) // Print each value of the list Temp = Head; while (Temp != NULL) { cout << Temp -> Num; Temp = Temp -> Next; }
Insert Node to Front of List 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; }
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?
Insert Node to Front of List if (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; }
Remove Front Node From List if (Head != NULL) { Temp = Head; Head = Head -> Next; delete Temp; Temp = NULL; }
Insert Node to End of List Node *P, *C; // for previous & current if (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; }
Remove Last Node in List if (Head != NULL) // empty list else 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; }
Insert Node (Val) to Ordered List (ascending) Temp = new Node; Temp -> Num = Val; //Case 1: Empty list – use code to insert to front if (Head == NULL) { } //Case 2: Val is smaller than first Node – insert to front else if (Val <= Head -> Num) { } //Case 3: Find correct location within the list else { // need to write this code } //Considerations: Search list comparing Val to Nodes already in list AND watching for end of list (NULL)
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; }
Remove Node (Val) from Linked List if (Head == Null) { return false; } // empty list else if (Head ->Num == Val) // delete first node { Temp = Head; Head = Head -> Next; delete Temp; Temp = Null; return true; }
Remove Node (Val) from Linked List else // 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 }
Linked List Class struct Node { intNum; Node * Next; } class LinkList { Node *Head; public: // put functions here }
Constructor LinkList ( ) { Head = NULL;}
Insert to Front Function void 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; } }
Remove From Front of List boolRemoveFront (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; } }
Calls to Insert & Remove int main ( ) { LinkListMyList ( ); 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 ( ); }
Now, You develop your own Linked List class using what we have done in class!!! Test with your own data. Test all functions thoroughly!!!
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?
New struct struct Node { string Lname; string Fname; int Age; // Num Node * Next; } • Does the order of the attributes matter? • If we use Num (instead of Age) will have less code changes.
Code Changes??? • Every place that Num in a node is assigned, also need to assign last name & first name.
Constructor – any changes needed? LinkList ( ) { Head = NULL;}
Traverse to Print //Print each value of the list Temp = Head; while (Temp != NULL) { cout<< Temp-> Lname << Temp-> Fname<<Temp -> Num << endl; Temp = Temp -> Next; }
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 } }
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?