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
Inserting a Node into a Specified Position of a Linked List To create a node for the new item newNode = new Node(item); To insert a node between two nodes.
Advertisements

Chapter 3 Lists Dr Zeinab Eid.
CSCE 3110 Data Structures & Algorithm Analysis
Queues and Linked Lists
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 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.
Queue & List Data Structures & Algorithm Abstract Data Types (ADTs) ADT is a mathematically specified entity that defines a set of its instances,
Linked list More terminology Singly-linked lists Doubly-linked lists DLLs compared to SLLs Circular 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.
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.
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.
© 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.
CSE 131 Computer Science 1 Module 9: Linked Lists Using references to link objects Basic operations on linked lists Implementing a linked list of integers.
CS212D : DATA STRUCTURES 1 Week 5-6 Linked List. Outline 2  Singly Linked Lists  Doubly Linked Lists  Recursions.
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.
Data Structures Using C++1 Chapter 5 Linked Lists.
Chapter 5 Linked Lists. © 2004 Pearson Addison-Wesley. All rights reserved 5 A-2 Preliminaries Options for implementing an ADT –Array Has a fixed size.
CS 206 Introduction to Computer Science II 02 / 02 / 2009 Instructor: Michael Eckmann.
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.
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.
Lists List Implementations. 2 Linked List Review Recall from CMSC 201 –“A linked list is a linear collection of self- referential structures, called nodes,
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
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
One implementation of the LIST ADT Insert new node before current and new node becomes current (assume new node created) node newNode = new node; head.
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.
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,
Linked List Stacks, Linked List Queues, Dequeues
Linked Lists Linked Lists 1 Sequences Sequences 07/25/16 10:31
EEL 4854 IT Data Structures Linked Lists
LINKED LISTS CSCD 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.
Lists CSE 373 Data Structures.
Doubly Linked Lists Lecture 21 Tue, Mar 21, 2006.
Problem Understanding
LINKED LISTS.
Figure 4.1 a) A linked list of integers; b) insertion; c) deletion.
Lists CSE 373 Data Structures.
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 – a reference to the next node in the list last node points to NULL Object next Ø Object next Object next

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 Ø Object next Object next head tail

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

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

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

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

Insertion at Head Create new_node –store object in new_node Point new_node next 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 to the node head points to Point head to new_node If tail points to NULL –point tail to new_node Ø head Object3 next new_nodetail

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

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

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

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

Exercises Implement a method to recursively count number of nodes in a linked list Implement a method to reverse a linked list Implement a method that takes as input two integers representing node positions and swaps the specified 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 next trailer Object2 prev next Object1 prev next header

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

Doubly Linked Lists Insertion Object3 prev next trailer Object2 prev next Object1 prev next header Object4 prev next insert here new_node

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