CS2006- Data Structures I Chapter 5 Linked Lists I.

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

Linked Lists Chapter 4.
DATA STRUCTURES USING C++ Chapter 5
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List.
Data Structures1 Basic Data Structures Elementary Structures Arrays Lists Search Structures Binary search Tree Hash Tables Sequence Structures Stacks Queues.
CS Data Structures II Review COSC 2006 April 14, 2017
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are crated using the class definition. Programming techniques.
Topic 11 Linked Lists -Joel Spolsky
Linked Lists. Preliminaries Options for implementing an ADT List Array Has a fixed size Data must be shifted during insertions and deletions Dynamic array.
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 Lists part II. Linked Lists A linked list is a dynamic data structure consisting of nodes and links: 627 start 8 This symbol indicates a null reference.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Odds and Ends Strings (from Chapter 9) StringTokenizer.
Chapter 7 Stacks II CS Data Structures I COSC 2006
Arrays and Linked Lists "All the kids who did great in high school writing pong games in BASIC for their Apple II would get to college, take CompSci 101,
1 Chapter 16-1 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
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.
CS 307 Fundamentals of Computer ScienceLinked Lists 1 Topic 14 Linked Lists "All the kids who did great in high school writing pong games in BASIC for.
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.
APS105 Lists. Structures Arrays allow a collection of elements –All of the same type How to collect elements of different types? –Structures; in C: struct.
Chapter 5 Linked Lists II
CS2006- Data Structures I Chapter 5 Linked Lists III.
Linked Lists Chapter 4. Linked Structures: Motivations Arrays have fixed size –Problematic for data structures of arbitrary size Arrays order items physically.
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.
Linked list: a list of items (nodes), in which the order of the nodes is determined by the address, called the link, stored in each node C++ Programming:
CSC 243 – Java Programming, Spring, 2014 Week 4, Interfaces, Derived Classes, and Abstract Classes.
Linked Lists CS 367 – Introduction to Data Structures.
CSC 243 – Java Programming, Fall, 2008 Tuesday, September 30, end of week 5, Interfaces, Derived Classes, and Abstract Classes.
Object-Oriented Programming Using C++ Third Edition Chapter 7 Using Classes.
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
Link-Based Implementations
Procedural and Object-Oriented Programming
Linked Lists.
Elementary Data Structures
CS 215 Final Review Ismail abumuhfouz Fall 2014.
Linked Lists Chapter 6 Section 6.4 – 6.6
CS Data Structures Chapter 8 Lists Mehmet H Gunes
Sequences 8/2/ :13 AM Linked Lists Linked Lists.
classes and objects review
Introduction to Classes
Object-Oriented Programming Using C++
Data Structures and Database Applications Abstract Data Types
public class StrangeObject { String name; StrangeObject other; }
Top Ten Words that Almost Rhyme with “Peas”
The Singly-Linked Structure
Topic 11 Linked Lists -Joel Spolsky
Chapter 4 Link Based Implementations
Introduction to Classes
Chapter 13 Collections.
Data structures in C++.
Programming II (CS300) Chapter 07: Linked Lists and Iterators
Ch. 5 Linked List When a list is implemented by an array Linked list
Linked Lists Chapter 4.
Chapter 16 Linked Structures
Linked Lists The items in a linked list data structure are linked to one another An item in a linked list references its successor A linked list is able.
Computer Science and Engineering
Figure 4.1 a) A linked list of integers; b) insertion; c) deletion.
Introduction to Data Structure
Intro to OOP with Java, C. Thomas Wu By : Zanariah Idrus
Data Structures & Algorithms
Chapter 9: Pointers and String
Programming II (CS300) Chapter 07: Linked Lists
Topic 11 Linked Lists -Joel Spolsky
LINEAR DATA STRUCTURES
Presentation transcript:

CS2006- Data Structures I Chapter 5 Linked Lists I

Topics Linked List and Array Object & Reference Reference-Based Linked List

Array Limitations Arrays Is there any other way to implement the list? Simple Fast, easy to perform search but Difficult to insert and delete items Must specify size at construction time Is there any other way to implement the list?

Linked Lists Fortunately, we can use a structure called a linked list to overcome this limitation. The linked list is a very flexible dynamic data structure: items may be added to it or deleted from it at will. A linked list allows as many elements as a programmer needs requiring much less maintenance.

Object & References class Student { } String name; int ID; double GPA; Address address; } class variables (instance variable) are initialized by compiler automatically. Name = null; ID=0; GPA=0.0; address=null; //object references are initialized with null

Object & References Create a reference to a type student Student student; student is called a reference variable (or reference) of type Student it contains the address of an object or null; the object needs to be created using new ?

Object & References A reference can also be called a pointer (to an object in memory) and they are often depicted graphically: student = new Student (“John Smith”, 40725, 3.57) student John Smith 40725 3.57

Object & References student = null; This means that student reference does not “point” to any object student

References as Links Object references can be used to create links between objects Suppose a Student class contained a reference to another Student object (code?) John Smith 40725 3.57 Jane Jones 58821 3.72

References as Links References can be used to create a variety of linked structures, such as a linked list: studentList

Objects Two objects of this class can be instantiated and chained together having the next reference of one Node object refer to the other. The second object’s next reference can refer to a third Node object, and so on, creating a linked list of Node objects. Each node will contain some data as specified by the programmer. This constitutes a linked list

Linked List Definition A collection of data items of the same type that are stored in separate objects referred to as "nodes". Each node contains, in addition to its data value(s), a reference to an object of the same type.

Linked Lists List: An external reference usually referred to as the "head" of the list contains the address of the first node object. Diagram of a sample linked list containing int data: Node NULL pointer 45 51 84 Head Node Item Next

A Linked List Node Class First attempt at a class for a linked list of integers: public class IntegerNode { public int item; public IntegerNode next; } Problem?

Final IntegerNode Class public class IntegerNode { private int item; private IntegerNode next; public IntegerNode(int newItem) { item = newItem; next = null; } // end constructor public IntegerNode(int newItem, IntegerNode nextNode) { next = nextNode;

Final IntegerNode Class (2) public void setItem(int newItem) { item = newItem; } // end setItem public int getItem() { return item; } // end getitem public void setNext(IntegerNode nextNode) { next = nextNode; } // end setNext public IntegerNode getNext() { return next; } // end getNext } // end class IntegerNode

A Polymorphic Linked List Node public class Node { private Object item; private Node next; public Node(Object newItem) { item = newItem; next = null; } // end constructor public Node(Object newItem, Node nextNode) { next = nextNode;

A Polymorphic Linked List Node public class Node { private Object item; private Node next; public Node(Object newItem) { item = newItem; next = null; } // end constructor public Node(Object newItem, Node nextNode) { next = nextNode;

A Polymorphic Linked List Node public void setItem(Object newItem) { item = newItem; } // end setItem public Object getItem() { return item; } // end getitem public void setNext(Node nextNode) { next = nextNode; } // end setNext public Node getNext() { return next; } // end getNext } // end class Node To instantiate a Node containing an Integer: Node n = new Node(new Integer(6)); or containing a character: Node n = new Node(new Character('A'));

A Polymorphic Linked List Node public void setItem(Object newItem) { item = newItem; } // end setItem public Object getItem() { return item; } // end getitem public void setNext(Node nextNode) { next = nextNode; } // end setNext public Node getNext() { return next; } // end getNext } // end class Node To instantiate a Node containing an Integer: Node n = new Node(new Integer(6)); or containing a character: Node n = new Node(new Character('A'));

Review When you declare a variable that refers to an object of a given class, you are creating a(n) ______ to the object. interface reference method ADT

Review Integer maxNum; maxNum = new Integer (15); ______ is a reference variable. Integer maxNum New 15

Review A reference variable declared as a data field within a class has the default value ______. -1 null empty

Review If you attempt to use a reference variable before it is instantiated, a(n) ______ will be thrown. IndexOutOfBoundsException InstantiationException IllegalAccessException NullPointerException

Review A linked list contains components, called ______, which are linked to one another. nodes arrays vectors references

Review According to the principle of information hiding, the data fields of a class must be declared as ______. public protected private abstract