Upload
holland
View
75
Download
1
Tags:
Embed Size (px)
DESCRIPTION
Linked List. Data Structures (TIB11) IT Department – Bunda Mulia University Teady Matius, M.Kom [email protected]. Objectives. To understand about linked list To understand about the operations of linked list To understand about the common variants of linked list. Pointer. - PowerPoint PPT Presentation
Citation preview
Linked List
Data Structures (TIB11)IT Department – Bunda Mulia University
Teady Matius, [email protected]
Objectives
• To understand about linked list• To understand about the operations of linked
list• To understand about the common variants of
linked list
Data Structures - Linked List 2
Pointer
• A pointer is basically used to represent the relation between two cells.
Example : Pointer A to B
Data Structures - Linked List 3
Pointer - example(Example from wikipedia)
• Pointer a pointing to variable b. Note that b stores a number, where a stores the address of b in memory (1462)
Data Structures - Linked List 4
Linked List
• A finite sequence of elementss1, s2, ....., sn
• Node : every recorded that contains information and linked to other node
• Linked List elements– Information– link: linked to other node
Data Structures - Linked List 5
Two important variables
• Head: contains the information of first node address pointer
• CurrentCell / PointerCell: contains the information of the current node address pointer that being accessed
Data Structures - Linked List 6
Remember!!!
• Head is the most important information to direct your linked list
• With the ‘Head’ you can go to the first node, and move toward to the destination node
• When you lose the ‘Head’ it means you lose your linked list too
• Never ever lose your ‘HEAD’!!!
Data Structures - Linked List 7
Single Linked List
Data Structures - Linked List 8
Linked List Operation
• Search / Locate• Insert– After the current cell– Before the current cell
• Delete
Data Structures - Linked List 9
Possible Operations
• At the front of list • At the middle of list• At the end of list
Data Structures - Linked List 10
Locate Operation
• Assign PointerCell as HeadPointerCell = Head;
• Move toward by directing the PointerCell to the next PointerCell until find the matched node.PointerCell = PointerCell->Next;
Data Structures - Linked List 11
Data Structures - Linked List 12
Insert Operation
• At the front of list Can be happen only at insert before current cell
• At the end of listCan be happen only at insert after current cell
• At the middle of list
Data Structures - Linked List 13
Insert at the front
• Make new node• Fill information at the new node• direct next link to the head node• Set head pointer to the new node
Insert at the front (cont.)
Data Structures - Linked List 15
Insert at the middle - after current cell
• Create new node• Fill information to the new node• Copy next link current node to the next link
new node• Set next link at the current node to the new
node
Insert at the middle - after current cell (cont.)
Data Structures - Linked List 17
Insert at the middle - before current cell
Note: You need to get the previous node address first!!!
• Create new node• Fill information to the new node• Locating previous node• Copy next link previous node to the next link
new node• Set next link at the previous node to the new
node
Insert at the middle - before current cell (cont.)
Data Structures - Linked List 19
Locating previous nextCan be done in many ways• Save the previous node when locating the current node
PreviousNode = CurrentNode;CurrentNode = CurrentNode->Next;
• Retrieve when neededRetrieveNode = HeadNode;While (RetrieveNode-> != CurrentNode){
RetrieveNode = RetrieveNode->Next;}PreviousNode = RetrieveNode;
• Use double list; directing previous node with previous link pointer.
Insert at the end
• Create new node• Fill the information at the new node• Set next node new node as NULL• Directing next link at the last node or tail to
the new node
Insert at the end (cont.)
Data Structures - Linked List 22
Delete Operation• At the front – delete head (REMEMBER: don’t until lose the head!)• At the middle• At the end – delete tail
Data Structures - Linked List 23
Delete Head (1st trick)
Data Structures - Linked List 24
Delete Head (2nd trick)
Delete Head (3rd trick)
Delete Middle
Data Structures - Linked List 27
Delete Tail
Data Structures - Linked List 28
Common Variants of Linked List
• Single Linked List• Double Linked List• Circular Linked List• Multilevel List
Data Structures - Linked List 29
Doubled Linked List
• Each node has two Link
• Previous Link pointed to the previous node• Next Link pointed to th next node• Head Prev Link Pointed as NULL• Tail Next Link Pointed as NULL
Data Structures - Linked List 30
Circular Linked List
• Next pointer at the tail, pointed to the Head
Data Structures - Linked List 31
What your opinion about double circular list?
Multilevel List
• List act as group list which node act as parent of groups have extra link to pointed to the other list as child list beside the link to the next group list node.
• Element– Information– Link to other node parent node– Link to child list
Data Structures - Linked List 33
Multilevel List Element
Example of multilevel list
references• http://courses.cs.vt.edu/~csonline/DataStructures/Lessons/index.html• http://www.cs.sunysb.edu/%7Eskiena/214/lectures/index.html• http://www.site.uottawa.ca/%7Eholte/T26/lecture1.html• Drozdek, Adam. Data Structures And Algorithms In C++ 3rd ed. Thomson
Course Technology. 2005.• Chai, Ian. White, Jonathon D. Structuring Data And Building Algorithms.
Mc Graw Hill. 2006.• Reingold, Edward M. Hansen Wilfred J. Data Structures. Little, Brown and
Company. 1983.
Data Structures - Linked List 36