CIS-255 Home Page: http://www.c-jump.com/bcc/c255c/c255syllabus.htm

Project 1/Part 3: Dynamic stack of integers using linked list


Description

The purpose of this assignment is to use linked list of dynamically allocated nodes.

Instructions

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

    singly-linked list

  2. 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;
    }
    
    
  3. 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
    
    
    
  4. Test your code against this version of the main driver: singly_linked_stack_main.cpp.

  5. Add global function

    
        void print_stack( Stack const* pstack );
    
    

    to print all stack elements.

  6. 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
    
    
  7. When ready, log into CIS-255 online website, follow the link to Submit Homework, and select Project 1/Part 3 to upload your source files. Alternatively, you may zip the folder with all of your source files and upload a single ZIP archive.

    PLEASE DO NOT send project, EXE, or other MSVC artifacts on your system. I only need your C++ source files to grade the assignment.