Advertisement
Falmung

DoublyListNode.h+main.cpp

Oct 26th, 2011
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.52 KB | None | 0 0
  1. // DoublyListNode.h
  2. #pragma once
  3. template <class T > class DoublyList;
  4. template<class T >
  5. class DoublyListNode
  6. {
  7.     friend class DoublyList< T >; // Make Double List a friend
  8. public:
  9.     DoublyListNode( const T &);
  10.     T getData() const;
  11. private:
  12.     T data;
  13.     DoublyListNode< T > *nextPtr;
  14.     DoublyListNode < T > *prePtr; // precede
  15. }; // end class DoublyListNode constructor
  16.  
  17. template < class T >
  18. DoublyListNode< T > :: DoublyListNode( const T &info) : data(info), nextPtr(NULL), prePtr(NULL)
  19. {
  20.     // empty body
  21. } // end DoublyListNode constructor
  22.  
  23. template< class T >
  24. T DoublyListNode< T > :: getData() const
  25. {
  26.     return data;
  27. }
  28.  
  29.  
  30. // main.cpp DoublyList
  31.  
  32. using namespace std;
  33. #include "DoublyList.h"
  34. #include "DoublyListNode.h"
  35.  
  36. int menu();
  37.  
  38. int main()
  39. {
  40.         //cout << "Hello World\n";
  41.         DoublyList< int > integerList;
  42.  
  43.         int index = 1, opt;
  44.         int value;
  45.        
  46.         do {
  47.                 opt = menu();
  48.                 switch(opt) {
  49.                 case 1:
  50.                         cout << "Entre un numero: ";
  51.                         cin >> value;
  52.                         integerList.insert(1, value);
  53.                         integerList.print();
  54.                         break;
  55.                 case 2:
  56.                         cout << "Final Enter a number: ";
  57.                         cin >> value;
  58.                         index = integerList.getLength();
  59.                         integerList.insert(index+1, value);
  60.                         integerList.print();
  61.                         break;
  62.                 case 3:
  63.                         cout << "Enter a number: ";
  64.                         cin >> value;
  65.                         cout << "Enter the index : ";
  66.                         cin >> index;
  67.                         integerList.insert(index, value);
  68.                         integerList.print();
  69.                         break;
  70.                 case 4:
  71.                         cout << "Enter the index: ";
  72.                         cin >> index;
  73.                         integerList.remove(index);
  74.                         integerList.print();
  75.                         break;
  76.                 case 5:
  77.                         cout << "View the content of the index: ";
  78.                         cin >> index;
  79.                         integerList.retrieve(index, value);
  80.                         cout << "Index: " << index << "Value: " << value << endl;
  81.                         integerList.print();
  82.                         break;
  83.                 case 6:
  84.                         integerList.print();
  85.                         break;
  86.                 case 7:
  87.                         DoublyList< int > newList( integerList);
  88.                         cout << "Original List\n";
  89.                         integerList.print();
  90.                         cout << "New List\n";
  91.                         newList.print();
  92.                 } // end switch
  93.  
  94.         } while( opt != 8);
  95.         return 0;
  96. } // end main
  97.  
  98. int menu()
  99. {
  100.         int opt;
  101.         cout << "Main Menu\n";
  102.         cout << "1.Add a node at the beginning of the list\n";
  103.         cout << "2. Add a node at the end of the list\n";
  104.         cout << "3.Add a node on the desired index of the list\n";
  105.         cout << "4.Eliminate a node on the desired position\n";
  106.         cout << "5.View the content of a node\n";
  107.         cout << "6.Print List\n";
  108.         cout << "7.Create another list aside from the one created\n";
  109.         cout << "8.Exit Program\n";
  110.         cout << "Enter an option:";
  111.         cin >> opt;
  112.         return opt;
  113. } // end menu
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement