Linked Lists Source: presentation based on notes written by R.Kay, A. Hill and C.Noble ● Lists in general ● Lists indexed using pointer arrays ● Singly.

Slides:



Advertisements
Similar presentations
Pointers.
Advertisements

Introduction to Linked Lists In your previous programming course, you saw how data is organized and processed sequentially using an array. You probably.
Review Learn about linked lists
Data Structures: A Pseudocode Approach with C
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
CS Data Structures Chapter 4 Lists.
2 Preliminaries Options for implementing an ADT List Array has a fixed size Data must be shifted during insertions and deletions Linked list is able to.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
 2007 Pearson Education, Inc. All rights reserved C Data Structures.
Lists 1. Introduction Data: A finite sequence of data items. Operations: Construction: Create an empty list Empty: Check if list is empty Insert: Add.
4-1 Topic 6 Linked Data Structures. 4-2 Objectives Describe linked structures Compare linked structures to array- based structures Explore the techniques.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
APS105 Lists. Structures Arrays allow a collection of elements –All of the same type How to collect elements of different types? –Structures; in C: struct.
Data Structures Doubly and Circular Lists Lecture 07: Linked Lists
Linked list: a list of items (nodes), in which the order of the nodes is determined by the address, called the link, stored in each node C++ Programming:
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
1 Linked List. 2 List A list refers to a sequence of data items  Example: An array The array index is used for accessing and manipulation of array elements.
Data Structures and Algorithm Analysis Dr. Ken Cosh Linked Lists.
LINKED LISTS.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R2. Elementary Data Structures.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
1 Linked List. List vs Arrays Two built-in data structures that can be used to organize data, or to create other data structures: Lists Arrays.
Chapter 16: Linked Lists.
CSE 1342 Programming Concepts
Stack and Heap Memory Stack resident variables include:
Lecture 6 of Computer Science II
C++ Programming:. Program Design Including
Pointers and Linked Lists
Data Structure By Amee Trivedi.
Chapter 12 – Data Structures
Linked List :: Basic Concepts
Pointers and Linked Lists
Vectors 5/31/2018 9:25 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
CHP - 9 File Structures.
Introduction to Linked Lists
Lectures linked lists Chapter 6 of textbook
Design & Analysis of Algorithm Priority Queue
Data Structure Interview Question and Answers
Review Deleting an Element from a Linked List Deletion involves:
12 C Data Structures.
Data Structure and Algorithms
UNIT-3 LINKED LIST.
Chapter 4 Linked Lists.
ENERGY 211 / CME 211 Lecture 12 October 17, 2008.
Data Structures Interview / VIVA Questions and Answers
Linked Lists head One downside of arrays is that they have a fixed size. To solve this problem of fixed size, we’ll relax the constraint that the.
CSCE 210 Data Structures and Algorithms
Lists.
Linked List Sudeshna Sarkar.
LINKED LISTS CSCD Linked Lists.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Introduction to Linked Lists
Lists.
Object Oriented Programming COP3330 / CGS5409
Arrays and Linked Lists
Sequences 11/27/2018 1:37 AM Singly Linked Lists Singly Linked Lists.
Linked Lists.
Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues
Linked Lists Chapter 4.
Linked List.
Chapter 17: Linked Lists.
Linked Lists Chapter 5 (continued)
Data Structures & Algorithms
17CS1102 DATA STRUCTURES © 2018 KLEF – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED.
Linked Lists Chapter 5 (continued)
Linked Lists Chapter 5 (continued)
Chapter 9 Linked Lists.
Presentation transcript:

Linked Lists Source: presentation based on notes written by R.Kay, A. Hill and C.Noble ● Lists in general ● Lists indexed using pointer arrays ● Singly linked lists using array storage ● Conventional linked list diagrams ● Data structure for a linked list node ● Inserting a node in a singly linked list ● Deleting a node in a singly linked list ● Traversing a list and finding keys ● Doubly linked lists

Lists in general 1 A list is an ordered sequence of items. Previous examples have used a one-dimensional array or 'vector' to store a list, each element of the array typically being a record containing data on an item. For example the following array, in 'C', could store a list of students and their marks. Typedef struct person { char name[21];int mark;} PERSON; PERSON student[100];

Lists in general 2 The order of items stored in a list using an array is determined by the sequential positioning of the items in memory, ie consecutive items in the list are stored in consecutive memory locations. The use of a static array implies that the list will have a fixed maximum size determined by the variable declaration. This means that the maximum size must be determined in advance and the program written to allow for this even though it may frequently use only a small portion of the array. Alternatively space for a dynamic array can be calculated and allocated at runtime using malloc() before storing the items if the number of records is already known.

Lists in general 3 A more important problem is met when one attempts to add an item to the list whilst preserving the order of the items. for example, to add the name BOB to the following list, it is necessary to copy each array element from student [3] onwards up one position in order to insert the new record. This problem was met when studying the Insertion Sort algorithm. A similar problem exists when deleting records from the list.

Lists indexed using pointer array 1 If each data element is large then the amount of unused space in an array may be considerable, particularly if the array has to be large enough to anticipate possible maximum use. Inserting, deleting or sorting data elements in the array will require a large movement of data which will slow the operation of the program. These problems can be alleviated by using 'dynamic data structures and storing their locations in an array. The problem of determining the maximum size of the pointer array is still present but this will be considerably less than than that of an array of data structures. Inserting, deleting or sorting data elements in the array will now require the movement and manipulation of pointers which will be faster.

Lists indexed using pointer array 2 The data storage to the right of the drawing can more flexibly be allocated independently per record as needed using the malloc() function.

Singly linked lists using array or dynamic storage 1 A linked list allows data to be inserted into and deleted from a list without physically moving any data. Additionally, if the linked list is implemented using dynamic data structures the list size can vary as the program executes. Each data element or 'node' contains an address pointer. Consecutive items are not necessarily stored in order or in consecutive memory locations.

Singly linked lists using array or dynamic storage 2 The 2 top diagrams represent the same data structure. Neither is more efficient. Insertion requires moving indexes, not moving data.

Singly linked lists using array storage 3 The bottom 2 diagrams on the previous slide show a node for Bob (mark 62) being inserted into the linked list in alphabetical order. Each node contains the address of the node that follows it in the list. So the nodes can be stored anywhere in memory. The last node in the list contains a special address that indicates that there are no further nodes. This sentinel value could be -1 instead of an array index, or NULL in place of a pointer. The advantage of a linked list over an array becomes apparent when an item is added to the list. Since the physical location of each node does not determine its logical position in the list, there is no need to move any data. All that is required is to alter one or more of the address pointers.

Conventional linked list diagrams 1

Conventional linked list diagrams 2 Although the items stored in a linked list are in order, it is only possible to perform a sequential search of the list. A search must start with the first node and proceed sequentialy from one node to the next. If dynamic memory allocation is used, the space occupied by the deleted node must be freed to avoid a memory leak.

Data structure for a linked list node Each cell in a linked list will be a structured object. One (or more) data members (fields) will contain data, and one member will be a pointer to another similar structured object. Thus a structure to store data on a student's name and mark could be declared using: typedef struct cell { char name[21];int mark;struct cell *next; } ITEM; To create dynamic variables requires the use of a pointer variable to indicate the position of the first structured object. Two further pointer variables are required to insert and delete other structured objects in the list. These are declared as follows: ITEM *p,*q,*first;

Creating a new list 1 To create the first structured object function malloc() is used. first = (ITEM*)malloc(sizeof(ITEM)); first->next = NULL; Function malloc() requires an integer parameter, in this case k, for the number of bytes required, in this case the size of structure item. The void pointer returned by malloc() has to be cast to (ITEM*), to make it into a pointer for the correct type of data. The pointer member next of structure variable first is assigned the value NULL, a constant defined in stdio.h as value 0.

Creating a new list 2 It is often convenient to have a dummy node as the first item in the list to avoid having to process the first node separately from the remainder. This dummy node contains no data.

Inserting a node in a singly linked list 1 insert(ITEM *p,ITEM *q){ q->next=p->next; p->next=q; } Before this function is called, the new node must exist and the address of the node before the insert point must be known. This function is more complicated if there is no dummy node at the front of the list.

Inserting a node in a singly linked list 2 When insert() is called, *p is assigned the address of the node before the insert position and *q is similarly pointed at the node to be inserted.

Inserting a node in a singly linked list 3 The next pointer of the new node is assigned the address of the following node. The next pointer on the preceding node is made to point at the new node.

Deleting a node in a singly linked list 1 delete(ITEM *p){ ITEM *q; q=p->next; p->next=q->next; free(q); } The parameter *p points to the node before the node to be deleted.

Deleting a node in a singly linked list 2 When delete() is called, *p is assigned the address of the node before the delete position and *q is declared as an ITEM pointer. q is assigned the address of the node to be deleted, which is stored in the next pointer of the preceding node.

Deleting a node in a singly linked list 3 The next pointer of the node before the one to be deleted is pointed at the node after the one to be deleted. The memory occupied by the node to be deleted is freed for reuse to avoid a memory leak.

Traversing a linked list void display(ITEM *p){ /* display all nodes in SLL */ /* print headings */ printf("\n%-20s%5s\n","name","mark"); printf("\n%-20s%5s\n","----","----"); while (p->next!=(ITEM*) NULL){ /* loops until last node displayed */ p=p->next; /* first item doesn't contain data, last does */ printf("%-20s%5d\n",p->name,p->mark); }

Finding keys in a linked list A similar function is used to traverse a SLL in order to find the address of a data item. Finding nodes before this target point for insertion while maintaining a sort order or required for a deletion is left as an exercise for the student. ITEM *find(ITEM *p, char *searchkey){ /* find node in SLL with name same as searchkey */ while (p->next!=(ITEM*) NULL){ /* loop until last node displayed */ p=p->next; /* first item doesn't contain data, last does */ if(strcmp(p->name,searchkey)==0) return p; /* return address of node found with same name */ } return (ITEM*) NULL; /* search key not in SLL*/ }

Doubly linked lists 1 A doubly linked list contains both a forward pointer and a backward pointer; i.e. it contains the address of the preceding and the following nodes. A doubly linked list occupies slightly more storage space, and it is more complex to add or delete nodes since twice as many address pointers have to be manipulated. However, it can be traversed in either direction enabling both the node preceding and the node following a given node to be located.

Doubly linked lists 2 A doubly linked list node to store data on a student's name and mark could be declared using: struct node { char name[21]; int mark; struct node *next, *prev; /* pointers to next and previous nodes */ }; typedef struct node ITEM;

Doubly linked lists 3 In this diagram, we have a doubly linked list in its initial state with a single dummy node at the head.

Doubly linked list node insertion 1 q->next=p->next; p->next=q;

Doubly linked list node insertion 2 p=q->next; q->prev=p->prev; p->prev=q;

Doubly linked list node deletion This is left as an exercise for the student. You are recommended to use a search function to identify the address of the node to be deleted and then to pass this address to the delete function. The previous and next nodes can be identified using the forward and back pointers on the node to be deleted. The forward and back pointers within the node to be deleted need to be assigned to the previous and next nodes. The node to be deleted then needs to be freed to avoid leaking memory. The older HTML version of these notes contains some drawings to illustrate these actions.