1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
|
#include <iostream>
#include <sstream>
using namespace std;
template <class T> class List;
template <class T>
class Node {
private:
T data;
Node<T> next;
Node<T> previous;
public:
Node(T, Node<T>, Node<T>);
virtual ~Node();
T getData();
Node<T>* getNext();
Node<T>* getPrevious();
void setData(T);
void setNext(Node<T>*);
void setPrevious(Node<T>*);
};
template <class T>
Node<T>::Node(T info, Node<T> nx, Node<T> prv)
:data(info), next(nx), previous(prv) {}
template <class T>
Node<T>::~Node() {}
template <class T>
T Node<T>::getData() { return data; }
template <class T>
Node<T>* Node<T>::getNext() { return next; }
template<class T>
Node<T>* Node<T>::getPrevious() { return previous; }
template<class T>
void Node<T>::setData(T info) { data = info; }
template<class T>
void Node<T>::setNext(Node<T>* nx) { next = nx; }
template<class T>
void Node<T>::setPrevious(Node<T>* prv) { previous = prv; }
template<class T>
class List{
private:
Node<T>* head;
Node<T>* temporal;
Node<T>* tail;
int numberOfElements;
public:
List();
virtual ~List();
void add(const T&);
void remove(int);
bool empty();
int getNumberOfElements();
void printList();
};
template<class T>
List<T>::List():head(NULL),temporal(NULL),tail(NULL),numberOfElements(0) {}
template<class T>
List<T>::~List(){}
template<class T>
void List<T>::add(const T& info){
if (empty) {
head = new Node<T>(info, NULL, NULL);
numberOfElements++;
}
else {
temporal = head;
while (temporal->getNext() != NULL) {
temporal = temporal->getNext();
}
Node<T>* newNode = new Node<T>(info, NULL, temporal);
temporal->setNext(newNode);
numberOfElements++;
}
}
template<class T>
void List<T>::remove(int position){
temporal = head;
for (int i=0;i<position;i++){
temporal = temporal->getNext();
}
temporal->getPrevious()->setNext(temporal->getNext());
temporal->getNext()->setPrevious(temporal->getPrevious());
delete temporal;
numberOfElements--;
}
template<class T>
bool List<T>::empty() { return head == NULL; }
template<class T>
int List<T>::getNumberOfElements() { return numberOfElements; }
template<class T>
void List<T>::printList(){
cout << "----------Printing List---------\n";
for (int i = 0; i < numberOfElements; i++) {
temporal = head;
cout << temporal->getData() << endl;
temporal = temporal->getNext();
}
}
class Person {
private:
string name;
string id;
public:
Person(string, string);
virtual ~Person();
friend ostream& operator <<(ostream& out, Person& p) {
out << "Name: " << p.name << "\tID: " << p.id << endl;
return out;
}
};
Person::Person(string n, string i):name(n),id(i) {}
Person::~Person(){}
int main() {
List<int> list1;
List<Person> list2;
for (int i = 0; i < 10; i++) {
list1.add(i * 3);
}
Person per1("A", "1");
Person per2("B", "2");
Person per3("C", "3");
Person per4("D", "4");
Person per5("E", "5");
list2.add(per1);
list2.add(per2);
list2.add(per3);
list2.add(per4);
list2.add(per5);
list1.printList();
list2.printList();
return 0;
}
|