LINKED LISTS Midwestern State University CMPS 1053

advertisement
1
LINKED
LISTS
Midwestern State University
CMPS 1053
Dr. Ranette Halverson
2
Basic LL Node
struct Node
{
int Num;
Node * Next;
}
Node *Head, *Temp;
Num
Next
3
Traverse a Linked List (print)
// Print each value of the list
Temp = Head;
while (Temp != NULL)
{
cout << Temp -> Num;
Temp = Temp -> Next;
}
4
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; }
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?
6
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; }
7
Remove Front Node From List
if (Head != NULL)
{
Temp = Head;
Head = Head -> Next;
delete Temp;
Temp = NULL;
}
8
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;
}
9
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;
}
10
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)
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; }
12
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;
}
13
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
}
14
Linked List Class
struct Node
{
int Num;
Node * Next;
}
class LinkList
{ Node *Head;
public:
// put functions here
}
15
Constructor
LinkList ( )
{ Head = NULL;}
16
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; }
}
17
Remove From Front of List
bool 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;
}
}
18
Calls to Insert & Remove
int 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 ( );
}
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!!!
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?
21
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.
22
Code Changes???
• Every place that Num in a node is
assigned, also need to assign last
name & first name.
23
Constructor – any changes needed?
LinkList ( )
{ Head = NULL;}
24
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;
}
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 }
}
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?
Download