Linked List, Stacks Queues

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.
Chapter 3 Lists Dr Zeinab Eid.
Queues and Linked 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.
© 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.
© 2004 Goodrich, Tamassia Vectors1. © 2004 Goodrich, Tamassia Vectors2 The Vector ADT (“Vector” = “Array List” in §6.1) The Vector ADT extends the notion.
Queue & List Data Structures & Algorithm Abstract Data Types (ADTs) ADT is a mathematically specified entity that defines a set of its instances,
Data Structures Lecture 4 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
LAB#4. Linked List : A linked list is a series of connected nodes. Each node contains at least: – A piece of data (any type) – Pointer to the next node.
Lists and Iterators CSC311: Data Structures 1 Chapter 6 Lists and Iterators Objectives Array Lists and Vectors: ADT and Implementation Node Lists: ADT.
Lists and Iterators (Chapter 6) COMP53 Oct 22, 2007.
© 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.
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.
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data.
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.
© 2004 Goodrich, Tamassia Vectors1 Lecture 03 Vectors, Lists and Sequences Topics Vectors Lists Sequences.
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data.
Arrays & Linked Lists Last Update: Aug 21, 2014EECS2011: Arrays & Linked Lists1.
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.
4-1 Topic 6 Linked Data Structures. 4-2 Objectives Describe linked structures Compare linked structures to array- based structures Explore the techniques.
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,
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.
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.
Data Structures Doubly and Circular Lists Lecture 07: Linked Lists
Introduction Dynamic Data Structures Grow and shrink at execution time Linked lists are dynamic structures where data items are “linked up in a chain”
Lecture 6 of Computer Science II
Elementary Data Structures
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,
Linked List Stacks, Linked List Queues, Dequeues
Data Structure Dr. Mohamed Khafagy.
Linked Lists Linked Lists 1 Sequences Sequences 07/25/16 10:31
COMP9024: Data Structures and Algorithms
Doubly Linked List Review - We are writing this code
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.
Lists and Sequences 9/21/2018 7:21 PM Sequences Sequences
LINKED LISTS CSCD Linked Lists.
Data Structures and Algorithms revision
Ch7. List and Iterator ADTs
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.
Lists and Sequences 12/8/2018 2:26 PM Sequences Sequences
CS212D: Data Structures Week 5-6 Linked List.
CS2013 Lecture 4 John Hurley Cal State LA.
Problem Understanding
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Linked Lists & Iterators
LAB#4 Linked List.
CS210- Lecture 6 Jun 13, 2005 Announcements
CS210- Lecture 7 Jun 14, 2005 Agenda Practice Session Vector
Data Structures & Programming
Problem Understanding
Presentation transcript:

Linked List, Stacks Queues

Sequences 12/4/2017 10:39 AM Linked Lists Linked Lists

Linked Lists Used for modelling lists. Different to arrayLists Can only add/remove at the start and end of a list.

Methods for single Linked list public void addFirst(StringNode n) public void removeFirst() public void addLast(StringNode node) public void removeLast() {

Singly Linked List node A singly linked list is a concrete data structure consisting of a sequence of nodes Each node stores: an element a link to the next node next elem  A B C D Linked Lists

The StringNode Class for List Nodes public class StringNode { // class Attributes – data instances private String element; // The data to be stored in this node IE. A STRING private StringNode nextNode; // A link to the next node in the chain IE A // POINTER or object reference /* Constructor Creates a node with the given element and next node. */ public StringNode(String e, StringNode n) { element = e; nextNode = n; } /* Constructor Creates a node with null references to its element and next node*/ public StringNode() { this(null, null); // call the construtor above i.e. public // StringNode(String e, StringNode n)

The StringNode Class for List Nodes public String getElement() // Accessor method { return element; } public StringNode getNext() // Accessor method return nextNode; public void setElement(String newElem) // Modifier methods: element = newElem; public void setNext(StringNode newNext) // Modifier methods: nextNode = newNext;

For any data structure/storage: Operations Location of Operation How to add How to retreive How to remove/delete. Beginning End Middle Linked Lists

Inserting at the Head of a List The current list... Create a new node with content X Insert the node Have new node point to old head Update head to point to new node head A B C  node X  node X head A B C  node head X A B C  Linked Lists

StringLinkedList data members public class StringLinkedList { StringNode head = null; // start of the list StringNode tail = null; // last element in the list

Linked List – addFirst method public void addFirst(StringNode n) { if (n == null) // Check we have a node to add... return; // Set our new node to point to the head of the current list. n.setNext(head); // Our new node 'n' will now become the head of the list head = n; // If the list was empty, make the tail point to // the new node as well if (tail == null) tail = n; } Linked Lists

Removing from the Head Update head to point to next node in the list Allow garbage collector to reclaim the former first node head A B C D  head A B C D  head B C D  A Linked Lists

Linked List – removeFirst method public void removeFirst() { // If list is empty, we can't remove anything so leave if (head == null) return; // Move head to the next item in the list head = head.getNext(); // If the list is empty, set the tail reference to null if (head == null) tail = null; // The original item that was at the head of the list // no longer has anything referencing it and will be // garbage collected in Java. In other programming languages // e.g. C++, you would have to delete it other wise you // would get a memory leak. }

Inserting at the Tail The current list... Create a new node pointing to null Insert new element Have old last node point to new node Update tail to point to new node head A B C  node X  node X  tail head A B C node tail head A B X  C Linked Lists

Linked List – addLast method public void addLast(StringNode node) { // If we were not given a node, leave if (node == null) return; // If list is empty, our new node will // be the head and tail of list if (head == null) { head = node; tail = node; } // Make the current last node point to our new node tail.setNext(node); // Now update the tail to be our new node Linked Lists

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 tail head A B C D  Linked Lists

Linked List – removeLast method public void removeLast() { if (head == null) return; // If list is empty, leave // If head is also the tail, the list // will be empty if (head == tail) { head = null; tail = null; return; } // Start at the head of the list StringNode n = head; // Now look for the last item while (n.getNext() != tail) n = n.getNext(); // n should now be pointing to the last but one // node in the list. This will be the new tail // We are going to drop the last element in the list // so make the current node's next pointer null n.setNext(null); // The old tail node is now replaced with 'n'. The // old tail node has no reference and will be garbage tail = n; Linked Lists

Linked List – removeLast method public void removeLast() { if (head == null) return; // If list is empty, leave // If head is also the tail, the list // will be empty if (head == tail) { head = null; tail = null; return; } // Start at the head of the list StringNode n = head; Linked Lists

Linked List – removeLast method // Now look for the last item while (n.getNext() != tail) n = n.getNext(); // n should now be pointing to the last but one // node in the list. This will be the new tail // We are going to drop the last element in the list // so make the current node's next pointer null n.setNext(null); // The old tail node is now replaced with 'n'. The // old tail node has no reference and will be garbage tail = n; } Linked Lists

Stack

Stack as a 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 takes O(1) time nodes t  elements Linked Lists

queue

Queue as a 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 r nodes f  elements Linked Lists

Doubly Linked List A doubly linked list provides a natural implementation of the Node List Nodes implement Position and store: element link to the previous node link to the next node Special trailer and header nodes prev next elem node trailer header nodes/positions elements Lists

Insertion p A B C p q A B C X p q A B X C We visualize operation insertAfter(p, X), which returns position q p A B C p q A B C X p q A B X C Lists

Insertion Algorithm Algorithm addAfter(p,e): Create a new node v v.setElement(e) v.setPrev(p) {link v to its predecessor} v.setNext(p.getNext()) {link v to its successor} (p.getNext()).setPrev(v) {link p’s old successor to v} p.setNext(v) {link p to its new successor, v} return v {the position for the element e} Lists

Deletion A B C D p A B C p D A B C We visualize remove(p), where p = last() A B C D p A B C p D A B C Lists

Deletion Algorithm Algorithm remove(p): t = p.element {a temporary variable to hold the return value} (p.getPrev()).setNext(p.getNext()) {linking out p} (p.getNext()).setPrev(p.getPrev()) p.setPrev(null) {invalidating the position p} p.setNext(null) return t Lists

Performance In the linked list implementation of the List ADT, The space used by a list with n elements is O(n) The space used by each position of the list is O(1) Operations of the List ADT run in up to O(n) time Lists