Linked Representation

Slides:



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

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.
Linear Lists – Linked List Representation
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.
List Representation - Chain Fall 2010 CSE, POSTECH.
CS102--Object Oriented Programming Lecture 17: – Linked Lists Copyright © 2008 Xiaoyan Li.
Linked Lists list elements are stored, in memory, in an arbitrary order explicit information (called a link) is used to go from one element to the next.
The Template Class Chain Chain Linear list. Each element is stored in a node. Nodes are linked together using pointers.
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.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
©Brooks/Cole, 2003 Chapter 11 Data Structures. ©Brooks/Cole, 2003 Data Structure Data structure uses collection of related variables that can be accessed.
Linked Lists list elements are stored, in memory, in an arbitrary order explicit information (called a link) is used to go from one element to the next.
Linear Lists – Linked List Representation CSE, POSTECH.
Iterators & Chain Variants. Iterators  An iterator permits you to examine the elements of a data structure one at a time.  C++ iterators Forward iterator.
Linked Lists list elements are stored, in memory, in an arbitrary order explicit information (called a link) is used to go from one element to the next.
Linked Lists list elements are stored, in memory, in an arbitrary order explicit information (called a link) is used to go from one element to the next.
Linear List Linked Representation. Linked Representation list elements are stored, in memory, in an arbitrary order explicit information (called a link)
SNU IDB Lab. Ch7. Linear Lists – Simulated Pointers © copyright 2006 SNU IDB Lab.
Simulated Pointers Limitations Of Java Pointers May be used for internal data structures only. Data structure backup requires serialization and deserialization.
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 Linked Lists Reading: Ch. 20.
(c) University of Washington16-1 CSC 143 Java Lists via Links Reading: Ch. 23.
© 2014 Goodrich, Tamassia, Goldwasser Singly Linked Lists1 Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition,
Linked Structures, LinkedStack
Objects and Variables Local variables – Confined to single context: allocated on stack – Primitive types such as int or object references – Must be initialized.
The Class Chain. next (datatype ChainNode) element (datatype Object) Use ChainNode abcde null firstNode size = number of elements.
1. Circular Linked List In a circular linked list, the last node contains a pointer to the first node of the list. In a circular linked list,
1 PART 4 Linked Lists Singly Linked Lists Circular Lists Applications Doubly Linked Lists Generalized Lists.
SNU IDB Lab. Ch6. Linear Lists – Linked Representation © copyright 2006 SNU IDB Lab.
Circular linked list A circular linked list is a linear linked list accept that last element points to the first element.
Simulated Pointers Limitations Of C++ Pointers May be used for internal data structures only. Data structure backup requires serialization and deserialization.
Linked List, Stacks Queues
Chapter 3: Fundamental Data Structures: The Array and Linked Structures Data Structures in Java: From Abstract Data Types to the Java Collections Framework.
Chapter 16: Linked Lists.
Sections 10.1 – 10.4 Introduction to Arrays
Lecture 6 of Computer Science II
Reminder: Course Policy
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,
Review Deleting an Element from a Linked List Deletion involves:
Storage Strategies: Dynamic Linking
Data Structure Dr. Mohamed Khafagy.
UNIT-3 LINKED LIST.
Stacks.
Sequences 8/2/ :16 AM Linked Lists Linked Lists.
Iterators An iterator permits you to examine the elements of a data structure one at a time. C++ iterators Input iterator Output iterator Forward iterator.
The Class chain.
Queue, Deque, and Priority Queue Implementations
A Bag Implementation that Links Data
Queue, Deque, and Priority Queue Implementations
Simulated Pointers.
Iterators An iterator permits you to examine the elements of a data structure one at a time. C++ iterators Input iterator Output iterator Forward iterator.
Simulated Pointers.
Linked Lists.
CS212D: Data Structures Week 5-6 Linked List.
Iterators An iterator permits you to examine the elements of a data structure one at a time. C++ iterators Input iterator Output iterator Forward iterator.
Chapter 11 Data Structures.
Iterators An iterator permits you to examine the elements of a data structure one at a time. C++ iterators Input iterator Output iterator Forward iterator.
Problem Understanding
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
The Class chain.
The Class chain.
CSC 143 Java Linked Lists.
Data Structures & Algorithms
Linked Lists based on slides created by Ethan Apter
Linked List Functions.
LINKED LIST Dr. T. Kokilavani Assistant Professor
Linked Lists and Loops based on slides by Ethan Apter
Linked Lists list elements are stored, in memory, in an arbitrary order explicit information (called a link) is used to go from one element to the next.
Problem Understanding
Presentation transcript:

Linked Representation list elements are stored, in memory, in an arbitrary order explicit information (called a link) is used to go from one element to the next

Layout of L = (a,b,c,d,e) using an array representation. Memory Layout Layout of L = (a,b,c,d,e) using an array representation. a b c d e The figures show computer memory. An array uses a contiguous chunk of memory. A linked representation uses an arbitrary layout. c a e d b

Linked Representation c a e d b firstNode pointer (or link) in e is null use a variable firstNode to get to the first element a

Normal Way To Draw A Linked List b c d e null firstNode link or pointer field of node data field of node

Chain firstNode null a b c d e A chain is a linked list in which each node represents one element. There is a link or pointer from one element to the next. The last node has a null pointer.

Node Representation package dataStructures; class ChainNode { // package visible data members Object element; ChainNode next; // constructors come here } next element

Constructors Of ChainNode null ChainNode() {} null element ChainNode(Object element) {this.element = element;} ChainNode(Object element, ChainNode next) {this.element = element; this.next = next;} next element

desiredNode = firstNode; // gets you to first node a b c d e null firstNode desiredNode = firstNode; // gets you to first node Do a checkIndex first. Then move to desired node. Once you are at the desired node, you can return the element in that node as in return desiredNode.element; When firstNode = null, there is no element whose index is 0.

desiredNode = firstNode.next; // gets you to second node a b c d e null firstNode desiredNode = firstNode.next; // gets you to second node

desiredNode = firstNode.next.next; // gets you to third node a b c d e null firstNode desiredNode = firstNode.next.next; // gets you to third node

get(5) a b c d e null firstNode desiredNode = firstNode.next.next.next.next.next; // desiredNode = null Note that checkIndex would throw an exception.

NullPointerException a b c d e null firstNode desiredNode = firstNode.next.next.next.next.next.next; // gets the computer mad // you get a NullPointerException firstNode.next.next.next.next.next = null So desiredNode = null.next, which causes the null pointer exception.

Remove An Element remove(0) firstNode = firstNode.next; a b c d e null firstNode remove(0) First do a checkIndex. Even though this change in value of firstNode leaves behind a pointer from the original first node to the new first node, the original first node is no longer part of the chain, because you cannot get to it from firstNode. The memory used by the original first node will be reclaimed automatically by Java’s garbage collector. firstNode = firstNode.next;

first get to node just before node to be removed a b d e null firstNode c b beforeNode c first get to node just before node to be removed beforeNode = firstNode.next;

now change pointer in beforeNode remove(2) firstNode null a b c d e beforeNode now change pointer in beforeNode beforeNode.next = beforeNode.next.next;

add(0,’f’) firstNode newNode b c d e null firstNode f newNode Step 1: get a node, set its data and link fields First, do a checkIndex. ChainNode newNode = new ChainNode(new Character(‘f’), firstNode);

add(0,’f’) firstNode newNode Step 2: update firstNode b c d e null firstNode f newNode Step 2: update firstNode firstNode = newNode;

firstNode = new ChainNode( new Character(‘f’), firstNode); One-Step add(0,’f’) a b c d e null firstNode f newNode firstNode = new ChainNode( new Character(‘f’), firstNode);

first find node whose index is 2 add(3,’f’) a b c d e null firstNode f newNode beforeNode c first find node whose index is 2 next create a node and set its data and link fields ChainNode newNode = new ChainNode(new Character(‘f’), beforeNode.next); finally link beforeNode to newNode beforeNode.next = newNode;

Two-Step add(3,’f’) beforeNode = firstNode.next.next; c d e null firstNode f newNode beforeNode beforeNode = firstNode.next.next; beforeNode.next = new ChainNode(new Character(‘f’), beforeNode.next);