Linked Lists 2 3 9 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.

Slides:



Advertisements
Similar presentations
Linked Lists.
Advertisements

Linked Lists CSE 2451 Matt Boggus. Dynamic memory reminder Allocate memory during run-time malloc() and calloc() – return a void pointer to memory or.
Singly Linked List BTECH, EE KAZIRANGA UNIVERSITY.
Linked List Variations
David Weinberg presents Linked Lists: The Background  Linked Lists are similar to ArrayLists in their appearance and method of manipulation  They do.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
Data Structures: A Pseudocode Approach with C
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
COMP103 - Linked Lists (Part A)1 Chapter 17 Truly dynamic memory management.
Introduction to C Programming CE Lecture 19 Linear Linked Lists.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists.
Implementation of Linked List For more notes and topics visit: eITnotes.com.
Lists 1. Introduction Data: A finite sequence of data items. Operations: Construction: Create an empty list Empty: Check if list is empty Insert: Add.
Linked Lists. A linear linked list is a collection of objects, called nodes, each of which contains a data item and a pointer to the next node in the.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 17: Linked Lists.
1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and.
Linked List by Chapter 5 Linked List by
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Elementary Data Structures Linked Lists Linked Lists Dale.
Department of Computer Science 1 Some Practice Let’s practice for the final a little bit. OK?
Data Structures AZHAR MAQSOOD NUST Institute of Information Technology (NIIT) Lecture 6: Linked Lists Linked List Basics.
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:
Linked List.  Is a series of connected nodes, where each node is a data structure with data and pointer(s) Advantages over array implementation  Can.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
CPSC 252 Linked Lists III Page 1 Variations on Singly Linked Lists Inserting or deleting at the front of a list is different from at any other point in.
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.
CSE 1342 Programming Concepts
Unit – I Lists.
Cpt S 122 – Data Structures Abstract Data Types
Pointers and Linked Lists
Chapter 12 – Data Structures
Linked List :: Basic Concepts
Pointers and Linked Lists
Introduction to Linked Lists
Lectures linked lists Chapter 6 of textbook
Linked Lists Chapter 6 Section 6.4 – 6.6
Review Deleting an Element from a Linked List Deletion involves:
12 C Data Structures.
Lecture - 6 On Data Structures
Data Structure Dr. Mohamed Khafagy.
UNIT-3 LINKED LIST.
Problems with Linked List (as we’ve seen so far…)
Linked List Variations
Introduction to Data Structures
Lists.
Linked List Sudeshna Sarkar.
Programmazione I a.a. 2017/2018.
LINKED LISTS CSCD Linked Lists.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Introduction to Linked Lists
Lists.
Optimizing Malloc and Free
Linked Lists.
Topic 11 Linked Lists -Joel Spolsky
Arrays and Linked Lists
Pointers and Linked Lists
Linked Lists.
[Chapter 4; Chapter 6, pp ] CSC 143 Linked Lists [Chapter 4; Chapter 6, pp ]
Doubly Linked Lists Lecture 21 Tue, Mar 21, 2006.
Chapter 17: Linked Lists.
Chapter 16 Linked Structures
LINKED LISTS.
Introduction to C++ Linear Linked Lists
Linked Lists Adapted from Dr. Mary Eberlein, UT Austin.
Data Structures & Algorithms
Lecture No.02 Data Structures Dr. Sohail Aslam
Data Structures & Algorithms
Linked List Mechanics Slides Table of Contents Linear Lists
General List.
BY PROF. IRSHAD AHMAD LONE.
Presentation transcript:

Linked Lists 2 3 9 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 memory we use be contiguous. We can take a little bit of memory from here and a little bit of memory from there just so long as we can connect them together. This new data structure is called a linked list. Each element of a linked list contains not only the data we want to store, but also a pointer to the next element. The final element in the list has the NULL pointer, represented by a cross through the bottom half of the last node in this slide. To store a linked list, we just need to remember where the first element is. We can find the rest by following the pointers.

Nodes n next typedef struct node { int n; struct node* next; } node; We’ll call each element of the linked list a node, which will contain the data we wish to store as well as a pointer to the next element in the list. Here, we use struct syntax to create our own data type to encapsulate a node. int n is the data we wish to store in the node and struct node* next defines a pointer to the next node in the linked list. Remember that a node won’t be typedef-ed until after all of these lines are executed. Thus, we have to write struct node instead of just node, both before the curly braces and inside the curly braces. On the very last line, we provide node to typedef as the name we want to use for this new data type for the rest of our program.

Search 2 3 9 head To search the list for 9, we'll need an additional pointer (depicted in yellow in the slide). This pointer will point at each node successively and allow us to check if n == 9. Note that this is linear search, and therefore the worst case runtime of this algorithm is O(n).

bool search(node* list, int n) { node* ptr = list; while (ptr != NULL) if (ptr->n == n) return true; } ptr = ptr->next; return false; To begin with, ptr points to the first node of the list. As long as ptr != NULL, we will dereference it and check to see if ptr->n is the value we are searching for. If so, the function returns true. If we haven't yet found the value we're searching for, ptr must progress to the next node in the list. To point ptr to the next node in the list, we can’t simply write ptr++. The memory in a linked list is not contiguous, so the next node in the list is not necessarily right next to where ptr is pointing. Instead, ptr needs to point wherever ptr->next is pointing. If ptr has reached the end of the list without finding the value we are searching for, the function returns false.

Insertion 2 3 9 head Insertion and deletion of elements is potentially expensive with arrays. If we want to insert an element in an array anywhere except the end, we have to shift the rest of the array to the right -- with long arrays, this takes a lot of time. And if the array is full, we can't even add to the end, and have to copy it! In contrast, linked lists make element insertion and deletion very easy. A list is a good choice when the number of objects in your list is not known before you start to solve the problem, and the size of this list may grow and shrink during the task Let's insert an element into this linked list. To begin with, we need to request memory for a new node, and initialize it with data. The n field of our new node here has been set to 1, and its next field has been set to NULL. If we wish to maintain an ordered list, our new node belongs at the head of the list. 1

Insertion 2 3 9 head We need to be careful when updating pointers. If we first update head to point to our new node, then we orphan the remainder of the linked list. 1

Insertion 2 3 9 head Instead, we should point the new node's next field to the same node that head is pointing to. 1

Insertion 2 3 9 head 1 And then have head point to the new node. Voila! Our new node has been inserted without ever losing hold of the rest of the linked list! 1

node* new = malloc(sizeof(node)); // check for NULL if (new == NULL) void insert(int n) { // create new node node* new = malloc(sizeof(node)); // check for NULL if (new == NULL) exit(1); } // initialize new node new->n = n; new->next = NULL; // insert new node at head new->next = head; head = new; To insert at the head of a linked list: malloc a new node and initialize it with data set the new node's next field to point to the first node in the list set head to point to the new node

Doubly Linked Lists 2 3 9 head A doubly-linked list is a linked list in which each element contains pointers to both the next and the previous elements. Doubly-linked lists are useful when you need to insert and remove objects from the center of the list, as well as from its ends, or when you need to traverse the list forward as well as backward.

DLL Nodes n next prev typedef struct node { int n; struct node* next; struct node* prev; } node; n next prev Here's how to declare a node in a doubly-linked list. Note that this node has two pointers: one which points to the next node in the list, and one which points to the previous node in the list.