© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data.

Slides:



Advertisements
Similar presentations
Singly linked lists Doubly linked lists
Advertisements

© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Arrays: pluses and minuses + Fast element access. -- Impossible to resize.
Chapter 3 Lists Dr Zeinab Eid.
© 2004 Goodrich, Tamassia Node-Lists1 6.2 Node Lists.
Lists1 © 2010 Goodrich, Tamassia. Position ADT The Position ADT models the notion of place within a data structure where a single object is stored It.
COSC 1P03 Data Structures and Abstraction 9.1 The Queue Whenever you are asked if you can do a job, tell 'em, "Certainly, I can!" Then get busy and find.
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data.
3 May Linked Lists CSE 2011 Winter Linked Lists2 Singly Linked Lists (3.2) A singly linked list is a concrete data structure consisting of.
Elementary Data Structures CS 110: Data Structures and Algorithms First Semester,
1 Queues and Lists. QUEUES Very similar to stacks The only difference between them is in the order in which elements are processed. A stack uses a last-in/first-out.
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
Data Structures Lecture 4 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
Two-Dimensional Arrays Introduction to Linked Lists COMP53 Sept
Unit 11 1 Unit 11: Data Structures H We explore some simple techniques for organizing and managing information H This unit focuses on: Abstract Data Types.
Queue using an array. .head.tail Pointers head and tail always point to the first empty slot before or after elements in the list. Thus, initially they.
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.
Queues.
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.
Singly Linked Lists - Ed. 2, 3: Chapter 4 - Ed. 4.: Chapter 3.
© 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
CSC 212 – Data Structures Lecture 13: Linked Lists.
Chapter 4 Linked Structures – Stacks Modified. Chapter Scope Object references as links Linked vs. array-based structures Managing linked lists Linked.
Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.
CS212D : DATA STRUCTURES 1 Week 5-6 Linked List. Outline 2  Singly Linked Lists  Doubly Linked Lists  Recursions.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
STACKS AND QUEUES 1. Outline 2  Stacks  Queues.
Mohammad Amin Kuhail M.Sc. (York, UK) University of Palestine Faculty of Engineering and Urban planning Software Engineering Department Computer Science.
Lists II. List ADT When using an array-based implementation of the List ADT we encounter two problems; 1. Overflow 2. Wasted Space These limitations are.
4-1 Topic 6 Linked Data Structures. 4-2 Objectives Describe linked structures Compare linked structures to array- based structures Explore the techniques.
Self-Referential Classes A Self-referential class is a class that contains a reference to an object that has the same class type. –A self-referential class.
Lecture 3 Queues Queues1. queue: – Retrieves elements in the order they were added. – First-In, First-Out ("FIFO") – Elements are stored in order of insertion.
Lecture5: Linked Lists Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
© 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.
© 2004 Goodrich, Tamassia Lists1. © 2004 Goodrich, Tamassia Lists2 Position ADT (§ 5.2.2) The Position ADT models the notion of place within a data structure.
Introduction Dynamic Data Structures Grow and shrink at execution time Linked lists are dynamic structures where data items are “linked up in a chain”
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
Question of the Day  What three letter word completes the first word and starts the second one: DON???CAR.
1 Midterm 1 on Friday February 12 Closed book, closed notes No computer can be used 50 minutes 4 questions Write a function Write program fragment Explain.
Priority Queues CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Abstract Data Types Queues Dale Roberts, Lecturer
Linked List, Stacks Queues
Lecture 6 of Computer Science II
Sequences 5/10/2018 2:01 PM Linked Lists Linked Lists.
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.
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,
Data Structure Dr. Mohamed Khafagy.
CS212D: Data Structures Week 5-6 Linked List.
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
Data Structures Linked list.
CS212D: Data Structures Week 5-6 Linked List.
LINKED LISTS CSCD Linked Lists.
Queues.
Arrays and Linked Lists
Sequences 11/27/2018 1:37 AM Singly Linked Lists Singly Linked Lists.
Queues 12/3/2018 Queues © 2014 Goodrich, Tamassia, Goldwasser Queues.
Sequences 12/8/2018 3:02 AM Linked Lists Linked Lists.
CS212D: Data Structures Week 5-6 Linked List.
Problem Understanding
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Linked Lists & Iterators
CS210- Lecture 6 Jun 13, 2005 Announcements
Stacks and Linked Lists
Presentation transcript:

© 2004 Goodrich, Tamassia Linked Lists1

© 2004 Goodrich, Tamassia Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data structure consisting of a sequence of nodes, forming a linear ordering. Each node stores Element (data) link to the next node next elem node ABCD 

© 2004 Goodrich, Tamassia Linked Lists3 Empty Linked List An empty linked list is defined by: Head = Tail = Null List with Single Node Head = Tail ≠Null  Head Tail

© 2004 Goodrich, Tamassia Linked Lists4 The Node Class for List Nodes public classNode{ // Instance variables: private Object element; private Node next; /** Creates a node with null references to its element and next node. */ public Node(){ this(null, null); } /** Creates a node with the given element and next node. */ public Node(Object e, Node n) { element = e; next = n; } // Accessor methods: public Object getElement() { return element; } public Node getNext() { return next; } // Modifier methods: public void setElement(Object newElem) { element = newElem; } public void setNext(Node newNext) { next = newNext; }

© 2004 Goodrich, Tamassia Linked Lists5 Inserting at the Head 1. Allocate a new node 2. Insert new element- data 3. Have new node point to old head 4. Update head to point to new node 5. Increment size of list

© 2004 Goodrich, Tamassia Linked Lists6 Algorithm for Inserting at Head new (T);//Create a new Node T.data = A;// store element data T.next = Head; //new node points to old Head node If (Head = Null) then Tail = T// we have a single node-list Head = T;// Update Head to point to the new node

© 2004 Goodrich, Tamassia Linked Lists7 Inserting at the Tail 1. Allocate a new node 2. Insert new element- data 3. Have new node point to null 4. Have old last node point to new node 5. Update tail to point to new node 6. Increment size of list

© 2004 Goodrich, Tamassia Linked Lists8 Algorithm for Inserting at Tail of List new (T); //Create a new Node T.data = A; // store element data T.next = Null; //new node points to Null object If (Head = Null) then Head = T // we have a single else Tail.next = T; node-list Tail= T;// Update Tail to point to the new node

© 2004 Goodrich, Tamassia Linked Lists9 Removing at the Head 1. Update head to point to next node in the list 2. Allow garbage collector to reclaim the former first node 3. Decrement size of list

© 2004 Goodrich, Tamassia Linked Lists10 Algorithm for Removing at the Head If (Head = Null) then Output “error” //empty list else { T = Head; // temporary pointer y = T.data; //save elem data Head = T.next; //update Head If (Head = Null) then Tail = Null; //test for single node Delete(T);//Garbage collection }//End of Algorithm Notice That: Any Removal Algorithm consists of a single statement.

© 2004 Goodrich, Tamassia Linked Lists11 Removing at the Tail Removing at the tail of a singly linked list is not efficient! There is no constant-time way to update the tail to point to the previous node

© 2004 Goodrich, Tamassia Linked Lists12 Algorithm for Removing at the Tail We must cycle through the list (link hopping), starting at the Head, to determine the new Tail-node. If (Head = Null) then Output “error” //empty list else { T = Head; // temporary pointer If (Head=Tail) then {Head=Null; T=Null} // test for single node else { while T.next≠Tail do T=T.next; T.next = Null} y = Tail.data; //save elem data Delete(Tail);//Garbage collection Tail = T; //update Tail }//End of Algorithm

© 2004 Goodrich, Tamassia Linked Lists13 Stack with a Singly Linked List We can implement a stack with a singly linked list The top element is stored at the first node of the list The space used is O(n) and each operation of the Stack ADT takes O(1) time  t nodes elements

© 2004 Goodrich, Tamassia Linked Lists14 Queue with a Singly Linked List We can implement a queue with a singly linked list The front element is stored at the first node The rear element is stored at the last node The space used is O(n) and each operation of the Queue ADT takes O(1) time f r  nodes elements