06.08.2013 Views

Laboratory Exercises, C++ Programming

Laboratory Exercises, C++ Programming

Laboratory Exercises, C++ Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

The GNU Compiler Collection and <strong>C++</strong> 7<br />

namespace cpp_lab1 {<br />

/* List is a list of long integers */<br />

class List {<br />

public:<br />

/* create an empty list */<br />

List();<br />

};<br />

/* destroy this list */<br />

~List();<br />

/* insert d into this list as the first element */<br />

void insert(long d);<br />

/* remove the first element less than/equal to/greater than d,<br />

depending on the value of df. Do nothing if there is no<br />

value to remove. The public constants may be accessed with<br />

List::LESS, List::EQUAL, List::GREATER outside the class */<br />

enum DeleteFlag { LESS, EQUAL, GREATER };<br />

void remove(long d, DeleteFlag df = EQUAL);<br />

/* returns the size of the list */<br />

int size() const;<br />

/* returns true if the list is empty */<br />

bool empty() const;<br />

/* returns the value of the largest number in the list */<br />

long largest() const;<br />

/* print the contents of the list (for debugging) */<br />

void debugPrint() const;<br />

private:<br />

/* a list node */<br />

struct Node {<br />

long value; // the node value<br />

Node* next; // pointer to the next node, 0 in the last node<br />

Node(long value = 0, Node* next = 0);<br />

};<br />

};<br />

Node* head; // the pointer to the list head, which contains<br />

// a pointer to the first list node<br />

/* forbid copying of lists */<br />

List(const List&);<br />

List& operator=(const List&);<br />

Notes: Node is a struct, i.e., a class where the members are public by default. This is<br />

not dangerous, since Node is private to the class. The copy constructor and assignment<br />

operator are private, so you cannot copy lists.<br />

The file list.h contains the class definition and list.cc contains a skeleton of the class<br />

implementation. Complete the implementation in accordance with the specification.<br />

Also implement, in a file ltest.cc, a test program that checks that your List implementation<br />

is correct. Be careful to check exceptional cases, such as removing the first or the last<br />

element in the list.<br />

Type make to build the program. Note that you will get warnings about unused<br />

parameters when the implementation skeleton is compiled. These warnings will disappear

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!