Pastebin launched a little side project called HostCabi.net, check it out ;-)Don't like ads? PRO users don't see any ads ;-)
Guest

Node.h, Node.cpp, Main.cpp

By: a guest on Oct 13th, 2012  |  syntax: C++  |  size: 1.44 KB  |  hits: 33  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Node.cpp
  2. #include "Node.h"
  3.  
  4. using namespace LinkedListOfClasses;
  5.  
  6. void head_insert(NodePtr &head, int the_number) {
  7.    
  8.     NodePtr temp_ptr;
  9.     temp_ptr = new Node(the_number, head);
  10.     head = temp_ptr;
  11.    
  12. }
  13.  
  14. Node::Node(int value, Node *next) {  
  15. }
  16.  
  17. void Node::setData(int value) {
  18. }
  19.  
  20. void Node::setLink(Node *next) {
  21. }
  22.  
  23. Node.h
  24. #include <iostream>
  25. using namespace std;
  26.  
  27. namespace LinkedListOfClasses {
  28.     class Node {
  29.        
  30.     public:
  31.         Node();
  32.         Node(int value, Node *next);
  33.         int getData() const;
  34.         Node *getLink() const;
  35.         void setData(int value);
  36.         void setLink(Node *next);
  37.     private:
  38.         int data;
  39.         Node *link;
  40.     };
  41.     typedef Node* NodePtr;
  42. }
  43.  
  44. main.cpp
  45. #include <iostream>
  46. #include "Node.h"
  47. using namespace std;
  48. using namespace LinkedListOfClasses;
  49.  
  50. void head_insert(NodePtr &head, int the_number) {
  51.    
  52.     NodePtr temp_ptr;
  53.     temp_ptr = new Node(the_number, head);
  54.     head = temp_ptr;
  55.    
  56. }
  57.  
  58. int main() {
  59.    
  60.     NodePtr head, temp;
  61.     head = new Node(0, NULL);
  62.     for (int i = 1; i < 5; i++) {
  63.         head_insert(head, i);
  64.     }
  65.     temp = head;
  66.     while (temp !=NULL) {
  67.         cout << temp->getData() << endl;
  68.         temp = temp->getLink();
  69.     }
  70.     temp = head;
  71.     while (temp !=NULL) {
  72.         NodePtr nodeToDelete = temp;
  73.         temp = temp->getLink();
  74.         delete nodeToDelete;
  75.     }
  76.     return 0;
  77. }