Download presentation
Presentation is loading. Please wait.
1
Computer Science 2
2
Be able to find someone in a linked list
Learning Targets Be able to find someone in a linked list Be able to delete someone from a linked list. Be able to create a simple track database.
3
Type ptrtype = ^rectype; rectype = record data:string; phone:string; next:ptrtype; end; Warm-up With a partner, write a procedure for finding someone’s phone number that is in a linked list. procedure FindNode(top:ptrtype; person:string);
4
Finding a node pseudo code
procedure FindNode(top:ptrtype;person:integer) Search for a node with the value equal to person in the list. If such a node is found, show the value. procedure FindNode(top:ptrtype; person:string); Var currNode:ptrtype; begin currNode := top; while (currNode<>nil) and (currNode^.data<> person) do currNode := currNode^.next; end; if (currNode<>nil) then writeln (person, ‘ phone number is ’, currNode^.phone) else writeln(person, ‘ was not found in the list’); End;
5
Deleting a node Steps Like InsertNode, there are two special cases
DeleteNode(top:ptrtype; person:string) Delete a node with the value equal to person from the list. Steps Find the desirable node (similar to FindNode) Set the pointer of the predecessor of the found node to the successor of the found node Release the memory occupied by the found node Like InsertNode, there are two special cases Delete first node Delete the node in middle or at the end of the list
6
Deleting a node Pseudocode
Procedure DeleteNode(var top:ptrtype; person:string); Var prevNode, currNode:ptrtype; begin prevNode := nil; currNode := top; while (currNode<>nil) and (currNode^.data <> person) do prevNode := currNode; currNode := currNode^.next; end; if (currNode<>nil)then if (prevNode<> nil) then prevNode^.next := currNode^.next; dispose(currNode); end else top := currNode^.next; End; Try to find the node with its value equal to x
7
Deleting a node Procedure DeleteNode(var top:ptrtype; person:string);
prevNode, currNode:ptrtype; begin prevNode := nil; currNode := top; while (currNode<>nil) and (currNode^.data <> person) do prevNode := currNode; currNode := currNode^.next; end; if (currNode<>nil)then if (prevNode<> nil) then prevNode^.next := currNode^.next; dispose(currNode); end else top := currNode^.next; End; prevNode currNode
8
Deleting a node Procedure DeleteNode(var top:ptrtype; person:string);
prevNode, currNode:ptrtype; begin prevNode := nil; currNode := top; while (currNode<>nil) and (currNode^.data <> person) do prevNode := currNode; currNode := currNode^.next; end; if (currNode<>nil)then if (prevNode<> nil) then prevNode^.next := currNode^.next; dispose(currNode); end else top := currNode^.next; End; front currNode
9
Insertion Linked List Assignment #2
You will be creating a program for the running track events. (Note: Time will be kept in seconds) Name Event (Push: Let it support the participant running in up to 3 events.) Best Time
10
What it needs to do. Enter the information Find a participant
Insertion linked list by name Find a participant Show their event and best time or say they are not in the list Show all participants, events and times in order based on their name Delete a person from the list Pushes Save to a file (Linked list to file) Load from a file (File to linked list) Enter an event and show all participants in that event Enter and event and give lane assignments based on speed for an 8 lane track 4, 5, 3, 6, 2, 7, 1, 8
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.