Linked Lists LJ A linked list is an ordered collection of data in which each element contains the location of the next element Element = Data + Link head data link XI empty
Trang 1
đề
s
Faculty of Computer Science and Engineering
BK TP.HCM 2
Trang 2OA pointer usage pir
printf(“Data in node: %dqd”, ptr->data); `
Trang 3Pointer in C++
_} Be careful in these cases:
Trang 4
Parameter Passing Techniques
void func(int* a, int* b){ void main() {
Trang 5Parameter Passing Techniques
void func(int* &a, int* b){ void main() {
Trang 6Parameter Passing Techniques
void func(int **a, int **b){ void main() {
Trang 7Linked Lists
LJ A linked list is an ordered collection of data in which each element contains the location of the next element
Element = Data + Link
head data link
XI
empty
linked list
Faculty of Computer Science and Engineering —- HOMUT Slide 7
Trang 8Nodes
A node with one data field
Trang 9Nodes
count <integer> data <dataType> key <keyT ype>
head <pointer> link <pointer> field1 < >
end list end node field2 < >
Trang 10Nodes — Implementation in C++
Trang 12Nodes — Implementation in C++
Trang 15Linked List — Implementation in C++
template <class List_ItemType>
list
Trang 16Linked List Algorithms
Trang 17Linked List Implementation
template <class List_ItemType>
Node<List_ItemType>* ploc),;
&pPre, Node<List_ItemType>* &pLoc);
Trang 18Linked List Implementation
template <class List_ItemType>
class LinkedList {
public:
~LinkedList();
void InserthLast (List_ItemType value);
int InsertItem(List_ItemType value, int Position);
List_ItemType DeleteFirst();
List_ItemType DeleteLast();
int DeleteItem(int Postion);
Trang 19Linked List Implementation
_] How to use Linked List data structure’
Trang 20Linked List Implementation
Trang 22Create List
Algorithm createList (ref list <metadata>)
Initializes metadata for a linked list
Pre list is a metadata structure passed by reference
Post metadata initialized
1 list.head = null
2 list.count = 0
3 return
Trang 23Linked List Implementation
template <class List_ItemType>
Trang 24Insert Node
_] Allocate memory for the new node and set up data
_] Point the new node to its successor
_] Point the new node's predecessor to It
Trang 25Insert into Empty List
Trang 26Insert at the Beginning
pNew -> link = list.head
list head = pNew
Trang 28Insert at End
pPre -> link = pNew
4 > 39 > 52 > 715 ⁄
list _l ›| 134 Nx]
pPre pNew
Trang 29Insert Node Algorithm
Algorithm insertNode (ref list <metadata>,
val pPre <node pointer>,
val dataln <datalype>)
Inserts data into a new node in the linked list
Pre list is metadata structure to a valid list
oPre Is pointer data's logical predecessor dataln contains data to be inserted
Post data have been inserted in sequence
Return true if successful, false if memory overflow
Faculty of Computer Science and Engineering —- HOMUT Slide 29
Trang 30if (oPre = null) Adding before first node or to empty list
1 pNew -> link = list.head
2 list.head = pNew else
Adding in middle or at end
1 pNew -> link = pPre -> link
2 pPre -> link = pNew list.count = list.count + 1
return true
End _ insertNode
Trang 31Insert Node
template <class List_ItemType>
Node<List_ItemType> *pNew = new Node<List_ItemType>();
_ II II MIIHII HH NHI gaiaaạaỤ
Faculty of Computer Science and Engineering — HOCMUT Slide 31
Trang 32Delete Node
_] Locate the node to be deleted
_] Point the node predecessor's link to its Successor
_] Release the memory for the deleted node
Trang 33Delete First Node
Trang 34General Delete Case
Trang 35Delete Node Algorithm
Algorithm deleteNode (ref list <metadata>,
val pPre <node pointer>,
val pLoc <node pointer>
ref dataOut <dataType>) Delete data from a linked list and returns it to calling
module
Pre list is metadata structure to a valid list
oPre is a pointer to predecessor node opLoc is a pointer to node to be deleted dataOut is variable to receive deleted data
Post data have been deleted and returned to caller
Faculty of Computer Science and Engineering — HOCMUT Slide 35
Trang 36Delete Node Algorithm
1 dataOut = pLoc -> data
2 if (pPre = null)
Delete first node
1 list.nead = pLoc -> link
3 else
Delete other nodes
1 pPre -> link = pLoc -> link
4 list.count = list.count - 1
5 recycle (pLoc)
6 return
Trang 37Delete Node
template <class List_ItemType>
List_ItemType LinkedList<List_ItemType>: :DeleteNode (
Node<List_ItemType> *pPre, Node<List_ItemType> *ploc) {
Trang 38Traverse List
_] Traverse module controls the loop:
calling a user-supplied algorithm to process data
oWalker = list.head
loop (oWalker not null)
process (pWalker -> data}
oWalker = pWalker -> link
Trang 40
Searching in Linked List
template <class List_ItemType>
Trang 41Destroy List Algorithm
Algorithm desiroyList (val list <metadata>)
Deletes all data in list
Pre list is metadata structure to a valid list
Post all data deleted
1 loop (list.head not null}
Trang 43int InsertLast (List_ItemType value) ;
int InsertItem(List_ItemType value, int Position);
List_ItemType DeleteFirst();
List_ItemType DeleteLast();
int DeleteItem(int Postion);
Trang 44Pointer vs Object Variable
Trang 45Pointer vs Object Variable
_Ì What are the pros and cons?
Faculty of Computer Science and Engineering — HOCMUT Slide 45
Trang 46sample Solution: Insert
template <class List_ItemType>
return O;
Node<List_ItemType>* newPtr, *pPre;
newPtr = new Node<List_ItemType>();
Trang 47sample Solution: Insert
Trang 48sample Solution: Delete
template <class List_ItemType>
1f (position < O || position > this->count)
Trang 49sample Solution: Clone
template <class List_ItemType>
LinkedList<List_ItemType>*
LinkedList<List_ItemType>::Clone() { LinkedList<List_ItemType>* result =
New LinkedList<List_ItemType>(); Node<List_ItemType>* p = this-—>head;
Trang 50Homework
_Ì Reverse a linked list
head +
a ! b || a >| b LS |
Result:
A