Lab 04 – Linked List.

Slides:



Advertisements
Similar presentations
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Advertisements

CS 340Chapter 3: Lists, Stacks, and Queues1 Abstract Data Types An ADT is a set of operations upon a set of data. Implementation details are not specified.
Classes, Objects, Arrays, Collections and Autoboxing Dr. Andrew Wallace PhD BEng(hons) EurIng
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Custom Templatized Data Structures.
Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
Separate Compilation Bryce Boe 2013/10/09 CS24, Fall 2013.
List Interface and Linked List Mrs. Furman March 25, 2010.
1 Chapter 3 Lists, Stacks, and Queues Reading: Sections 3.1, 3.2, 3.3, 3.4 Abstract Data Types (ADT) Iterators Implementation of Vector.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 18 List ADT Animated Version.
2.4 Exceptions n Detects try { //code that may raise an exception and/or set some condition if (condition) throw exceptionName; //Freq. A string } n Handles.
CS212: Object Oriented Analysis and Design
C++ Lesson 1.
CS 215 Final Review Ismail abumuhfouz Fall 2014.
John Hurley Cal State LA
Exceptions, Templates, and the Standard Template Library (STL)
Doubly Linked List Review - We are writing this code
Homework 4 questions???.
C++ Object-Oriented Programming
classes and objects review
Programming Abstractions
Friday, January 26, 2018 Announcements… For Today… For Next Time…
Linked lists Motivation: we can make arrays, but their functionality is slightly limited and can be difficult to work with Biggest issue: size management.
LinkedList Class.
Monday, March 19, 2018 Announcements… For Today… For Next Time…
– Introduction to Object Technology
Friday, February 16, 2018 Announcements… For Today… 4.5-, pgs. 252
Wednesday, February 14, 2018 Announcements… For Today…
8.1 Tree Terminology and Applications
Lab 08 - BST.
Lab 10 - Quicksort.
10.8 Heapsort 10.9 Quicksort Chapter 10 - Sorting.
C++ Templates L03 - Iterator 10 – Iterator.
10 – Iterators C++ Templates 4.6 The Iterator pgs
Wednesday, April 11, 2018 Announcements… For Today…
Object Oriented Programming COP3330 / CGS5409
3.3 Abstract Classes, Assignment, and Casting in a Hierarchy
Lab 03 - Iterator.
Chapter 18: Linked Lists.
Lab 05 – Expressions.
Conditional Statements
Lecture 20 Linked Lists Richard Gesick.
14 – Sequential Containers
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Programming Abstractions
Doubly Linked List Implementation
C++ Tutorial Rob Jagnow
Exceptions 1 CMSC 202.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Standard Version of Starting Out with C++, 4th Edition
Exceptions, Templates, and the Standard Template Library (STL)
C++ Templates L03 - Iterator 10 – Iterator.
Templates and Iterators
Jordi Cortadella and Jordi Petit Department of Computer Science
C++ Templates L03 - Iterator 10 – Iterator.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Programming II (CS300) Chapter 07: Linked Lists
Doubly Linked List Implementation
Linked Lists Templates and Iterators
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Chapter 3 Lists, Stacks, and Queues
CMSC 202 Lesson 20 Exceptions 1.
14 – Sequential Containers
Lab 03 – Linked List.
The List Container and Iterators
16 – Sequential Containers
8.1 Tree Terminology and Applications
SPL – PS1 Introduction to C++.
Lab 04 - Iterator.
Data Structures & Programming
Data Structures & Programming
Presentation transcript:

Lab 04 – Linked List

Sequence Classes L04 - Linked List A sequence has the property that elements can be inserted or removed anywhere in the sequence, not just at the beginning or at the end. Some sequences are indexed, which means their elements can be accessed in arbitrary order (called random access) using a subscript to select an element. For other sequences you must start at the beginning and process the elements in order. Iterators facilitate sequential access and random access. The vector and list (linked list) classes and their similarities and differences . These classes implement the common interface requirements for sequential containers.

C++ Templates L04 - Linked List Templates are a feature of the C++ programming language that allows functions and classes to operate with generic types. This allows a function or class to work on many different data types without being rewritten for each one. class MyPair_int { int values [2]; public: MyPair(int first, int second) values[0]=first; values[1]=second; } }; MyPair_int myInts(115, 36); template <typename T> class MyPair { T values [2]; public: MyPair(T first, T second) values[0]=first; values[1]=second; } }; MyPair<int> myInts(115, 36); MyPair<double> myFloats(3.0, 2.18); class MyPair_double { double values [2]; public: MyPair(double first, double second) values[0]=first; values[1]=second; } }; MyPair_double myFloats(3.0, 2.18);

C++ Interface Templates L04 - Linked List class Interface { public: Interface(){} virtual ~Interface(){} virtual void myMethod() = 0; // pure virtual method }; class MyClass : public Interface private: int myMember; MyClass(){} ~MyClass(){} virtual void myMethod() // myMethod implementation int main(void) Interface* myClass = new MyClass(); myClass->myMethod(); delete myClass; return 0; } MyClass Interface

Linked List Interface Template L04 - Linked List //**YOU MAY NOT MODIFY THIS DOCUMENT** #ifndef LINKED_LIST_INTERFACE_H #define LINKED_LIST_INTERFACE_H template<typename T> class LinkedListInterface { public: LinkedListInterface(void) {}; virtual ~LinkedListInterface(void) {}; /** Insert Node at beginning of linked list (no duplicates) */ virtual bool insertHead(T value) = 0; /** Insert Node at end of linked list (no duplicates) */ virtual bool insertTail(T value) = 0; /** Insert node after matchNode (no duplicates) */ virtual bool insertAfter(T matchNode, T node) = 0; /** Remove Node from linked list */ virtual bool remove(T value) = 0; /** Remove all Nodes from linked list */ virtual bool clear() = 0; /** Return Node at index (0 based) */ virtual T at(int index) = 0; /** Returns the number of nodes in the linked list */ virtual int size() const = 0; }; #endif // LINKED_LIST_INTERFACE_H #ifndef LINKED_LIST_H #define LINKED_LIST_H using std::string; using std::ostream; /** Linked List */ template<typename T> class LinkedList : public LinkedListInterface<T> { private: struct Node T data; Node* next; Node(const T& d) : data(d), next(NULL) {} Node(const T& d, Node* n) : data(d), next(n) {} }; Node* head; public: LinkedList() { this->head = NULL; } ~LinkedList() { clear(); } /** Insert Node at beginning of list (no duplcates) */ virtual bool insertHead(T value) { /*...*/ } /** Insert Node at end of list (no duplcates) */ virtual bool insertTail(T value) { /*...*/ } /* ... */ #endif // LINKED_LIST_H

Lab 04 – Linked List INT STRING insertHead <data> L04 - Linked List COMMAND DESCRIPTION OUTPUT INT Instantiates a LinkedList<int> object for subsequent commands. true or false STRING Instantiates a LinkedList<string> object for subsequent commands. insertHead <data> Insert <data> at the head of the list. insertTail <data> Insert <data> at the tail of the list. insertAfter <match>,<data> Insert <data> after <match> found in the list. remove <data> Remove <data> from the list. at <index> Return Node data from list Node at <index> (0 based). Return default type if out of bounds. data or T() (bonus) throw error size Return the number of Nodes in the list. # of Nodes clear Delete all Nodes in the list. true printList Output Nodes in the list. Node0 Node1...

Lab 04 – Linked List Step 1 - Begin with a main function. L04 - Linked List Step 1 - Begin with a main function. Open for input argv[1] and for output argv[2]. Add code to read and output input file. Parse input commands and output corresponding messages. Step 2 - Design and implement a LinkedList template class. Create a LinkedList template class that inherits from LinkedListInterface class. Since LinkedListInterface is an abstract class, stub out all virtual functions so that there are no compiler errors. Include both .h files in your main file. Implement one function at a time and then test it before moving on to the next one. Try implementing more basic functions first, like toString and insertHead. Create your own test case files to aid in your incremental testing. Step 3 - When you've completed the incremental testing, test your program with the provided test cases.

Lab 04 – Linked List Points Requirement L04 - Linked List Points Requirement 4 argv[1] and argv[2] used for input / output streams respectively. No execution interaction (i.e. system("pause"); or getchr();). 6 LinkedList is a template class and is derived from the pure abstract template class LinkedListInterface. Basic linked list commands insertHead, insertTail, and printList are correctly implemented for types int and string. (lab04_in_01.txt). Linked list insertAfter and size commands are correctly implemented for types int and string. (lab04_in_02.txt). Linked list remove and clear commands are correctly implemented for types int and string. (lab04_in_03.txt). Linked list at index command is correctly implemented for types int and string. (lab04_in_04.txt). VS_MEM_CHECK macro is included in main to detect memory leaks. No Memory leaks are reported. 5 (BONUS) Linked list at index command throws an "Invalid Index" object when the index is less than zero or greater than the number of nodes. A try-block in main catches and reports the error. Processing the input continues. (BONUS) The printList command uses a nested iterator object to output the contents of the LinkedList container.