http://www.c-jump.com/bcc/c255c/c255syllabus.htmThe purpose of this assignment is to use linked list of dynamically allocated nodes.
In this assignment you will re-write the previous version of dynamic stack of integers. Your task this time is to employ singly-linked list of dynamically allocated nodes:
The following code demonstrates dynamic allocation and de-allocation of memory by a singly-linked list (download singly_linked_sample.cpp and Node.h.)
#include "Node.h"
int main( )
{
Node head;
// Insert nodes into the list:
head.insert( new Node( 123 ) );
head.insert( new Node( 456 ) );
print_list( head ); // display nodes
// Remove all dynamic nodes from the list:
Node* iter = head.pnext;
while ( iter != NULL ) {
Node* obsolete = iter;
iter = iter->pnext;
delete obsolete; // remove node from memory
}
// Safe programming practice:
// indicates that no dynamic nodes are present
head.pnext = NULL;
return 0;
}
The new version of the stack should support the following interface:
class Stack {
public:
// Push new element to the stack:
void push( int value );
// Pop element from the stack:
void pop();
// Writeable access to element on top of the stack:
int& top();
// Read-only access to element on top of the stack:
int top() const;
// Return stack size:
size_t size() const;
// Is stack empty?
bool is_empty();
};//class Stack
Test your code against this version of the main driver: singly_linked_stack_main.cpp.
Add global function
void print_stack( Stack const* pstack );
to print all stack elements.
Note that class Stack should act as an adapter of the class Node:
| Stack member | Node member |
|---|---|
| push( ) | insert( ) |
| pop( ) | remove_next( ) |
| top( ) | Node::data |
| size( ) | size( ) |
| is_empty( ) | size( ) == 0 |
Therefore, the Stack should have a Node data member, and, whenever possible, delegate all work to the corresponding functions of the Node class:
class Stack {
Node head;
public:
// Push new element to the stack:
void push( int value )
{
head.insert( new Node( value ) );
}
//...
};//class Stack
When ready, submit your source files via e-mail attachment sent to:
Igor.Kholodov@bristolcc.edu
Please ZIP multiple files into a single archive.
Thank you, and good luck!