Linked Lists [AJ 15].

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

Stacks, Queues, and Linked Lists
Linked Lists Linear collections.
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data.
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
COMP 103 Linked Stack and Linked Queue.
Doubly-Linked Lists Cmput Lecture 16 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based.
Linked Lists. Preliminaries Options for implementing an ADT List Array Has a fixed size Data must be shifted during insertions and deletions Dynamic array.
CS 307 Fundamentals of Computer Science 1 Linked Lists many slides taken from Mike Scott, UT Austin.
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.
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.
Singly Linked Lists - Ed. 2, 3: Chapter 4 - Ed. 4.: Chapter 3.
Stacks, Queues, and Deques
Announcements  I will discuss the labtest and the written test #2 common mistakes, solution, etc. in the next class  not today as I am still waiting.
IMPLEMENTING ARRAYLIST – Part 2 COMP 103. RECAP  Abstract Classes – overview, details in 2 nd year  Implementing the ArrayList: size(), get(), set()
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
Scott Grissom, copyright 2004 Ch 6 Data Structures Slide 1 Linear Data Structures Chapter 6 focuses on: Lists Stacks Queues You can skip the Array implementation.
1 Linked Structures, LinkedSet References as Links Linear Linked Lists and Non-linear Structures Managing Linked Lists Data Encapsulation Separate from.
(c) University of Washington16-1 CSC 143 Java Lists via Links Reading: Ch. 23.
CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations.
Lecture5: Linked Lists Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Chapter 5 Linked Lists II
Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
Linked Lists Revision Based on: v/
Recursive Objects (Part 2) 1. Adding to the front of the list  adding to the front of the list  t.addFirst('z') or t.add(0, 'z') 2 'a' 'x' LinkedList.
LINKED LIST’S EXAMPLES Salim Malakouti. Linked List? 523 Pointer Node ValuePointer.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 18 List ADT Animated Version.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
CSCS-200 Data Structure and Algorithms Lecture
Recursive Objects Singly Linked List (Part 2) 1. Operations at the head of the list  operations at the head of the list require special handling because.
Linked Lists CS 367 – Introduction to Data Structures.
1 Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues Jung Soo (Sue) Lim Cal State LA.
Linked Structures - Stacks. Linked Structures An alternative to array-based implementations are linked structures A linked structure uses object references.
Linked Data Structures
Data Structures: Linked Lists
The List ADT.
Sequences 5/10/2018 2:01 PM Linked Lists Linked Lists.
Copy Constructor / Destructors Stacks and Queues
Storage Strategies: Dynamic Linking
Stacks.
Revision Based on: Linked Lists Revision Based on:
Sequences 8/2/ :13 AM Linked Lists Linked Lists.
Sequences 8/2/ :16 AM Linked Lists Linked Lists.
LinkedList Class.
public class StrangeObject { String name; StrangeObject other; }
Chapter 17 Object-Oriented Data Structures
Top Ten Words that Almost Rhyme with “Peas”
Pointers and Linked Lists
MyList<T> It’s a generic type, <T> is a type parameter
Linked Lists.
Stacks.
Queues.
Topic 16 Queues "FISH queue: n.
Programming in Java Lecture 11: ArrayList
Sequences 12/8/2018 3:02 AM Linked Lists Linked Lists.
Programming II (CS300) Chapter 07: Linked Lists and Iterators
ADT list.
Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues
Example: LinkedSet<T>
Computer Science and Engineering
ArrayLists 22-Feb-19.
Collections Framework
Stacks CS-240 Dick Steflik.
Data Structures & Algorithms
Recursive Objects Singly Linked Lists.
Queues Definition of a Queue Examples of Queues
Programming II (CS300) Chapter 07: Linked Lists
Stacks, Queues, and Deques
TCSS 143, Autumn 2004 Lecture Notes
Presentation transcript:

Linked Lists [AJ 15]

Linked Lists – Revisited a common way of storing arrays of data can insert or remove elements without reallocating or reorganizing of the entire structure allows insertion and removal of nodes at any point in the list we do not have to specify a fixed size many data structures often implemented using the concept of linked list stacks, queues, associative arrays, etc.

Anatomy of a Linked List a linked list consists of: a sequence of nodes myList a b c d each node contains a value (data) and a link (pointer or reference) to some other node the last node contains a null link the list may have a header

An Oversimplified Linked List a linked list that has a head, no tail, no iterator adds elements to the end of this list a specified element a specified element at a specified position removes the element at the specified position returns the element at the specified position This example is based on MyCSTutorials.com

Node Class a a node must contain: reference to the next node in the chain (or null if there isn't one) data carried by this node, which could be of any type public class Node { Node next; Object data; // constructor section a

Node Constructors two constructors: to create a node with no next node public Node(Object data) { this.next = null; this.data = data; } to create a node with the given data and next node public Node(Object data, Node next) { this.next = next;

Node Methods public Object getData() { return this.data; } public void setData(Object data) { this.data = data; public Node getNext() { return this.next; public void setNext(Node next) { this.next = next; returns data of this node set data of this node return the next node set next node to the given node

LinkedList Class we need a reference to the head node public class LinkedList { private Node head; private int listCount; // constructor section // method section }

LinkedList Constructor public LinkedList() { head = new Node(null); listCount = 0; } creates an empty list reference to the head node is set to a new node with no data listCount keeps count of the number of elements in the list

LinkedList add Method public void add(Object data) { Node temp = new Node(data); Node current = head; while(current.getNext() != null) current = current.getNext(); } current.setNext(temp); listCount++; adds the specified element to the end of this list start at the head node, then crawl to the end of the list the last node now referring to the new node that we have just added

LinkedList add Method public void add(Object data, int index) { Node temp = new Node(data); Node current = head; for(int i =1; i < index && current.getNext() !=null; i++) current = current.getNext(); } temp.setNext(current.getNext()); current.setNext(temp); listCount++; adds the specified element at the specified position crawls to the requested index or the last element in the list the new node's next-node is now referring to this node's next-node the last node now referring to the new node that we have just added

LinkedList remove Method public boolean remove(int index) { if(index < 1 || index > size()) { return false; } Node current = head; for(int i = 1; i < index; i++) { if(current.getNext() == null) { current = current.getNext(); current.setNext(current.getNext().getNext()); listCount--; return true; removes the element at the specified position exit if the index is out of range crawls to the requested index or the last element in the list exit if the last element of the list the current node's next-node is now referring to this node's next-node

LinkedList get Method returns the element at the specified position public Object get(int index) { if(index <= 0) return null; Node current = head.getNext(); for(int i = 1; i < index; i++) { if(current.getNext() == null) { } current = current.getNext(); return current.getData(); returns the element at the specified position index must be at least 1 exit if the last element of the list

A Simplified Linked List a better, yet simplified, LinkedList implements the List interface have to provide an implementation for each method specified in the List interface remember, List interface extends the Iterable interface have to provide an implementation for the single method iterator specified in the Iterable

Node Class a a node must contain: reference to the next node in the chain (or null if there isn't one) data carried by this node, which could be of any type public class Node<T> { private T element; private Node<T> next; // constructor section a

Node Constructors three constructors: 1) a node with no element and no next node public Node() { this.element = null; this.next = null; } 2) a node with the given element and next node public Node(T element, Node<T> next) { this.element = element; this.next = next;

Node Constructors 3) creates a deep copy of the given node. public Node(Node<T> copy) { this.element = copy.element; if (copy.next == null) { this.next = null; } else { this.next = new Node<T>(copy.next); if an empty list

Node Methods public T getElement() { return this.element; } public Node<T> getNext() { return this.next; public void setNext(Node<T> next) { this.next = next; returns data of this node return the next node set next node to the given node

LinkedList Class public class LinkedList<T> implements List<T> { private Node<T> head; private Node<T> tail; // constructors public LinkedList() { this.head = null; this.tail = null; } public LinkedList(Node<T> head, Node<T> tail){ this.head = head; this.tail = tail; PEx09 complete the copy constructor that creates a deep copy of the given linked list creates an empty list create a list with the given node as head

LinkedList Methods PEx09 private Node<T> getHead() { return this.head; } PEx09 write a similar function that returns the tail of the list public boolean isEmpty() { return (this.head == null); public Iterator<T> iterator() { return new LinkedListIterator<T>(this.head); return the head of this list test if this list is empty return an iterator for this list

LinkedList Methods PEx09 write add methods public int size() { int size = 0; for(T element : this) { size++; } return size; PEx09 write add methods the specified element to the end of this list use the tail the specified element at the specified position when passed index is zero when passed index is equal to the size or crawls to the requested index return the head of this list

LinkedList Methods PEx09 write reverse methods 1) use the head and the tail 2) another reverse method that takes two parameters, a list and a node get element that is stored in that node (position), and also the elements that are stored in following nodes then add these elements in the reversed order to the given list