Introduction. Reference

Similar documents
CS W3134: Data Structures in Java

ECE 242 Data Structures and Algorithms. Linked List I. Lecture 10. Prof.

1. Stack Implementation Using 1D Array

Data Structures - Binary Trees 1

Why Use Binary Trees? Data Structures - Binary Trees 1. Trees (Contd.) Trees

A linked list grows as data is added to it. In a linked list each item is packaged into a node.

Data Structures and Algorithms. Chapter 5. Dynamic Data Structures and Abstract Data Types

Programming 2. Topic 8: Linked Lists, Basic Searching and Sorting

CS171 Midterm Exam. October 29, Name:

DATA STRUCTURES AND ALGORITHMS

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

COL106: Data Structures and Algorithms. Ragesh Jaiswal, IIT Delhi

The combination of pointers, structs, and dynamic memory allocation allow for creation of data structures

A List Implementation That Links Data

CS2012 Programming Techniques II

EC8393FUNDAMENTALS OF DATA STRUCTURES IN C Unit 3

1. Introduction. Lecture Content

Linked Lists. Linked list: a collection of items (nodes) containing two components: Data Address (link) of the next node in the list

+ Abstract Data Types

Linked Lists. Linked list: a collection of items (nodes) containing two components: Data Address (link) of the next node in the list

List ADT. B/W Confirming Pages

Keeping Order:! Stacks, Queues, & Deques. Travis W. Peters Dartmouth College - CS 10

Stacks, Queues (cont d)

Ch. 17: Linked Lists. Introduction to Linked Lists

Data Structures. Alice E. Fischer. Lecture 4, Fall Alice E. Fischer Data Structures L4... 1/19 Lecture 4, Fall / 19

Data Structures and Algorithms. Roberto Sebastiani

You must include this cover sheet. Either type up the assignment using theory4.tex, or print out this PDF.

Data Structures - Simple Sorting

Chapter 17: Linked Lists

! A data structure representing a list. ! A series of dynamically allocated nodes. ! A separate pointer (the head) points to the first

DOWNLOAD PDF LINKED LIST PROGRAMS IN DATA STRUCTURE

Dynamic Data Structures

That means circular linked list is similar to the single linked list except that the last node points to the first node in the list.

CS 171: Introduction to Computer Science II. Linked List. Li Xiong

! A data structure representing a list. ! A series of nodes chained together in sequence. ! A separate pointer (the head) points to the first

Stacks have many uses Arithmetic Language parsing Keeping track of recursion (more in this in a week or so)

Chapter 17. Disk Storage, Basic File Structures, and Hashing. Records. Blocking

CS231 - Spring 2017 Linked Lists. ArrayList is an implementation of List based on arrays. LinkedList is an implementation of List based on nodes.

LINKED LISTS cs2420 Introduction to Algorithms and Data Structures Spring 2015

Laboratorio di Algoritmi e Strutture Dati

Lists. Arrays. CPSC 233: Introduction to Data Structures, Lists 1. James Tam. James Tam

9/26/2018 Data Structure & Algorithm. Assignment04: 3 parts Quiz: recursion, insertionsort, trees Basic concept: Linked-List Priority queues Heaps

Fun facts about recursion

Data Structures and Algorithms, Winter term 2017 Practice Assignment 7

Algorithms and Data Structures

Dynamic Data Structures

.:: UNIT 3 ::. LIST AND LINKED LIST

Announcements. Prelude (2) Prelude (1) Data Structures and Information Systems Part 1: Data Structures. Lecture 6: Lists.

Data structure and algorithm in Python

Introduction to Linked Data Structures

January 24, Abstract Data Types (ADTs) An ADT is an abstraction of a data structure.

Account joeacct = new Account (100, new Account (500)); Account joeacct = new Account (100, new Account (500, null));

9/29/2016. Chapter 4 Trees. Introduction. Terminology. Terminology. Terminology. Terminology

CS201 ArrayLists, Generics, and Dynamic Data Structures (Chapters 14, 15)

CSC 172 Data Structures and Algorithms. Lecture #8 Spring 2018

Implementation. Learn how to implement the List interface Understand the efficiency trade-offs between the ArrayList and LinkedList implementations

Hash Table and Hashing

Data Structures and Algorithms " Arrays and Linked Lists!

Introduction to Linked Lists

CSC 172 Data Structures and Algorithms. Lecture #9 Spring 2018

Chapter 17: Linked Lists

Linked lists. Comp Sci 1575 Data Structures. Definitions. Memory structure. Implementation. Operations. Comparison

Linked Lists. private int num; // payload for the node private Node next; // pointer to the next node in the list }

Linked Lists and other Dynamic Data Structures

Computer Science 62. Bruce/Mawhorter Fall 16. Midterm Examination. October 5, Question Points Score TOTAL 52 SOLUTIONS. Your name (Please print)

Linear Data Structures

Basic Data Structures

Stacks (5.1) Abstract Data Types (ADTs) CSE 2011 Winter 2011

Suppose that we have linked list of integers where each node is represented as: // An item in the list.

Extra: B+ Trees. Motivations. Differences between BST and B+ 10/27/2017. CS1: Java Programming Colorado State University

Data structure is an organization of information, usually in memory, for better algorithm efficiency.

Self-referential Structures and Linked List. Programming and Data Structure 1

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- MARCH, 2012 DATA STRUCTURE (Common to CT and IF) [Time: 3 hours

Data Structure Series

CMPSCI 187: Programming With Data Structures. Lecture #16: Thinking About Recursion David Mix Barrington 12 October 2012

Basic Data Structures 1 / 24

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

CMSC 341 Lecture 10 Binary Search Trees

Stack and Queue. Stack:

Due: 9 February 2017 at 1159pm (2359, Pacific Standard Time)

Linked lists. Insert Delete Lookup Doubly-linked lists. Lecture 6: Linked Lists

Computer Science 210 Data Structures Siena College Fall Topic Notes: Linear Structures

Linked List. April 2, 2007 Programming and Data Structure 1

Storage. Outline. Variables and Updating. Composite Variables. Storables Lifetime : Programming Languages. Course slides - Storage

Chapter 2: Using Data

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

Introduction to Data Structures and Algorithms

Lists are great, but. Stacks 2

DATA STRUCTURES AND ALGORITHMS

Implementing a List in Java. CSE 143 Java. Just an Illusion? List Interface (review) Using an Array to Implement a List.

Stack as an ADT. COMP 182: Data Structures and Program Design Lecture Slides 8. Stack as an Idea

CS 360 Programming Languages Interpreters

DATA STRUCTURES: OVERVIEW AND ITS APPLICATIONS

Exam 1 Prep. Dr. Demetrios Glinos University of Central Florida. COP3330 Object Oriented Programming

Prelim 1. CS 2110, October 1, 2015, 5:30 PM Total Question Name True Short Testing Strings Recursion

Linked Lists. College of Computing & Information Technology King Abdulaziz University. CPCS-204 Data Structures I

Operations on Singly (Simply) Linked Lists

Queues. CITS2200 Data Structures and Algorithms. Topic 5

Heap Arrays and Linked Lists. Steven R. Bagley

Homework 2: Imperative Due: 5:00 PM, Feb 15, 2019

Transcription:

Introduction Data Structures - Linked Lists 01 Dr TGI Fernando 1 2 Problems with arrays Unordered array - searching is slow, deletion is slow Ordered array - insertion is slow, deletion is slow Arrays have a fixed length Linked s solves some of these problems Linked s - general purpose storage structures after arrays Applicable in many cases in which arrays can be used UNLESS you need frequent random access to individual items using an index 1 Email: tgifernando@gmailcom 2 URL: http://tgifernandowordpresscom/ 1 / 20 2 / 20 Node Reference - Each data item is embedded in a node - Each node has an item a reference to next node in the p u b l i c c l a s s Node { p u b l i c i n t item ; // Data p u b l i c Node next ; // reference to next node - Recursive data structure: Definition of the Node class referred to Node itself - Each node object contains a reference (next) to the next node in the A reference is a number that refers to an object It s the object s address in the computer s memory, but you don t need to know its value; you just treat it as a magic number that tells you where the object is p u b l i c s t a t i c v o i d main ( String [] args ) { Node l1, l2; // references to Node Node objnode = new Node (); // Node object objnode item = 10; l1 = l2 = objnode ; // l1 and l2 refers to objnode System out println ( l 2 item = + l1 item ); System out println ( l 2 item = + l2 item ); 3 / 20 4 / 20

Null pointer Linked List - has a value reserved for indicating that the pointer does not refer to a valid object - routinely used to represent conditions such as the end of a of unknown length or the failure to perform some action - A linked (or simply a ) made up of nodes - Each node is connected to the next node by the reference next - Last node s next is set to null Null reference In Java programming, null can be assigned to any variable of a reference type (that is, a non-primitive type) to indicate that the variable does not refer to any object or array Relationship, Not position In an array each item occupies a particular position This position can be directly accessed by using an index 5 / 20 In a the only way to find a particular element is to follow along the chain of elements 6 / 20 The complete Node class The LinkList class In addition to data, this class contains a constructor and a method, displaynode(), that displays the node s data item c l a s s Node { p u b l i c i n t item ; // data p u b l i c Node next ; // reference to next node p u b l i c Node ( i n t i) { // constructor item = i; // initialize data next = n u l l ; // not necessary ; automatically set to null p u b l i c v o i d displaynode () { // display node item System out print ( item ); // end class Node c l a s s LinkList { p r i v a t e Node head ; // reference to first node on p u b l i c v o i d LinkList () { // constructor head = n u l l ; // empty - no items on yet p u b l i c boolean isempty () { // true if is empty r e t u r n ( head == n u l l ); // other methods go here Contains only one data item: a reference (head) to the first node on the The reference head finds the other nodes by following the chain of references from head, using each node s next field: 7 / 20 8 / 20

Inserting an item at the beginning of the Inserting an item at the beginning of the (Contd) This is the simplest way to add an item to a linked 1 allocate space for a new node, 2 copy the item into it, 3 make the new node s next pointer point to the current head of the and 4 make the head of the point to the newly allocated node p u b l i c v o i d insertfirst ( i n t i) { // inserts at start of Node newnode = new Node ( i); // make a new node newnode next = head ; // newnode s next pointer to point current head head = newnode ; // head points to the newnode 9 / 20 10 / 20 Deleting the first node Reverse of insertfirst () method Removes the first node from the and head points to second node on the This second node is found by looking at the next field in the first node Returns the reference of the deleted node deletefirst() method assumes the is not empty The displaylist() method Start from head and follow the chain of references from node to node A reference variable (to a node) current points to each node in turn Initially current points to head, which holds a reference to the first node The statement current = currentnext; changes current to point to the next node on the The end of is detected by by the next field of the last node pointing to null rather than another node w h i l e ( current!= n u l l ) { // until end of 11 / 20 12 / 20

The LinkList class c l a s s LinkList { p r i v a t e Node head ; // reference to first node on p u b l i c v o i d LinkList () { // constructor head = n u l l ; // empty - no items on yet p u b l i c boolean isempty () { // true if is empty r e t u r n ( head == n u l l ); p u b l i c v o i d insertfirst ( i n t i) { // inserts at start of // p u b l i c Node deletefirst () { // delete first item ( assumes not empty ) // p u b l i c v o i d displaylist () { // displays items on the // 13 / 20 The find() method Searches for a key in the Work much like displaylist() method The reference current initially points to head and in each turn it moves to the next node At each node, find() checks whether that node s key is the one it s looking for Output - If the key is found, it returns with a reference to that node - If find() reaches to the end of the without finding the desired node, it returns null Lab Work 04 - LinkList class 1 Complete the coding of the LinkList class 2 Include two methods find() and delete() described in slides 15 and 16 3 Write a class (LinkListApp) to use the methods defined in the LinkList class appropriately The delete() method Similar to find() method in the way it searches for the node to be deleted Needs to maintain two references: current node ( current ) and to the node preceding the current node ( previous ) At each cycle thorough a while loop, just before current is set to currentnext, previous is set to current To delete the current node (once found it) previousnext = currentnext; Special case: If the node to be deleted is the first node, the node is deleted by changing head to headnext The code that covers these two possibilities: i f ( current == head ) // if first node, head = head next ; // change head e l s e // otherwise previous next = current next ; // bypass node 14 / 20 15 / 20 16 / 20

Double-ended Double-ended (Contd) This tail node allows to insert a new node directly at the end of the You can insert a new node at the end of an ordinary single-ended by iterating through the entire until you reach the end But this approach is inefficient Suitable for some applications (Eg queue) Similar to an ordinary linked ; but it has one additional reference ( tail ) to the last node in addition to the reference head 17 / 20 18 / 20 The FirstLastList class Efficiency of a linked c l a s s FirstLastList { // implements a double - ended p r i v a t e Node head ; // ref to first node p r i v a t e Node tail ; // ref to last node p u b l i c FirstLastList () { // constructor p u b l i c boolean isempty () { // true if no nodes p u b l i c v o i d insertfirst ( i n t i) { // insert at front of p u b l i c v o i d insertlast ( i n t i) { // insert at end of p u b l i c Node deletefirst () { // delete first link p u b l i c v o i d displaylist () { Insertion and deletion at the beginning * Very fast * Need only changing one or two references * Takes O(1) time Finding, deleting or inserting next to a specific item * Requires searching through, on average, half the items in the * O(N) comparisons (Arrays also O(N) for these operations) * Linked - nothing needs to be moved when an item is inserted or deleted A linked uses exactly as much memory as it needs A linked can grow or shrink without wasting the memory or running out of room 19 / 20 20 / 20