IndexedListWithIteratorsViaLinear1Ind

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

1 Linked lists Sections 3.2, 3.3, 3.5 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors.
CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
CHAPTER 4 Queues. Queue  The queue, like the stack, is a widely used data structure  A queue differs from a stack in one important way  A stack is.
CHAPTER 4 Queues MIDTERM THURSDAY, OCTOBER 17 IN LAB.
COMP 103 Linked Stack and Linked Queue.
CS 106 Introduction to Computer Science I 12 / 06 / 2006 Instructor: Michael Eckmann.
Stacks. What is a stack? Last-in first-out data structure (LIFO) New objects are placed on top Removal restricted to top object Examples?
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Fall 2007CS 2251 Iterators and Tree Traversals. Fall 2007CS 2252 Binary Trees In a binary tree, each node has at most two subtrees A set of nodes T is.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
30-Jun-15 Stacks. What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything.
Stacks CS-240 & CS-341 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed.
Stacks, Queues, and Deques. 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.
Review 1 Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists.
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 3)
Data structures Abstract data types Java classes for Data structures and ADTs.
1 Chapter 17 Object-Oriented Data Structures. 2 Objectives F To describe what a data structure is (§17.1). F To explain the limitations of arrays (§17.1).
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
1 The Stack Class Final Review Fall 2005 CS 101 Aaron Bloomfield.
Object-Oriented Programming Simple Stack Implementation.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Stacks And Queues Chapter 18.
CS 367 Introduction to Data Structures Lecture 5.
1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.
Lecture 20 Stacks and Queues. Stacks, Queues and Priority Queues Stacks – first-in-last-out structure – used for function evaluation (run-time stack)
Copyright © Curt Hill Stacks An Useful Abstract Data Type.
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
Click to edit Master text styles Stacks Data Structure.
List Structures What is a list? A homogeneous collection of elements with a linear relationship between the elements linear relationship - each element.
1 Lecture 9: Stack and Queue. What is a Stack Stack of Books 2.
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.
1 Iterators & the Collection Classes. 2 » The Collection Framework classes provided in the JAVA API(Application Programmer Interface) contains many type.
Values vs. References Lecture 13.
Stack: a Linked Implementation
Review Array Array Elements Accessing array elements
Pointers and Linked Lists
Pointers and Linked Lists
Copy Constructor / Destructors Stacks and Queues
Queues Rem Collier Room A1.02
CE 221 Data Structures and Algorithms
Stacks and Queues.
Queues Queues Queues.
Chapter 17 Object-Oriented Data Structures
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
CMSC 341 Lecture 5 Stacks, Queues
Pointers and Linked Lists
CSCI 3327 Visual Basic Chapter 7: Data Manipulation in Arrays
THURSDAY, OCTOBER 17 IN LAB
Stacks, Queues, and Deques
Lesson Objectives Aims
Pointers and Linked Lists
Arrays versus ArrayList
Stacks: Implemented using Linked Lists
Stack A data structure in which elements are inserted and removed only at one end (called the top). Enforces Last-In-First-Out (LIFO) Uses of Stacks Evaluating.
Chapter 4 Queues.
Stacks CS-240 Dick Steflik.
Lists.
Chapter 5 Stack (part 1).
CSCS-200 Data Structure and Algorithms
Stacks.
Lecture 9: Stack and Queue
Binary Tree Iterators Tree Traversals: preorder, inorder, postorder
Presentation transcript:

IndexedListWithIteratorsViaLinear1Ind An extension of the IndexedListViaLinear1Ind class with two iterators, frontToRear() and rearToFront().

Writing the Code The code itself seems easy enough to write, it simply involves looking at each object in a list starting either from the front or the back and adding the option to remove the last object looked at. Two variables are used to accomplish this, previous and next, which hold the values of the last and current object respectively.

FrontToRear Iterator Constructor The boolean “iterating” is set to true, as the process has now begun. “previous” is a Linear1Ind structure and is given a value of null as to ensure that the remove() method is not called before the next() method is called. “current” is a Linear1Ind structure and is assigned as the list that you wish to run the iteration process on.

FrontToRear Iterator Methods hasNext() next() remove() Returns a value of true while “current” does not point to a null link. The value of “previous” is set to the value of “current.” “current” is set equal to the next link in the list you are iterating. The data of the last object looked at is returned. The current pointer in “previous” is set to the next one. “previous” is then set equal to null to ensure that the remove() method is not called again before the next() method.

RearToFront Iterator Constructor The boolean “iterating” is set to true, as the process has now begun. “current” is a StackViaLinear1Ind structure. It is filled with a for-loop that starts at the beginning of the Indexed List you wish to iterate and proceeds to push() all the objects it contains. “previous” is assigned a null value to ensure that the remove() method is not called before the next() method.

RearToFront Methods hasNext() next() remove() This returns a value of true while “current” is not empty. The item at the top of “current” is popped and assigned to “previous.” The data of the last object looked at is returned. The current pointer in “previous” is set to the next one. “previous” is then set equal to null to ensure that the remove() method is not called again before the next() method.

The Driver Two IndexedListWithIteratorsViaLinear1Ind’s are created, each holding 15 items. One list is iterated with the frontToRear() method and the other with the rearToFront() method. In both cases, any occurrences of the number zero are removed to ensure that the remove() method works. The lists are printed both before and after the iterations to make sure that the process has been completed successfully.

Conclusion Overall, the implementation of an iterator in the IndexedListViaLinear1Ind class does not seem to be too much of a problem as far as coding is concerned. The timing of the frontToRear() method seems to be fairly good as its linear. However, the timing in the rearToFront() method suffers somewhat as a stack had to be used to properly implement it.