1 / 21

Linked lists

Linked lists. November 12, 2012. Concepts of linked lists. An array is an example of a list One major problem with the arrays is that the size of an array must be specified at the beginning. A different way to represent data is linked list. structure 1. structure 2. structure 3. item.

skyler
Download Presentation

Linked lists

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Linked lists November 12, 2012

  2. Concepts of linked lists • An array is an example of a list • One major problem with the arrays is that the size of an array must be specified at the beginning. • A different way to represent data is linked list. structure 1 structure 2 structure 3 item item item • A linked list is a collection of structures ordered not by their physical placement in memory but by logical links that are stored as part of the data. node

  3. Concepts of linked lists • A node maybe represented in general form as follows … • The structure may contain more than one item with different data types. However, one of the items must be a pointer of the type tag_name. member 3 member 1 member 2 next

  4. Concepts of linked lists • Consider an example to illustrate the concept of linking. • Assume that the list contains two nodes node1 and node2. • This statement creates space for two empty nodes • The next pointer of node1 can be made to point to node2 by statement node1 node2 node1.age node2.age node1.next node2.next node1 node2 node1.age node2.age Address node1.next node2.next

  5. Concepts of linked lists • Let us assign values to the field age • We may continue the this process to create a linked list of any number of values: • To end list we have to assign a special symbol: null pointer • The value of the age member of node2 can be printed out by node2 node1.age node2.age node1.next node2.next Link

  6. Advantage of Linked List • A linked list is dynamic data structure. Linked lists can grow or shrink in size during the execution of a program. • A linked list does not waste memory space. It uses the memory that is just needed for the list at any point of time. It is not necessary to specify the number of nodes to be used in the list. • The linked lists provide flexibility that allows to rearrange items efficiently. It easy to insert and delete items. • The major limitation is that the access to any arbitrary item is little time consuming and cumbersome.

  7. Example 1 Write a recursive function that will search a list for a specific item, returning NULL if it is not found and a pointer to the node containing the value if it is found. data data data data X head

  8. Example 2 Write a function that will insert a node pointed to by newnode before the node pointed to by target. newnode head target item item item item item X

  9. Type of linked lists structure 1 structure 2 structure 3 • Circular linked lists. • Two-way or doubly linked lists. • Circular doubly linked lists. structure 1 structure 2 structure 3 X item X item item item item item structure 1 structure 2 structure 3 item item item

  10. Doubly-Linked List Implementation Issues in C • A node in a doubly-linked list is a structure that has at least three fields. One of the fields is a data field; the two are pointers that contains the address of the logical predecessor node and logical successor node in the sequence. • An example doubly-linked list node with one data field: left data right

  11. Basic Operations on a Doubly-Linked List • Add a node. • Delete a node. • Search for a node. • Traverse (walk) the list. Useful for counting operations or aggregate operations. • Note: the operations on a doubly-linked list are exactly the same that are required for a singly-linked list. The reasons for using a doubly-linked list are application dependent for the most part.

  12. Adding Nodes to a Doubly-Linked List There are four steps to add a node to a doubly-linked list: • Allocate memory for the new node. • Determine the insertion point to be after (pCur). • Point the new node to its successor and predecessor. • Point the predecessor and successor to the new node. Current node pointer (pCur) can be in one of two states: • It can contain the address of a node (i.e. you are adding somewhere after the first node – in the middle or at the end) • It can be NULL (i.e. you are adding either to an empty list or at the beginning of the list)

  13. Adding Nodes to an Empty Doubly-Linked List Before After pNew 39 pNew 39 pHead pHead pCur pCur

  14. 55 124 pCur Adding a Node to the Middle of a Doubly-Linked List Before pNew 64 After 64 pNew 55 124 pCur Code

  15. Adding a Node to the End of a Doubly-Linked List Before pNew 84 45 76 After 84 pNew pCur 45 76 pCur Code

  16. Inserting a Node Into a Doubly-Linked List (Complete code)

  17. Deleting a Node from a Doubly-Linked List • Deleting a node requires that we logically remove the node from the list by changing various links and then physically deleting the node from the list (i.e., return it to the heap). • Any node in the list can be deleted. Note that if the only node in the list is to be deleted, an empty list will result. In this case the head pointer will be set to NULL. • To logically delete a node: • First locate the node itself (pCur). • Change the predecessor’s and successor's link fields to point each other (see example). • Recycle the node using the free() function.

  18. Deleting the First Node from a Doubly-Linked List Before pHead 75 124 pCur After pHead Recycled 124 pCur Code

  19. Deleting a Node from a Linked List – General Case Before 43 64 75 47 124 pCur After 43 64 Recycled 75 124 Code pCur

  20. Searching a Doubly-Linked List • Notice that both the insert and delete operations on a linked list must search the list for either the proper insertion point or to locate the node corresponding to the logical data value that is to be deleted.

  21. Traversing a Doubly-Linked List • List traversal requires that all of the data in the list be processed. Thus each node must be visited and the data value examined. • Notice that this is identical code to the singly-linked list except for the name of the pointer link.

More Related