Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- template <typename N> class Node{
- private:
- N key;
- Node<N>* next;
- public:
- Node(N key){
- this->key = key;
- next = NULL;
- }
- int getKey() { return key; }
- Node<N>* getNext() { return next; }
- void setNext(Node<N>* next) { this->next = next; }
- };
- template <typename L> class List{
- private:
- Node<L>* head, *tail;
- int size;
- Node<L>* _search(L x){
- Node<L>* temp = head;
- while(temp!=NULL)
- {
- if(temp->getKey() == x)
- return temp;
- temp = temp->getNext();
- }
- return NULL;
- }
- public:
- List(){
- head = tail = NULL;
- size = 0;
- }
- int getSize() { return size; }
- int search(L x){
- if(_search(x))
- return 1;
- return 0;
- }
- List<L>* insertInHead(L x){
- Node<L>* temp = new Node<L>(x);
- size++;
- if(!head){
- head = tail = temp;
- return this;
- }
- temp->setNext(head);
- head = temp;
- return this;
- }
- List<L>* insertInTail(L x){
- if(!head){
- return insertInHead(x);
- }
- Node<L>* temp = new Node<L>(x);
- size++;
- tail->setNext(temp);
- tail = temp;
- return this;
- }
- void del(L x){
- Node<L>* temp;
- if(search(x))
- temp = _search(x);
- else{
- cout << "Elemento Inesistente" << endl;
- return;
- }
- Node<L>* prev = head;
- while(prev->getNext() != temp)
- prev = prev->getNext();
- prev->setNext(temp->getNext());
- delete(temp);
- size--;
- }
- void print(){
- Node<L>* temp = head;
- while(temp){
- cout << temp->getKey() << " ";
- temp = temp->getNext();
- }
- cout << endl;
- }
- };
- int main(){
- List<int>* T = new List<int>();
- T->insertInHead(5)->insertInTail(567)->insertInHead(6)->insertInTail(8)->insertInHead(45);
- T->print();
- T->del(6);
- T->print();
- T->del(8);
- T->print();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement