chore: migrate

This commit is contained in:
2025-12-21 10:50:53 +07:00
parent 534d59a44e
commit ecdb490c35
16 changed files with 868 additions and 0 deletions
+41
View File
@@ -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
+265
View File
@@ -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;
}
+265
View File
@@ -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;
}
+39
View File
@@ -0,0 +1,39 @@
#ifndef LIST_H
#define LIST_H
#include <iostream>
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
+41
View File
@@ -0,0 +1,41 @@
#include "List.h"
#include "Queue.h"
#include "Stack.h"
#include <iostream>
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;
}
+23
View File
@@ -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;
}
+18
View File
@@ -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
+23
View File
@@ -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;
}
+18
View File
@@ -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
BIN
View File
Binary file not shown.
+41
View File
@@ -0,0 +1,41 @@
#include "List.h"
#include "Queue.h"
#include "Stack.h"
#include <iostream>
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;
}
+23
View File
@@ -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;
}
+18
View File
@@ -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
+23
View File
@@ -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;
}
+18
View File
@@ -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
+12
View File
@@ -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