210 likes | 303 Views
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.
E N D
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 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
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
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
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
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.
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
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
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
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
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.
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)
Adding Nodes to an Empty Doubly-Linked List Before After pNew 39 pNew 39 pHead pHead pCur pCur
55 124 pCur Adding a Node to the Middle of a Doubly-Linked List Before pNew 64 After 64 pNew 55 124 pCur Code
Adding a Node to the End of a Doubly-Linked List Before pNew 84 45 76 After 84 pNew pCur 45 76 pCur Code
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.
Deleting the First Node from a Doubly-Linked List Before pHead 75 124 pCur After pHead Recycled 124 pCur Code
Deleting a Node from a Linked List – General Case Before 43 64 75 47 124 pCur After 43 64 Recycled 75 124 Code pCur
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.
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.