Hello, this is my first post here so please let me know if I'm doing something wrong.

I have a linked list project that I'm having a little trouble with.
I was given LinkedList.cpp (main) and that can't be changed. I had to use a completed program that we did already and split it up into different files using Node.h, Node.cpp, List.h, and List.cpp.

1) Please check for correctness: I have to place all List and Node functionality in the custom namespace called CIS_CSC_275.
2) I'm not sure how to do this: Add a new memeber function to the List class called unappend, which removes the last element.

Any help would be greatly appreciated. Thanks!

LinkedList.cpp

#include <iostream>
#include "List.h"
using namespace std;

int main(void)
{
    using namespace CIS_CSC_275;

    List l;

    l.print();

    // Add 3 nodes, printing after each.
    l.append(1); l.print();
    l.append(2); l.print();
    l.append(3); l.print();

    // Remove 3 nodes, printing after each.
    l.unappend(); l.print();
    l.unappend(); l.print();
    l.unappend(); l.print();

    return(0);
}

List.h

#ifndef LIST_H
#define LIST_H
#include <iostream>
#include "Node.h"

namespace CIS_CSC_275{

class List {
public:
    List();
    void append(int new_data);
    void print();
private:
    Node *head;
};
}
#endif

List.cpp

#include <iostream>
#include "list.h"
using namespace std;

using namespace CIS_CSC_275;

List::List(){
    head = new Node;
}

void List::append(int new_data)
    {
        Node *next = new Node(new_data);
        Node *curr = head;
        while (curr->getNext() != NULL)
        {
            curr = curr->getNext();
        }
        curr->setNext(next);
    }


void List::print() {
        Node *curr = head;
        curr->print();
        while (curr->getNext() != NULL) {
            curr = curr->getNext();
            curr->print();
        }
    }

Node.h

#ifndef NODE_H
#define NODE_H

namespace CIS_CSC_275{

class Node {
public:
    Node();
    Node(int new_data);
    void print();
    int getData();
    void setData(int new_data);
    Node* getNext();
    void setNext(Node* new_next);

private:
    int data;
    Node *next;
};
}

#endif

Node.cpp

#include "node.h"
#include <iostream>
using namespace std;

using namespace CIS_CSC_275;

Node::Node() {
        data = 0;
        next = NULL;
    }

Node::Node(int new_data)
    {
        data = new_data;
        next = NULL;
    }

void Node::print()
    {
        cout << "[data=" << data << "|next=" << next << "]" << endl;
    }

int Node::getData()
    {
        return data;
    }

void Node::setData(int new_data)
    {
        data = new_data;
    }

Node* Node::getNext()
    {
        return next;
    }

void Node::setNext(Node* new_next)
    {
        next = new_next;
    }

Solved elsewhere

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.