360 likes | 593 Views
Linked List. Data Structures (TIB11) IT Department – Bunda Mulia University Teady Matius, M.Kom tmulyana@bundamulia.ac.id. Objectives. To understand about linked list To understand about the operations of linked list To understand about the common variants of linked list. Pointer.
E N D
Linked List Data Structures (TIB11) IT Department – Bunda Mulia University Teady Matius, M.Kom tmulyana@bundamulia.ac.id
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
Pointer • A pointer is basically used to represent the relation between two cells. Example : Pointer A to B Data Structures - Linked List
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
Linked List • A finite sequence of elements s1, 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
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
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
Single Linked List Data Structures - Linked List
Linked List Operation • Search / Locate • Insert • After the current cell • Before the current cell • Delete Data Structures - Linked List
Possible Operations • At the front of list • At the middle of list • At the end of list Data Structures - Linked List
Locate Operation • Assign PointerCell as Head PointerCell = Head; • Move toward by directing the PointerCell to the next PointerCell until find the matched node. PointerCell = PointerCell->Next; Data Structures - Linked List
Insert Operation • At the front of list Can be happen only at insert before current cell • At the end of list Can be happen only at insert after current cell • At the middle of list Data Structures - Linked List
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
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
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
Locating previous next Can be done in many ways • Save the previous node when locating the current node PreviousNode = CurrentNode; CurrentNode = CurrentNode->Next; • Retrieve when needed RetrieveNode = 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
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
Delete Head (1st trick) Data Structures - Linked List
Delete Middle Data Structures - Linked List
Delete Tail Data Structures - Linked List
Common Variants of Linked List • Single Linked List • Double Linked List • Circular Linked List • Multilevel List Data Structures - Linked List
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
Circular Linked List • Next pointer at the tail, pointed to the Head Data Structures - Linked 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
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