CS212D: Data Structures Week 5-6 Linked List.

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Singly linked lists Doubly linked lists
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Arrays: pluses and minuses + Fast element access. -- Impossible to resize.
Queues and Linked Lists
Linear Lists – Linked List Representation
Chapter 17 Linked List Saurav Karmakar Spring 2007.
M180: Data Structures & Algorithms in Java
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data.
Queue & List Data Structures & Algorithm Abstract Data Types (ADTs) ADT is a mathematically specified entity that defines a set of its instances,
Data Structures: A Pseudocode Approach with C
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are crated using the class definition. Programming techniques.
Linked Lists1 Part-B3 Linked Lists. Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data structure consisting of a sequence.
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.
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data.
© 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
CS212D : DATA STRUCTURES 1 Week 5-6 Linked List. Outline 2  Singly Linked Lists  Doubly Linked Lists  Recursions.
Mohammad Amin Kuhail M.Sc. (York, UK) University of Palestine Faculty of Engineering and Urban planning Software Engineering Department Computer Science.
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
© 2014 Goodrich, Tamassia, Goldwasser Singly Linked Lists1 Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition,
1 Linked Lists (Lec 6). 2  Introduction  Singly Linked Lists  Circularly Linked Lists  Doubly Linked Lists  Multiply Linked Lists  Applications.
Department of Computer Science 1 Some Practice Let’s practice for the final a little bit. OK?
Arrays, Link Lists, and Recursion Chapter 3. Sorting Arrays: Insertion Sort Insertion Sort: Insertion sort is an elementary sorting algorithm that sorts.
Given a node v of a doubly linked list, we can easily insert a new node z immediately after v. Specifically, let w the be node following v. We execute.
CSCE , SPRING 2016 INSTRUCTOR: DR. NANCY M. AMATO 1.
Data Structures: A Pseudocode Approach with C 1 Chapter 5 Objectives Upon completion you will be able to: Explain the design, use, and operation of a linear.
CH 1-4 : INTRODUCTION ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA AND.
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
Linked List, Stacks Queues
Chapter 16: Linked Lists.
COMP9024: Data Structures and Algorithms
Lecture 6 of Computer Science II
Data Structure By Amee Trivedi.
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.
Ch7. List and Iterator ADTs
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,
Big-O notation Linked lists
Data structures and algorithms
Linked List Stacks, Linked List Queues, Dequeues
Data Structure Dr. Mohamed Khafagy.
Linked Lists Linked Lists 1 Sequences Sequences 07/25/16 10:31
UNIT-3 LINKED LIST.
CS212D: Data Structures Week 5-6 Linked List.
CSE 143 Linked Lists [Chapter , 8.8] 3/30/98.
Sequences 8/1/2018 4:38 AM Linked Lists Linked Lists.
Sequences 8/2/ :13 AM Linked Lists Linked Lists.
Sequences 8/2/ :16 AM Linked Lists Linked Lists.
EEL 4854 IT Data Structures Linked Lists
Linked List Sudeshna Sarkar.
CS212D: Data Structures Week 5-6 Linked List.
LINKED LISTS CSCD Linked Lists.
Arrays and Linked Lists
Sequences 11/27/2018 1:37 AM Singly Linked Lists Singly Linked Lists.
Linked Lists.
Sequences 12/8/2018 3:02 AM Linked Lists Linked Lists.
Doubly Linked Lists or Two-way Linked Lists
11-3 LINKED LISTS A linked list is a collection of data in which each element contains the location of the next element—that is, each element contains.
Programming II (CS300) Chapter 07: Linked Lists and Iterators
CS2013 Lecture 4 John Hurley Cal State LA.
Lists CSE 373 Data Structures.
Problem Understanding
LINKED LISTS.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Linked Lists & Iterators
Lists CSE 373 Data Structures.
CS210- Lecture 6 Jun 13, 2005 Announcements
Programming II (CS300) Chapter 07: Linked Lists
Chapter 9 Linked Lists.
Problem Understanding
Presentation transcript:

CS212D: Data Structures Week 5-6 Linked List

Outline Singly Linked Lists Doubly Linked Lists Recursions

Data Structures and Algorithms A data structure is a way of storing data in a computer so that it can be used efficiently. Algorithms An algorithm is a finite sequence of well-defined instructions for solving a problem and guaranteed to terminate in a finite time. An algorithm does not necessarily need to be executable in a computer, but can be converted into a program.

Algorithms versus Heuristics A heuristic is a finite sequence of well-defined instructions for partially solving a hard problem and guaranteed to terminate in a finite time. A heuristic is not always guaranteed to find a solution while an algorithm is. Descriptions of algorithms We will use Java-like pseudo code mixed with English to describe algorithms.

Abstract Data Types An Abstract Data Type (ADT) is a specification of a set of data and the set of operations that can be performed on the data. Such a data type is abstract in the sense that it is independent of various concrete implementations. The definition can be mathematical, or it can be programmed as an interface. We need to implement an ADT by using a class to make it usable.

Linked Lists

Singly Linked List linked list, in its simplest form, is a collection of nodes that collectively form a linear sequence. In a singly linked list, each node stores a reference to an object that is an element of the sequence, as well as a reference to the next node of the list. next node elem  A B C D

Singly Linked List (Example) The node’s element field refers to an object that is an element of the sequence (the airport code MSP, in this example), while the next field refers to the subsequent node of the linked list (or null if there is no further node). 9-Dec-18 Computer Science Department

Singly Linked List The linked list instance must keep a reference to the first node of the list, known as the head. Without an explicit reference to the head, there would be no way to locate that node. The last node of the list is known as the tail. The tail of a list can be found by traversing the linked list 9-Dec-18 Computer Science Department

Implementing a single linked list class 9-Dec-18 Computer Science Department

Implementing a single linked list class cont. 9-Dec-18 Computer Science Department

Implementing a single linked list class cont. 9-Dec-18 Computer Science Department

Inserting an Element at the Head of Single Linked List(1/2) Allocate a new node Insert the new element Have the new node point to old head Update head to point to the new node

Inserting an Element at the Head of Single Linked List (2/2)

Inserting an Element at the Tail of Single Linked List (1/2) Allocate a new node Insert the new element Have the new node point to null Have the old last node point to new node Update tail to point to new node

Inserting an Element at the Tail of Single Linked List(2/2)

Removing an Element from the Head of Single Linked List (1/2) Update head to point to next node in the list Allow garbage collector to reclaim the former first node

Removing an Element from the Head of Single Linked List(2/2)

Removing at the Tail (1/2) Removing the tail node of a singly linked list cannot be done in constant time. We need to traverse the whole linked list to remove the tail node, which takes O(n) time, where n is the number of nodes in the linked list.

Removing at the Tail (2/2) Algorithm removeLast() { if ( size = 0 ) Indicate an error: the list is empty; else { if ( size = 1 ) // only one node head = null; tail=null; else // at least two nodes { t = head; /* make t point to the head */ while ( t.getNext != null ) /* find the last node */ { s = t; t = t.getNext(); } s.setNext(null); //null out the next pointer of the last node } size = size - 1; //decrement the node count

Exercises Add a member method called Print to the Single Linked List class, that prints all the list elements. State wither the following sentence is True or False, explain your answer: we cannot easily delete the last node of a singly linked list. 9-Dec-18 Computer Science Department

Doubly Linked List A doubly linked list is a linked list in which each node keeps an explicit reference to the node before it and a reference to the node after it. Each node stores: element link to the previous node “prev” link to the next node “next” Special trailer and header nodes prev next elem node

Inserting with Double Linked List Every insertion into our doubly linked list representation will take place between a pair of existing nodes. 9-Dec-18 Computer Science Department

Inserting at the Head of the Double Linked List (1/2) 9-Dec-18 Computer Science Department

Inserting at the Head of the Double Linked List (1/2) 9-Dec-18 Computer Science Department

Inserting an Element in the Middle of the Double Linked List 9-Dec-18 Computer Science Department

Inserting an Element at the Trailer 9-Dec-18 Computer Science Department

Deletion with Double Linked List The deletion of the node proceeds in the opposite fashion of an insertion. The two neighbors of the node to be deleted are linked directly to each other, thereby bypassing the original node. As a result, that node will no longer be considered part of the list and it can be reclaimed by the system. 9-Dec-18 Computer Science Department

Removing in the Middle 9-Dec-18 Computer Science Department

Implementing a Doubly Linked List Class 9-Dec-18 Computer Science Department

Implementing a Doubly Linked List Class Cont. 9-Dec-18 Computer Science Department

Implementing a Doubly Linked List Class Cont. 9-Dec-18 Computer Science Department

Implementing a Doubly Linked List Class Cont. 9-Dec-18 Computer Science Department

Exercises Add a member method called PrintOpposite to the Double Linked List class, that prints all the double linked list elements in an opposite order. 9-Dec-18 Computer Science Department

References Chapter 3, Data Structures and Algorithms by Goodrich and Tamassia. Garbage Collection by Bill Venners (http://www.artima.com/insidejvm/ed2/gc.html).