Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- namespace List {
- class Node
- {
- public:
- Node();
- Node(int value, Node *next);
- // Constructor to initialize a Node
- int getData( ) const;
- // Retrieve datat for Node
- Node *getLink( ) const;
- // Retrieve next Node
- void setData(int value);
- // modify the vaule in that Node
- void setLink(Node *next);
- // change reference to the next Node
- private:
- int data;
- Node *link;
- };
- typedef Node* NodePtr;
- Node::Node() : data(0), link(NULL)
- {
- }
- Node::Node(int value, Node *next) : data(value), link(next)
- {
- }
- //accessor and mutators:
- int Node::getData( ) const
- {
- return data;
- }
- Node* Node::getLink( ) const
- {
- return link;
- }
- void Node::setData(int value)
- {
- data = value;
- }
- void Node::setLink(Node *next)
- {
- link = next;
- }
- class list
- {
- public:
- list();
- list(NodePtr& node);
- void head_insert(int number);
- Node *node();
- private:
- Node *head;
- };
- list::list()
- {
- head = new Node(0, NULL);
- }
- list::list(NodePtr& node)
- {
- head = node;
- }
- Node* list::node()
- {
- return head;
- }
- void list::head_insert(int number)
- {
- NodePtr temp;
- temp = new Node(number, head);
- head = temp;
- }
- ostream& operator <<(ostream& out, list& node)
- {
- NodePtr tmp;
- tmp = node.node();
- while(tmp != NULL)
- {
- cout << tmp->getData() << endl;
- tmp = tmp->getLink();
- }
- return out;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement