Advertisement
bobett1234

node.h

Aug 31st, 2013
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. namespace List {
  6.  
  7. class Node
  8. {
  9. public:
  10. Node();
  11. Node(int value, Node *next);
  12. // Constructor to initialize a Node
  13.  
  14. int getData( ) const;
  15. // Retrieve datat for Node
  16.  
  17. Node *getLink( ) const;
  18. // Retrieve next Node
  19.  
  20. void setData(int value);
  21. // modify the vaule in that Node
  22.  
  23. void setLink(Node *next);
  24. // change reference to the next Node
  25.  
  26. private:
  27. int data;
  28. Node *link;
  29. };
  30.  
  31. typedef Node* NodePtr;
  32.  
  33. Node::Node() : data(0), link(NULL)
  34. {
  35.  
  36. }
  37.  
  38. Node::Node(int value, Node *next) : data(value), link(next)
  39. {
  40.  
  41. }
  42.  
  43. //accessor and mutators:
  44.  
  45. int Node::getData( ) const
  46. {
  47. return data;
  48. }
  49.  
  50. Node* Node::getLink( ) const
  51. {
  52. return link;
  53. }
  54.  
  55. void Node::setData(int value)
  56. {
  57. data = value;
  58. }
  59.  
  60. void Node::setLink(Node *next)
  61. {
  62. link = next;
  63. }
  64.  
  65. class list
  66. {
  67. public:
  68. list();
  69. list(NodePtr& node);
  70. void head_insert(int number);
  71.  
  72. Node *node();
  73.  
  74. private:
  75. Node *head;
  76. };
  77.  
  78. list::list()
  79. {
  80. head = new Node(0, NULL);
  81. }
  82.  
  83. list::list(NodePtr& node)
  84. {
  85. head = node;
  86. }
  87.  
  88. Node* list::node()
  89. {
  90. return head;
  91. }
  92.  
  93. void list::head_insert(int number)
  94. {
  95. NodePtr temp;
  96. temp = new Node(number, head);
  97. head = temp;
  98. }
  99.  
  100. ostream& operator <<(ostream& out, list& node)
  101. {
  102. NodePtr tmp;
  103. tmp = node.node();
  104. while(tmp != NULL)
  105. {
  106. cout << tmp->getData() << endl;
  107. tmp = tmp->getLink();
  108. }
  109. return out;
  110. }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement