Lab 03 – Linked List.

Slides:



Advertisements
Similar presentations
DATA STRUCTURES USING C++ Chapter 5
Advertisements

Linked Lists Linear collections.
Chapter 3 – Lists A list is just what the name implies, a finite, ordered sequence of items. Order indicates each item has a position. A list of size 0.
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.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
1 Data Structures CSCI 132, Spring 2014 Lecture 20 Linked Lists.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department.
Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
List Interface and Linked List Mrs. Furman March 25, 2010.
The List ADT Reading: Sections 3.2, 3.3, 3.5.
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.
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.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 18: Stacks and Queues.
C++ Programming:. Program Design Including
Andy Wang Object Oriented Programming in C++ COP 3330
Cpt S 122 – Data Structures Abstract Data Types
18 Chapter Stacks and Queues
Chapter 18: Stacks and Queues.
Chapter 4 The easy stuff.
Doubly Linked List Review - We are writing this code
Homework 4 questions???.
Programming Abstractions
A Doubly Linked List There’s the need to access a list in reverse order prev next data dnode header 1.
LinkedList Class.
Monday, March 19, 2018 Announcements… For Today… For Next Time…
Pointers and Dynamic Variables
Friday, February 16, 2018 Announcements… For Today… 4.5-, pgs. 252
Wednesday, February 14, 2018 Announcements… For Today…
Lab 06 – Railroad.
Chapter 6 – Queues and Deques
8.1 Tree Terminology and Applications
Lab 08 - BST.
Lab 10 - Quicksort.
10.8 Heapsort 10.9 Quicksort Chapter 10 - Sorting.
10 – Iterators C++ Templates 4.6 The Iterator pgs
Stacks.
Linked Lists.
Lab 04 – Linked List.
Chapter 19: Stacks and Queues.
Wednesday, April 11, 2018 Announcements… For Today…
Object Oriented Programming COP3330 / CGS5409
5.4 Additional Stack Applications
Lab 03 - Iterator.
Chapter 18: Linked Lists.
Lab 05 – Expressions.
Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors
Andy Wang Object Oriented Programming in C++ COP 3330
Programming Abstractions
Doubly Linked List Implementation
Exceptions 1 CMSC 202.
Lists - I The List ADT.
Lists - I The List ADT.
Queues Jyh-Shing Roger Jang (張智星)
Templates and Iterators
Lists.
General List.
Building Java Programs
Doubly Linked List Implementation
Chapter 3 Lists, Stacks, and Queues
CMSC 202 Lesson 20 Exceptions 1.
The List Container and Iterators
Abstract Data Types Stacks CSCI 240
16 – Sequential Containers
8.1 Tree Terminology and Applications
5.3 Implementing a Stack Chapter 5 – The Stack.
Lab 04 - Iterator.
5.1 The Stack Abstract Data Type
Data Structures & Programming
Presentation transcript:

Lab 03 – Linked List

Sequence Classes L03 - 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++ Interface Templates L03 - 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 L03 - Linked List //**** YOU MAY NOT MODIFY THIS DOCUMENT ****/ #ifndef LINKED_LIST_INTERFACE_H #define LINKED_LIST_INTERFACE_H #include <string> template<typename T> class LinkedListInterface { public: LinkedListInterface(void) {} virtual ~LinkedListInterface(void) {} /** Insert Node at beginning of linked list */ virtual void push_front(const T& value) = 0; /** Remove Node at beginning of linked list */ virtual void pop_front(void) = 0; /** Return Node at beginning of linked list */ virtual T& front(void) = 0; /** Return true if linked list size == 0 */ virtual bool empty(void) const = 0; /** Remove all Nodes with value from linked list */ virtual void remove(const T& value) = 0; /** Remove all Nodes from linked list */ virtual void clear(void) = 0; /** Reverse Nodes in linked list */ virtual void reverse(void) = 0; /** Return the number of nodes in the linked list */ virtual size_t size(void) const = 0; /** Return contents of Linked List as a string */ virtual std::string toString(void) 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 */ virtual void push_front(T& value) { /*...*/ } /** Remove Node at beginning of linked list */ virtual bool pop_front(void) { /*...*/ } /* ... */ #endif // LINKED_LIST_H

Lab 03 – Linked List Clear Empty Delete First Insert <data>... L03 - Linked List COMMAND DESCRIPTION EXAMPLE Clear Delete all items in the linked list. void clear(void); Clear OK Size 0 Empty Output true if linked list empty, else false. bool empty(void); Empty false Empty true Delete Delete the first item in the linked list. Throw an error "Empty" if linked list is empty. void pop_front(void); PrintList The quick brown fox Delete OK PrintList quick brown fox Delete Empty! First Output the first item in the linked list. Throw an error "Empty" if linked list is empty. T& front(void); First The Insert <data>... Insert item(s) at the head of the linked list void push_front(const T& value) ; Insert The very quick brown fox Insert jumped over the very lazy dog. PrintList Output the contents of the linked list, space separated. Output "Empty!" if list is empty. string toString(void) const; PrintList fox brown quick very The PrintList Empty! Remove <data> Remove all like items from the linked list. Throw an error "Empty" if linked list is empty. void remove(const T& value); PrintList The very very quick brown fox Remove very Reverse Reverse the items in the linked list. Throw an error "Empty" if linked list is empty. void reverse(void); Reverse OK PrintList The very quick brown fox Size Return the number of Nodes in the list. size_t size(void) const; Size 11

Lab 03 – Linked List Step 1 - Begin with a main function. L03 - 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 Insert. 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 03 – Linked List Points Requirement L03 - Linked List Points Requirement 10 Basic linked list commands Insert, Clear, and PrintList commands are correctly implemented. (lab03_in_01.txt). Linked list Empty and Size commands are correctly implemented. (lab03_in_02.txt). Linked list Delete, First, and Remove commands are correctly implemented. (lab03_in_03.txt). Linked list Reverse command is correctly implemented. (lab03_in_04.txt). An error is thrown ("Empty!") when front(), pop_front(), or reverse() linked list methods are called with empty lists. A try-block in main catches and reports the error. Processing of input commands continues. (lab03_in_05.txt). -10 Memory leaks, g++ compiler warnings, array out-of-bounds detected, or execution interactions (i.e. system("pause"); or getchr();) used. 2 LinkedList is a template class and is derived from the pure abstract template class LinkedListInterface. A try-block in main catches and reports errors such as accesses to empty lists. All derived class objects have a public insertion (<<) operator.

Lab 03 – Linked List L03 - Linked List

Lab 03 – Linked List L03 - Linked List