Linked Lists. Example We would like to keep a list of inventory records – but only as many as we need An array is a fixed size Instead – use a linked.

Slides:



Advertisements
Similar presentations
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Arrays: pluses and minuses + Fast element access. -- Impossible to resize.
Advertisements

Chapter 3 Lists Dr Zeinab Eid.
CSCE 3110 Data Structures & Algorithm Analysis
CS 367 – Introduction to Data Structures
Linear Lists – Linked List Representation
Data Structures ADT List
Linked Lists Linked Lists Representation Traversing a Linked List
Data Structures Using C++
Chapter 17 Linked List Saurav Karmakar Spring 2007.
M180: Data Structures & Algorithms in Java
Review Learn about linked lists
Linked list More terminology Singly-linked lists Doubly-linked lists DLLs compared to SLLs Circular Lists.
COSC 1P03 Data Structures and Abstraction 5.1 Linear Linked Structures.
Linked Lists
Linked List Improvements & Memory. BigO's What is BigO for our basic linked list operations? InsertStart Insert at middle InsertEnd Retrieve First Value.
Data Structures & Algorithms
Variations of Linked Lists CS 302 – Data Structures Sections 6.2, 6.3 and 6.4.
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
CS 206 Introduction to Computer Science II 09 / 17 / 2008 Instructor: Michael Eckmann.
Linked Lists. Example We would like to keep a list of inventory records – but only as many as we need An array is a fixed size Instead – use a linked.
Doubly Linked Lists CS 308 – Data Structures. Node data info: the user's data next, back: the address of the next and previous node in the list.back.next.info.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 17: Linked Lists.
Doubly Linked Lists1 © 2014 Goodrich, Tamassia, Goldwasser Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition,
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data.
Chapter 3: Arrays, Linked Lists, and Recursion
Chapter 17 Linked List.
1 CSC 211 Data Structures Lecture 21 Dr. Iftikhar Azim Niaz 1.
Implementation of Linked List For more notes and topics visit: eITnotes.com.
Data Structures Using Java1 Chapter 4 Linked Lists.
4-1 Topic 6 Linked Data Structures. 4-2 Objectives Describe linked structures Compare linked structures to array- based structures Explore the techniques.
Kovács Zita 2014/2015. II. félév DATA STRUCTURES AND ALGORITHMS 26 February 2015, Linked list.
© 2014 Goodrich, Tamassia, Goldwasser Singly Linked Lists1 Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition,
Subject Name : Data Structure Using C Title : Linked Lists
Data Structures Using C++1 Chapter 5 Linked Lists.
Page 1 – Spring 2010Steffen Vissing Andersen Software Development with UML and Java 2 SDJ I2, Spring 2010 Agenda – Week 8 Linked List (a reference based.
Data Structures Doubly and Circular Lists Lecture 07: Linked Lists
2/21/20161 List Operations Advanced Programming Ananda Gunawardena.
Lists (2). Circular Doubly-Linked Lists with Sentry Node Head.
CSC 205 Programming II Lecture 15 Linked List – Other Variations.
1 What is a Circular Linked List? l A circular linked list is a list in which every node has a successor; the “last” element is succeeded by the “first”
Department of Computer Science 1 Some Practice Let’s practice for the final a little bit. OK?
Circular linked list A circular linked list is a linear linked list accept that last element points to the first element.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
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.
Doubly Linked List Exercises Sometimes it is useful to have a linked list with pointers to both the next and previous nodes. This is called a doubly linked.
CS32 Discussion Section 1B Week 3 TA: Hao Yu (Cody)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists.
Linked Lists and Generics Written by J.J. Shepherd.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists.
Linked Lists Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
Arrays, Link Lists, and Recursion Chapter 3. Sorting Arrays: Insertion Sort Insertion Sort: Insertion sort is an elementary sorting algorithm that sorts.
UNIT-II Topics to be covered Singly linked list Circular linked list
Data Structure and Algorithm: CIT231 Lecture 6: Linked Lists DeSiaMorewww.desiamore.com/ifm1.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
Lecture 6 of Computer Science II
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.
Sequences 6/3/2018 9:11 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Doubly Linked Lists 6/3/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Review Deleting an Element from a Linked List Deletion involves:
Linked List Stacks, Linked List Queues, Dequeues
EEL 4854 IT Data Structures Linked Lists
Dummy Nodes, Doubly Linked Lists and Circular Linked Lists
Linked Lists.
Doubly Linked Lists or Two-way Linked Lists
CS212D: Data Structures Week 5-6 Linked List.
Doubly Linked Lists Lecture 21 Tue, Mar 21, 2006.
LINKED LISTS.
Linked List Improvements
Linked Lists Chapter 5 (continued)
Presentation transcript:

Linked Lists

Example We would like to keep a list of inventory records – but only as many as we need An array is a fixed size Instead – use a linked list What are the disadvantages of using a linked list?

Linked List Node – one element of the linked list –Object – data stored in the node – examples? –next_ptr – a pointer to the next node in the list last node points to NULL Object next_ptr Ø Object next_ptr Object next_ptr

Linked List head keeps track of the head of the list tail keeps track of the last node in the list –tail not always used Object next_ptr Ø Object next_ptr Object next_ptr head tail

Insertion at Head Object1 next_ptr Ø head Object2 next_ptr Object3 next_ptr Insert here new_node tail

Insertion at Head Create new_node –store object in new_node Point new_node next_ptr to the node head points to Object1 next_ptr Ø head Object2 next_ptr Object3 next_ptr new_node tail

Insertion at Head Create new_node –store object in new_node Point new_node next_ptr to the node head points to Point head to new_node Object1 next_ptr Ø head Object2 next_ptr Object3 next_ptr new_node tail

Insertion at Head Does this algorithm work for the list below? Object1 next_ptr Ø head Object2 next_ptr Object3 next_ptr Ø head Object3 next_ptr new_node tail

Insertion at Head Create new_node –store object in new_node Point new_node next_ptr to the node head points to Point head to new_node If tail points to NULL –point tail to new_node

Insertion at Head Create new_node –store object in new_node Point new_node next_ptr to the node head points to Point head to new_node If tail points to NULL –point tail to new_node Ø head Object3 next_ptr new_nodetail

Insertion at Tail Ø head Object3 next_ptr new_nodetail Object1 next_ptr Ø head Object2 next_ptr Object3 next_ptr Insert here tail new_node

Find find(3) find(16) - always remember to deal with special cases 5 next_ptr Ø 3 12 next_ptr head tail

Deletion Deletion of head –Complexity? Deletion of tail –Complexity? Object1 next_ptr Ø head Object2 next_ptr Object3 next_ptr tail

Insertion/Deletion in Middle Insert between Object1 and Object2 Delete Object1 Object1 next_ptr Ø head Object2 next_ptr Object3 next_ptr tail

Exercises Recursively count number of nodes Reverse list Swap two nodes

Doubly Linked Lists Each node keeps a pointer to the next node and to the previous node –Makes some operations (such as insertion at end) more efficient –Costs? At the beginning and end of the list are sentinel nodes –Simplify insertion/deletion algorithm Object3 prev_ptr next_ptr trailer Object2 prev_ptr next_ptr Object1 prev_ptr next_ptr header

Doubly Linked Lists Insertion and deletion at beginning/end Insertion and deletion in middle Object3 prev_ptr next_ptr trailer Object2 prev_ptr next_ptr Object1 prev_ptr next_ptr header trailerheader

Doubly Linked Lists Insertion Object3 prev_ptr next_ptr trailer Object2 prev_ptr next_ptr Object1 prev_ptr next_ptr header Object4 prev_ptr next_ptr insert here new_node

Doubly Linked Lists Insertion at head 1.Set next_ptr of new_node to point to what header’s next_ptr points to 2.Set prev_ptr of node that header’s next_ptr points to to point to new_node 3.Set prev_ptr of new_node to point to header 4.Set header’s next_ptr to point to new_node Number 1 must come before number 4 Insertion at trailer? Deletion? Object3 prev_ptr next_ptr trailer Object2 prev_ptr next_ptr Object1 prev_ptr next_ptr header Object4 prev_ptr next_ptr insert here new_node