Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lab 04 – Linked List.

Similar presentations


Presentation on theme: "Lab 04 – Linked List."— Presentation transcript:

1 Lab 04 – Linked List

2 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.

3 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);

4 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

5 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

6 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...

7 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.

8 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.

9


Download ppt "Lab 04 – Linked List."

Similar presentations


Ads by Google