diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d4fb281 --- /dev/null +++ b/.gitignore @@ -0,0 +1,41 @@ +# Prerequisites +*.d + +# Compiled Object files +*.slo +*.lo +*.o +*.obj + +# Precompiled Headers +*.gch +*.pch + +# Linker files +*.ilk + +# Debugger Files +*.pdb + +# Compiled Dynamic libraries +*.so +*.dylib +*.dll + +# Fortran module files +*.mod +*.smod + +# Compiled Static libraries +*.lai +*.la +*.a +*.lib + +# Executables +*.exe +*.out +*.app + +# debug information files +*.dwo diff --git a/List.cpp b/List.cpp new file mode 100644 index 0000000..8b6c81f --- /dev/null +++ b/List.cpp @@ -0,0 +1,265 @@ +#include "List.h" +#include "Queue.h" +#include "Stack.h" + +List::List() +{ + pHead = NULL; + count = 0; +} + +void List::addLast(int newdata) +{ + Node *pNew = new Node; + pNew->data = newdata; + if (pHead == NULL) + { + pHead = pNew; + ++count; + return; + } + Node *pTemp = pHead; + while (pTemp->next != NULL) + { + pTemp = pTemp->next; + } + pTemp->next = pNew; + ++count; +} + +void List::addConstant(int nConstant) +{ + Node *pTemp = pHead; + if (pTemp == NULL) + return; + while (pTemp->next != NULL) + pTemp = pTemp->next; + pTemp->data += nConstant; + return; +} + +void List::addFirst(int newdata) +{ + Node *pTemp = new Node; + pTemp->data = newdata; + pTemp->next = pHead; + pHead = pTemp; + count++; +} + +void List::display() +{ + Node *pTemp = pHead; + while (pTemp != NULL) + { + cout << pTemp->data << " "; + pTemp = pTemp->next; + } + cout << endl; +} + +void List::displayPoly() +{ + Node *pTemp = pHead; + int i = 0; + while (pTemp != NULL) + { + int index = count - 1 - i; + if (pTemp->data != 0) + { + if (pTemp->data > 0) + { + if (i != 0) + cout << "+"; + } + cout << pTemp->data; + if (index == 1) + cout << "x"; + else if (index > 1) + cout << "x^" << index; + } + ++i; + pTemp = pTemp->next; + } + cout << endl; +} + +void List::removeFirst() +{ + if (pHead == NULL) + return; + Node *pDel = pHead; + pHead = pHead->next; + delete pDel; + --count; +} + +void List::removeLast() +{ + if (pHead == NULL) + return; + Node *pTemp = pHead; + if (count == 1) + { + pHead = NULL; + delete pTemp; + count = 0; + return; + } + Node *pPrev = NULL; + while (pTemp->next != NULL) + { + pPrev = pTemp; + pTemp = pTemp->next; + } + pPrev->next = NULL; + delete pTemp; + --count; +} + +int List::returnFirst() +{ + if (pHead == NULL) + return 0; + return pHead->data; +} + +int List::returnLast() +{ + if (pHead == NULL) + return 0; + Node *pTemp = pHead; + while (pTemp->next != NULL) + pTemp = pTemp->next; + return pTemp->data; +} + +List::~List() +{ + Node *pTemp = pHead; + while (pTemp != NULL) + { + pTemp = pTemp->next; + delete pHead; + pHead = pTemp; + } +} + +List *addPoly(List *list1, List *list2) +{ + if (list1 == NULL || list2 == NULL) + return NULL; + int i = list1->count, + j = list2->count; + Node *pHead1 = list1->pHead; + Node *pHead2 = list2->pHead; + if (i <= 0 || j <= 0) + return NULL; + if (i > j) + { + while (i > j) + { + pHead1 = pHead1->next; + --i; + } + } + else + { + while (j > i) + { + pHead2 = pHead2->next; + --j; + } + } + List *ret = new List; + while (i > 0) + { + int t = pHead1->data + pHead2->data; + ret->addLast(t); + pHead1 = pHead1->next; + pHead2 = pHead2->next; + --i; + } + return ret; +} + +List *reverseListWithStack(List *list) +{ + if (list == NULL) + return NULL; + Node *pTemp = list->pHead; + Stack *stack = new Stack; + int count = 0; + while (pTemp != NULL) + { + stack->push(pTemp->data); + pTemp = pTemp->next; + ++count; + } + List *ret = new List; + while (count > 0) + { + ret->addLast(stack->pop()); + --count; + } + return ret; +} + +List *reverseListWithQueue(List *list) +{ + if (list == NULL) + return NULL; + Node *pTemp = list->pHead; + Queue *queue = new Queue; + int count = 0; + while (pTemp != NULL) + { + queue->enQueue(pTemp->data); + pTemp = pTemp->next; + ++count; + } + List *ret = new List; + while (count > 0) + { + ret->addFirst(queue->deQueue()); + --count; + } + return ret; +} + +List *appendList(List *list1, List *list2) +{ + List *ret; + if (list2 == NULL) + { + ret = list1; + } + else if (list1 == NULL) + { + ret = list2; + } + else + { + Node *pTemp; + ret = new List; + pTemp = list1->pHead; + while (pTemp != NULL) + { + ret->addLast(pTemp->data); + pTemp = pTemp->next; + list1->removeFirst(); + } + pTemp = list2->pHead; + while (pTemp != NULL) + { + ret->addLast(pTemp->data); + pTemp = pTemp->next; + list2->removeFirst(); + } + } + delete list1; + list1 = NULL; + delete list2; + list2 = NULL; + return ret; +} diff --git a/Nhut/List.cpp b/Nhut/List.cpp new file mode 100644 index 0000000..8b6c81f --- /dev/null +++ b/Nhut/List.cpp @@ -0,0 +1,265 @@ +#include "List.h" +#include "Queue.h" +#include "Stack.h" + +List::List() +{ + pHead = NULL; + count = 0; +} + +void List::addLast(int newdata) +{ + Node *pNew = new Node; + pNew->data = newdata; + if (pHead == NULL) + { + pHead = pNew; + ++count; + return; + } + Node *pTemp = pHead; + while (pTemp->next != NULL) + { + pTemp = pTemp->next; + } + pTemp->next = pNew; + ++count; +} + +void List::addConstant(int nConstant) +{ + Node *pTemp = pHead; + if (pTemp == NULL) + return; + while (pTemp->next != NULL) + pTemp = pTemp->next; + pTemp->data += nConstant; + return; +} + +void List::addFirst(int newdata) +{ + Node *pTemp = new Node; + pTemp->data = newdata; + pTemp->next = pHead; + pHead = pTemp; + count++; +} + +void List::display() +{ + Node *pTemp = pHead; + while (pTemp != NULL) + { + cout << pTemp->data << " "; + pTemp = pTemp->next; + } + cout << endl; +} + +void List::displayPoly() +{ + Node *pTemp = pHead; + int i = 0; + while (pTemp != NULL) + { + int index = count - 1 - i; + if (pTemp->data != 0) + { + if (pTemp->data > 0) + { + if (i != 0) + cout << "+"; + } + cout << pTemp->data; + if (index == 1) + cout << "x"; + else if (index > 1) + cout << "x^" << index; + } + ++i; + pTemp = pTemp->next; + } + cout << endl; +} + +void List::removeFirst() +{ + if (pHead == NULL) + return; + Node *pDel = pHead; + pHead = pHead->next; + delete pDel; + --count; +} + +void List::removeLast() +{ + if (pHead == NULL) + return; + Node *pTemp = pHead; + if (count == 1) + { + pHead = NULL; + delete pTemp; + count = 0; + return; + } + Node *pPrev = NULL; + while (pTemp->next != NULL) + { + pPrev = pTemp; + pTemp = pTemp->next; + } + pPrev->next = NULL; + delete pTemp; + --count; +} + +int List::returnFirst() +{ + if (pHead == NULL) + return 0; + return pHead->data; +} + +int List::returnLast() +{ + if (pHead == NULL) + return 0; + Node *pTemp = pHead; + while (pTemp->next != NULL) + pTemp = pTemp->next; + return pTemp->data; +} + +List::~List() +{ + Node *pTemp = pHead; + while (pTemp != NULL) + { + pTemp = pTemp->next; + delete pHead; + pHead = pTemp; + } +} + +List *addPoly(List *list1, List *list2) +{ + if (list1 == NULL || list2 == NULL) + return NULL; + int i = list1->count, + j = list2->count; + Node *pHead1 = list1->pHead; + Node *pHead2 = list2->pHead; + if (i <= 0 || j <= 0) + return NULL; + if (i > j) + { + while (i > j) + { + pHead1 = pHead1->next; + --i; + } + } + else + { + while (j > i) + { + pHead2 = pHead2->next; + --j; + } + } + List *ret = new List; + while (i > 0) + { + int t = pHead1->data + pHead2->data; + ret->addLast(t); + pHead1 = pHead1->next; + pHead2 = pHead2->next; + --i; + } + return ret; +} + +List *reverseListWithStack(List *list) +{ + if (list == NULL) + return NULL; + Node *pTemp = list->pHead; + Stack *stack = new Stack; + int count = 0; + while (pTemp != NULL) + { + stack->push(pTemp->data); + pTemp = pTemp->next; + ++count; + } + List *ret = new List; + while (count > 0) + { + ret->addLast(stack->pop()); + --count; + } + return ret; +} + +List *reverseListWithQueue(List *list) +{ + if (list == NULL) + return NULL; + Node *pTemp = list->pHead; + Queue *queue = new Queue; + int count = 0; + while (pTemp != NULL) + { + queue->enQueue(pTemp->data); + pTemp = pTemp->next; + ++count; + } + List *ret = new List; + while (count > 0) + { + ret->addFirst(queue->deQueue()); + --count; + } + return ret; +} + +List *appendList(List *list1, List *list2) +{ + List *ret; + if (list2 == NULL) + { + ret = list1; + } + else if (list1 == NULL) + { + ret = list2; + } + else + { + Node *pTemp; + ret = new List; + pTemp = list1->pHead; + while (pTemp != NULL) + { + ret->addLast(pTemp->data); + pTemp = pTemp->next; + list1->removeFirst(); + } + pTemp = list2->pHead; + while (pTemp != NULL) + { + ret->addLast(pTemp->data); + pTemp = pTemp->next; + list2->removeFirst(); + } + } + delete list1; + list1 = NULL; + delete list2; + list2 = NULL; + return ret; +} diff --git a/Nhut/List.h b/Nhut/List.h new file mode 100644 index 0000000..6bffad1 --- /dev/null +++ b/Nhut/List.h @@ -0,0 +1,39 @@ +#ifndef LIST_H +#define LIST_H + +#include + +using namespace std; + +class Node +{ +public: + int data; + Node *next; +}; + +class List +{ +private: + int count; + Node *pHead; + +public: + List(); + void addLast(int); + void addConstant(int); + void addFirst(int); + void removeFirst(); + void removeLast(); + int returnFirst(); + int returnLast(); + void display(); + void displayPoly(); + friend List *addPoly(List *, List *); + friend List *reverseListWithStack(List *); + friend List *reverseListWithQueue(List *); + friend List *appendList(List *, List *); + ~List(); +}; + +#endif // LIST_H \ No newline at end of file diff --git a/Nhut/Poly.cpp b/Nhut/Poly.cpp new file mode 100644 index 0000000..5ab000f --- /dev/null +++ b/Nhut/Poly.cpp @@ -0,0 +1,41 @@ +#include "List.h" +#include "Queue.h" +#include "Stack.h" +#include + +using namespace std; + +int main() +{ + List *myList1 = new List, + *myList2 = new List; + myList1->addFirst(1); + myList1->addFirst(0); + myList1->addFirst(1); + myList1->addFirst(0); + myList1->addFirst(5); + myList1->display(); + myList2->addFirst(1); + myList2->addFirst(2); + myList2->addFirst(3); + myList2->display(); + myList1->displayPoly(); + myList2->displayPoly(); + List *result = addPoly(myList1, myList2); + myList1->display(); + myList2->display(); + if (result != NULL) + result->display(); + myList1->display(); + myList2->display(); + myList1->displayPoly(); + myList2->displayPoly(); + result->displayPoly(); + cout << myList1->returnFirst() << endl; + myList1->removeFirst(); + myList1->display(); + cout << myList1->returnLast() << endl; + myList1->removeLast(); + myList1->display(); + return 0; +} \ No newline at end of file diff --git a/Nhut/Queue.cpp b/Nhut/Queue.cpp new file mode 100644 index 0000000..afdddec --- /dev/null +++ b/Nhut/Queue.cpp @@ -0,0 +1,23 @@ +#include "Queue.h" + +Queue::Queue() +{ + queue = new List; +} + +void Queue::enQueue(int newdata) +{ + queue->addLast(newdata); +} + +int Queue::deQueue() +{ + int ret = queue->returnFirst(); + queue->removeFirst(); + return ret; +} + +Queue::~Queue() +{ + delete queue; +} \ No newline at end of file diff --git a/Nhut/Queue.h b/Nhut/Queue.h new file mode 100644 index 0000000..c41d82e --- /dev/null +++ b/Nhut/Queue.h @@ -0,0 +1,18 @@ +#ifndef QUEUE_H +#define QUEUE_H + +#include "List.h" + +class Queue +{ + private: + List *queue; + + public: + Queue(); + void enQueue(int); + int deQueue(); + ~Queue(); +}; + +#endif // QUEUE_H \ No newline at end of file diff --git a/Nhut/Stack.cpp b/Nhut/Stack.cpp new file mode 100644 index 0000000..a0b702e --- /dev/null +++ b/Nhut/Stack.cpp @@ -0,0 +1,23 @@ +#include "Stack.h" + +Stack::Stack() +{ + stack = new List; +} + +void Stack::push(int newdata) +{ + stack->addFirst(newdata); +} + +int Stack::pop() +{ + int ret = stack->returnFirst(); + stack->removeFirst(); + return ret; +} + +Stack::~Stack() +{ + delete stack; +} \ No newline at end of file diff --git a/Nhut/Stack.h b/Nhut/Stack.h new file mode 100644 index 0000000..447f69b --- /dev/null +++ b/Nhut/Stack.h @@ -0,0 +1,18 @@ +#ifndef STACK_H +#define STACK_H + +#include "List.h" + +class Stack +{ +private: + List *stack; + +public: + Stack(); + void push(int); + int pop(); + ~Stack(); +}; + +#endif // STACK_H \ No newline at end of file diff --git a/Poly b/Poly new file mode 100644 index 0000000..76f4e66 Binary files /dev/null and b/Poly differ diff --git a/Poly.cpp b/Poly.cpp new file mode 100644 index 0000000..5ab000f --- /dev/null +++ b/Poly.cpp @@ -0,0 +1,41 @@ +#include "List.h" +#include "Queue.h" +#include "Stack.h" +#include + +using namespace std; + +int main() +{ + List *myList1 = new List, + *myList2 = new List; + myList1->addFirst(1); + myList1->addFirst(0); + myList1->addFirst(1); + myList1->addFirst(0); + myList1->addFirst(5); + myList1->display(); + myList2->addFirst(1); + myList2->addFirst(2); + myList2->addFirst(3); + myList2->display(); + myList1->displayPoly(); + myList2->displayPoly(); + List *result = addPoly(myList1, myList2); + myList1->display(); + myList2->display(); + if (result != NULL) + result->display(); + myList1->display(); + myList2->display(); + myList1->displayPoly(); + myList2->displayPoly(); + result->displayPoly(); + cout << myList1->returnFirst() << endl; + myList1->removeFirst(); + myList1->display(); + cout << myList1->returnLast() << endl; + myList1->removeLast(); + myList1->display(); + return 0; +} \ No newline at end of file diff --git a/Queue.cpp b/Queue.cpp new file mode 100644 index 0000000..afdddec --- /dev/null +++ b/Queue.cpp @@ -0,0 +1,23 @@ +#include "Queue.h" + +Queue::Queue() +{ + queue = new List; +} + +void Queue::enQueue(int newdata) +{ + queue->addLast(newdata); +} + +int Queue::deQueue() +{ + int ret = queue->returnFirst(); + queue->removeFirst(); + return ret; +} + +Queue::~Queue() +{ + delete queue; +} \ No newline at end of file diff --git a/Queue.h b/Queue.h new file mode 100644 index 0000000..c41d82e --- /dev/null +++ b/Queue.h @@ -0,0 +1,18 @@ +#ifndef QUEUE_H +#define QUEUE_H + +#include "List.h" + +class Queue +{ + private: + List *queue; + + public: + Queue(); + void enQueue(int); + int deQueue(); + ~Queue(); +}; + +#endif // QUEUE_H \ No newline at end of file diff --git a/Stack.cpp b/Stack.cpp new file mode 100644 index 0000000..a0b702e --- /dev/null +++ b/Stack.cpp @@ -0,0 +1,23 @@ +#include "Stack.h" + +Stack::Stack() +{ + stack = new List; +} + +void Stack::push(int newdata) +{ + stack->addFirst(newdata); +} + +int Stack::pop() +{ + int ret = stack->returnFirst(); + stack->removeFirst(); + return ret; +} + +Stack::~Stack() +{ + delete stack; +} \ No newline at end of file diff --git a/Stack.h b/Stack.h new file mode 100644 index 0000000..447f69b --- /dev/null +++ b/Stack.h @@ -0,0 +1,18 @@ +#ifndef STACK_H +#define STACK_H + +#include "List.h" + +class Stack +{ +private: + List *stack; + +public: + Stack(); + void push(int); + int pop(); + ~Stack(); +}; + +#endif // STACK_H \ No newline at end of file diff --git a/makefile b/makefile new file mode 100644 index 0000000..a581680 --- /dev/null +++ b/makefile @@ -0,0 +1,12 @@ +all: List.o Queue.o Stack.o Poly.o + g++ -o Poly List.o Queue.o Stack.o Poly.o +List.o: List.h List.cpp + g++ -c List.h List.cpp +Queue.o: Queue.h Queue.cpp + g++ -c Queue.h Queue.cpp +Stack.o: Stack.h Stack.cpp + g++ -c Stack.h Stack.cpp +Poly.o: Poly.cpp + g++ -c Poly.cpp +clean: + rm -f *.o Poly