Lab05. //-------------------------------------------------------------------- // // Laboratory 5...

4
CS 302 Data Structures Lab05

Transcript of Lab05. //-------------------------------------------------------------------- // // Laboratory 5...

Page 1: Lab05. //-------------------------------------------------------------------- // // Laboratory 5 ListLinked.h // // Class declaration for the linked implementation.

CS 302 Data StructuresLab05

Page 2: Lab05. //-------------------------------------------------------------------- // // Laboratory 5 ListLinked.h // // Class declaration for the linked implementation.

//--------------------------------------------------------------------//// Laboratory 5 ListLinked.h//// Class declaration for the linked implementation of the List ADT////--------------------------------------------------------------------

#ifndef LISTLINKED_H#define LISTLINKED_H

#include <stdexcept>#include <iostream>

using namespace std;

template <typename DataType>class List { public: List(int ignored = 0); List(const List& other); List& operator=(const List& other); ~List();

void insert(const DataType& newDataItem) throw (logic_error); void remove() throw (logic_error); void replace(const DataType& newDataItem) throw (logic_error); void clear();

bool isEmpty() const; bool isFull() const;

void gotoBeginning() throw (logic_error); void gotoEnd() throw (logic_error); bool gotoNext() throw (logic_error); bool gotoPrior() throw (logic_error);

DataType getCursor() const throw (logic_error);

// Programming exercise 2 void moveToBeginning () throw (logic_error);

// Programming exercise 3 void insertBefore(const DataType& newDataItem) throw (logic_error); void showStructure() const;

private: class ListNode { public: ListNode(const DataType& nodeData, ListNode* nextPtr);

DataType dataItem; ListNode* next; };

ListNode* head; ListNode* cursor;

};

#endif

Page 3: Lab05. //-------------------------------------------------------------------- // // Laboratory 5 ListLinked.h // // Class declaration for the linked implementation.

template <typename DataType>List<DataType>::ListNode::ListNode(const DataType& nodeData, ListNode* nextPtr)// Creates a list node containing item elem and next pointer// nextPtr.

: dataItem(nodeData), next(nextPtr){}

Page 4: Lab05. //-------------------------------------------------------------------- // // Laboratory 5 ListLinked.h // // Class declaration for the linked implementation.

Allocating a node

• head = new ListNode(newDataItem, NULL);• Note: No template necessary in this allocation statement• Lab Book (page 63) has the template and it should not.