0% found this document useful (0 votes)
39 views5 pages

LinkedList2 Union de Listas Ligadas

The document defines a LinkedList class with methods to insert, delete, and print nodes. It also defines an LNode class to represent nodes in the linked list. The main function creates two linked lists L1 and L2, inserts and deletes nodes from each, prints them, and appends L2 to L1 to print the combined list.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views5 pages

LinkedList2 Union de Listas Ligadas

The document defines a LinkedList class with methods to insert, delete, and print nodes. It also defines an LNode class to represent nodes in the linked list. The main function creates two linked lists L1 and L2, inserts and deletes nodes from each, prints them, and appends L2 to L1 to print the combined list.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

#include <iostream> #include "LinkedList.

h" using namespace std; int main (int argc, char** argv) { LinkedList L1; L1.InsertLast(new LNode(17)); L1.InsertFirst(new LNode(16)); L1.InsertLast(new LNode(9)); L1.InsertFirst(new LNode(5)); L1.DeleteFirst(); L1.DeleteLast(); cout << "Lista 1" <<endl; L1.Print(); LinkedList L2; L2.InsertLast(new LNode(117)); L2.InsertFirst(new LNode(1)); L2.InsertLast(new LNode(10)); L2.DeleteFirst(); L2.DeleteLast(); cout << "Lista 2" <<endl; L2.Print(); cout<< "Lista 1+2" <<endl; L1.Append(&L2); L1.Print(); getchar(); return EXIT_SUCCESS; }

#include <iostream> #include "LinkedList.h" LNode::LNode(int value) { m_data = value; m_next = NULL; } LinkedList::LinkedList() { m_head = NULL; m_tail = NULL; }

void LinkedList::InsertFirst(LNode *item) { // (*item).m_next = m_head; esta y la de abajo practicamente hacen lo mismo item->m_next = m_head; m_head = item; if (m_tail ==NULL) m_tail = item; } void LinkedList::InsertLast(LNode *item) { if (m_head == NULL) { m_head = m_tail = item; return; } m_tail->m_next = item; m_tail = item; } void LinkedList::DeleteFirst() { if(m_head == NULL) return; //Equivalente de (1) /*if(m_head->m_data == NULL) m_tail = NULL;*/ LNode* aux = m_head; m_head = m_head->m_next; delete aux; //(1) if(m_head == NULL) m_tail = NULL; } void LinkedList::DeleteLast() { if(m_head == NULL) return; if(m_head->m_next == NULL) { delete m_head; m_head = m_tail = NULL; return; } LNode* aux = m_head; while (aux->m_next->m_next != NULL) aux = aux->m_next; aux->m_next = NULL; delete m_tail; m_tail = aux;

} void LinkedList::Print() { Print(false); } void LinkedList::Print(bool reverse) { if (reverse == false) { std::cout << Count() << "Nodos(s): "; LNode* aux = m_head; while(aux != NULL) { std::cout << aux->m_data << " -> " ; aux = aux->m_next; } std::cout << "NULO" <<std::endl; } else { //ToDo: Lista doblemente ligada std::cout << "No se tiene la implementacion de una Lista Doblemente Ligada" << std::endl; } } int LinkedList::GetHeadValue() { /*if (!m_head) return NaN; return m_head->m_data;*/ return m_head ? m_head->m_data:NaN; } int LinkedList::GetTailValue() { return m_tail ? m_tail->m_data:NaN; } LNode* LinkedList::GetHeadRef() { return m_head; } LNode* LinkedList::GetTailRef() { return m_tail; } int LinkedList::Count() { int count = 0;

for(LNode* aux = m_head; aux; aux = aux->m_next, count++); return count; } /* int LinkedList::Count() { LNode* aux = m_head; while(aux =NULL) { Count++; aux=aux->m_next; } return Count; } */ void LinkedList::Append(LinkedList* list) { InsertLast(list->GetHeadRef()); m_tail = list->GetTailRef(); }

#ifndef LINKEDLIST2_H_ #define LINKEDLIST2_H_ #define NaN 0x80000000 class LNode { public: int m_data; LNode* m_next; LNode(int value); }; class LinkedList { private: LNode* m_head; LNode* m_tail; public:

LinkedList(); void InsertFirst(LNode* item); void InsertLast(LNode* item); void DeleteFirst(); void DeleteLast(); void Print(); void Print(bool reverse); int GetHeadValue(); int GetTailValue(); LNode* GetHeadRef(); LNode* GetTailRef(); int Count(); void Append(LinkedList* list); }; #endif

You might also like