Section 2.6 Linked List StringLog ADT Implementation

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

Stacks, Queues, and Linked Lists
Data Structures ADT List
Lab 8 Ordered list. OVERVIEW In an ordered list the elements are maintained in ascending (or descending) order based on the data contained in the list.
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
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.
0 of 37 Stacks and Queues Lecture of 37 Abstract Data Types To use a method, need to know its essentials: signature and return type o additionally,
CHAPTER 7 Queues.
Main Index Contents 11 Main Index Contents Tree StructuresTree Structures (3 slides) Tree Structures Tree Node Level and Path Len. Tree Node Level and.
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.
CHAPTER 6 Stacks. 2 A stack is a linear collection whose elements are added and removed from one end The last element to be put on the stack is the first.
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.
Review 1 Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example.
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.
1 Stacks. 2 A stack has the property that the last item placed on the stack will be the first item removed Commonly referred to as last-in, first-out,
CSC 205 Programming II Lecture 18 The Eight Queens Problem.
1 Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
09-1 Queues and List-Based ADT Implementations Problem Set: PS3 due Wednesday, March 7 Wellesley College CS230 Lecture 09 Monday, February 26 Handout #18.
ليست هاي پيوندي Linked Lists ساختمان داده ها و الگوريتم ها.
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.
1 Data Structures CSCI 132, Spring 2014 Lecture 20 Linked Lists.
(c) University of Washington16-1 CSC 143 Java Linked Lists Reading: Ch. 20.
U n i v e r s i t y o f H a i l 1 ICS 202  2011 spring  Data Structures and Algorithms 
Lab 6 Stack ADT. OVERVIEW The stack is one example of a constrained linear data structure. In a stack, the elements are ordered from most recently added.
Week 2 - Friday.  What did we talk about last time?  Computing Big Oh.
Lecture 20 Stacks and Queues. Stacks, Queues and Priority Queues Stacks – first-in-last-out structure – used for function evaluation (run-time stack)
What is a List? A list is a homogeneous collection of elements, with a linear relationship between elements. Each list element (except the first) has a.
Chapter 2 Abstract Data Types. Chapter 2: Abstract Data Types 2.1 – Abstraction 2.2 – The StringLog ADT Specification 2.3 – Array-Based StringLog ADT.
Section 2.3 Array-Based StringLog ADT Implementation.
Chapter 5 The Queue ADT. 5.1 Queues Queue A structure in which elements are added to the rear and removed from the front; a “first in, first out” (FIFO)
Section 2.2 The StringLog ADT Specification. 2.2 The StringLog ADT Specification The primary responsibility of the StringLog ADT is to remember all the.
1 CS162: Introduction to Computer Science II Abstract Data Types.
Section 3.7 Linked-Based Implementations
Chapter 3 The Stack ADT.
Sections 3.4 Formal Specification
Stack: a Linked Implementation
Section 2.5 Introduction to Linked Lists
Queues Rem Collier Room A1.02
LinkedIntList(int n) Write a constructor for LinkedIntList that accepts an int n parameter and makes a list of the number from 0 to n new LinkedIntList(3)
Software Development Java Classes and Methods
Stacks.
Linked Lists Damian Gordon.
Queue data structure.
Stacks and Queues.
Priority Queue.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
CMSC 341 Lecture 5 Stacks, Queues
Linked Lists, Stacks and Queues Textbook Sections
Queues.
Chapter 13 Collections.
Memory Organization Process 1 (browser) Code Process 3 (word)
Circular List removedNode General Case Single Node Case lastNode aNode
Data Structures ADT List
Data Structures ADT List
Programming II (CS300) Chapter 07: Linked Lists and Iterators
ADT list.
Podcast Ch18b Title: STree Class
Chapter 10 1 – Binary Trees Tree Structures (3 slides)
Chapter 16 Linked Structures
Queues CSC212.
Data Structures ADT List
ADT Queue (Array Implementation)
CSC 143 Java Linked Lists.
Chapter 2 The Stack ADT.
General List.
Programming II (CS300) Chapter 07: Linked Lists
Queues: Implemented using Linked Lists
Chapter 9 The Priority Queue ADT
slides created by Marty Stepp
Data Structures & Programming
Presentation transcript:

Section 2.6 Linked List StringLog ADT Implementation

2.6 Linked List StringLog ADT Implementation We call our new StringLog class the LinkedStringLog class, to differentiate it from the array-based class of Section 2.3. We also refer to this approach as a reference-based approach. The class fulfills the StringLog specification and implements the EnhancedStringLogInterface interface.

Instance Variables LLStringNode log; String name; In this implementation, the elements of a StringLog are stored in a linked list of LLStringNode objects. We call the instance variable that we use to access the strings log. It will reference the first node on the linked list, so it is a reference to an object of the class LLStringNode. String name; Recall that every StringLog must have a name. We call the needed variable name.

Instance variables and constructor public class LinkedStringLog implements StringLogInterface { protected LLStringNode log; // reference to first node of linked // list that holds the StringLog strings protected String name; // name of this StringLog public LinkedStringLog(String name) // Instantiates and returns a reference to an empty StringLog object // with name "name". log = null; this.name = name; }

The insert operation An example use: Insert the new string in the front: public void insert(String element) // Precondition: This StringLog is not full. // // Places element into this StringLog. { LLStringNode newNode = new LLStringNode(element); newNode.setLink(log); log = newNode; } An example use: LinkedStringLog strLog; strLog = new ArrayStringLog("aliases"); strLog.insert("Babyface"); String s1 = new String("Slim"); strLog.insert(s1);

Example use of insert (part 1)

Example use of insert (part 2)

Example use of insert (part 3)

The clear operation public void clear() // Makes this StringLog empty. { log = null; } The clear operation

Three Observers public boolean isFull() // Returns true if this StringLog is full, false otherwise. { return false; } public String getName() // Returns the name of this StringLog. return name; public int size() // Returns the number of Strings in this StringLog. int count = 0; LLStringNode node; node = log; while (node != null) count = count + 1; node = node.getLink(); return count;

The toString Observer public String toString() // Returns a nicely formatted string representing this StringLog. { String logString = "Log: " + name + "\n\n"; LLStringNode node; node = log; int count = 0; while (node != null) count = count + 1; logString = logString + count + ". " + node.getInfo() + "\n"; node = node.getLink(); } return logString; Note that size, toString, and contains (next slide) all use a form of a linked list traversal.

The contains method We reuse our design from the array-based approach, but use the linked list counterparts of each operation: public boolean contains(String element) { LLStringNode node; node = log; while (node != null) if (element.equalsIgnoreCase(node.getInfo())) // if they match return true; else node = node.getLink(); } return false;