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.

Slides:



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

Stacks, Queues, and Linked Lists
Linked Lists Linear collections.
Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List.
Lecture Stacks. A stack is a Last-In-First-Out (LIFO) or a First-In-Last-Out (FILO) abstract data type E.g. a deck of cards in which cards may be added.
Algorithm Analysis.
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data.
Data Structure (Part I) Stacks and Queues. Introduction to Stack An stack is a ordered list in which insertion and deletions are made at one end. –The.
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
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.
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.
CMPT 225 ADT List using Dynamic arrays A dynamic data structure is one that changes size, as needed, as items are inserted or removed The Java ArrayList.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
Chapter 4 Linked Lists Anshuman Razdan Div of Computing Studies
 2006 Pearson Education, Inc. All rights reserved Data Structures.
Linked List (I) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
Self Referential Structure. A structure may not contain a member of its own type. struct check { int item; struct check n; // Invalid };
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Chapter 7 More Lists. Chapter 7: More Lists 7.1 – Circular Linked Lists 7.2 – Doubly Linked Lists 7.3 – Linked Lists with Headers and Trailers 7.4 – A.
Data Structures Outline Introduction Self-Referential Classes Dynamic Memory Allocation Linked Lists Stacks Queues Trees.
Review 1 Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Custom Templatized Data Structures.
 2006 Pearson Education, Inc. All rights reserved Data Structures.
 2007 Pearson Education, Inc. All rights reserved C Data Structures.
Introduction to Data Structures Systems Programming.
Information and Computer Sciences University of Hawaii, Manoa
Lecture 14 Linked Lists 14-1 Richard Gesick. Linked Lists Dynamic data structures can grow and shrink at execution time. A linked list is a linear collection.
 2002 Prentice Hall, Inc. All rights reserved. Chapter 19 – Data Structures Outline 19.1 Introduction 19.2 Self-Referential Classes 19.3 Dynamic Memory.
ليست هاي پيوندي Linked Lists ساختمان داده ها و الگوريتم ها.
Mohammad Amin Kuhail M.Sc. (York, UK) University of Palestine Faculty of Engineering and Urban planning Software Engineering Department Computer Science.
Lab 7 Queue ADT. OVERVIEW The queue is one example of a constrained linear data structure. The elements in a queue are ordered from least recently added.
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.
Introduction to Data Structures Systems Programming Concepts.
1 Chapter 17 – Data Structures Outline Introduction Self-Referential Classes Dynamic Memory Allocation Linked Lists Stacks Queues Trees.
Kovács Zita 2014/2015. II. félév DATA STRUCTURES AND ALGORITHMS 26 February 2015, Linked list.
Introduction to Data Structures and Algorithms
Final Exam –Date: Aug 27 th –Time: 9:00am – 12:00pm –Location: TEL 0014.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
Chapter Lists Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2010.
Linked Lists. Introduction In linked list each item is embedded in a link Each item has two parts – Data – Pointer to the next item in the list Insert,
Introduction Dynamic Data Structures Grow and shrink at execution time Linked lists are dynamic structures where data items are “linked up in a chain”
Java How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To introduce the basic concepts of linked lists ❏ To introduce the basic concepts.
Question of the Day  What three letter word completes the first word and starts the second one: DON???CAR.
CSCS-200 Data Structure and Algorithms Lecture
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Linked Lists Outline Introduction Self-Referential Structures.
UNIT-II Topics to be covered Singly linked list Circular linked list
Data Structure and Algorithm: CIT231 Lecture 6: Linked Lists DeSiaMorewww.desiamore.com/ifm1.
List data structure This is a new data structure. The List data structure is among the most generic of data structures. In daily life, we use shopping.
Data Structures: Linked Lists
Chapter 12 – Data Structures
5.13 Recursion Recursive functions Functions that call themselves
CS2006- Data Structures I Chapter 5 Linked Lists I.
Chapter 22 Custom Generic Data Structures
CISC181 Introduction to Computer Science Dr
Sequences 8/2/ :13 AM Linked Lists Linked Lists.
Stacks and Queues.
8-1.
8-1.
Chapter 19 – Data Structures
Arrays and Linked Lists
Data Structures ADT List
Sequences 12/8/2018 3:02 AM Linked Lists Linked Lists.
Review & Lab assignments
Intro to OOP with Java, C. Thomas Wu By : Zanariah Idrus
Presentation transcript:

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 typically has a variable definition that is the same type as the Class. –This type of class can be used to create data structures like linked lists, stacks, etc. –null is used to indicate the end of the data structure.

Self-Referential Classes Class Node { private Object data; private Node next; public void Node(Object d) { } public void setData(Object d) {} public Object getData() {} public void setNext(Node nextNode) {} public Node getNext() {} }

Dynamic Memory Allocation The ability to have your program get more space in memory while your program is running is called dynamic memory allocation. –The new operator must be used to obtain dynamic memory allocation.

Linked Lists A linked list is a linear collection of nodes that are connected by memory reference links. –The first node has a reference to the second node, the second node has a reference to the third node, etc. –The last node of the list always has a null reference. This marks the end of the list.

Linked Lists J B P A First Node Last Node

Linked Lists vs. Arrays Arrays can become full, and they can not be easily resized while the program executes. Linked Lists can be used when the number of elements is unknown because the length of the list can increase and decrease as needed.

Linked Lists Example: –EmptyListException –List Class –ListTest program

Linked Lists The insertAtFront method first checks to see if the list is empty, if it is: –then the first and last nodes are set to the same node, –otherwise, the new item is inserted at the front of the list and the old first item becomes the second item.

Linked Lists insertAtFront A P H List New node A P H List New node

Linked Lists The insertAtBack method first checks to see if the list is empty, if it is: –then the first and last nodes are set to the same node, –otherwise, the new item is inserted at the end of the list and the old last item becomes the second to last item.

Linked Lists insertAtBack A P H List New node A P H List New node

Linked Lists The insertNodeAfter method first checks to see if the list is empty, if it is: –then the first and last nodes are set to the same node, –otherwise, the node (n1) after which the new item is to be inserted is located and the new item is inserted after n1.

Linked Lists insertNodeAfter A P H List New node M P H List New node M A

Linked Lists The removefromFront method first assigns the variable to hold the removed item to the first node. –If the first and last node are the same node, then they are both set to null –Otherwise, the first node is set to be the next node.

Linked Lists removeFromFront A P List A P Removed Item

Linked Lists The removefromBack method first assigns the variable to hold the removed item to the last node. –If the first and last node are the same node, then they are both set to null –Otherwise, the second to last node is set to be the last node.

Linked Lists removeFromBack A P List A P Removed Item

Linked Lists The removeNodeAfter method assigns the variable to hold the removed item to the next node. –If the first and last node are the same node, then they are both set to null –otherwise, the node (n1) after which the item is to be removed is located and the item is removed. Node (n1) is set to point to the next node in the list.

Linked Lists removeNodeAfter A P List M P H Removed item M A H

Example Write a program that inserts 25 random integers from 0 to 100 in order in a linked list object. The program should calculate the sum of the elements, and the floating-point average of the elements. –List2.java

Searching Linked Lists Extend List2.java to implement a listSearch method. The method takes an Object and an int key.

Stacks Implementation of a Stack using a Linked List.

Queues Implementation of a Queue using a Linked List.