kar7mp5
[자료 구조] Doubly Linked List로 CPP STL - 3 본문
728x90
#include <iostream>
using namespace std;
struct Node {
Node* next;
Node* prev;
int data;
};
class nodeList {
public:
nodeList();
Node* begin();
Node* end();
int Size();
bool isEmpty();
void insert(Node* pos, const int& e);
void erase(Node* pos);
bool nextP(Node* pos);
bool prevP(Node* pos);
void finddivisor(const int& e);
private:
Node* header;
Node* trailer;
int size;
};
nodeList::nodeList() {
header = new Node;
trailer = new Node;
header->next = trailer;
trailer->prev = header;
header->prev = trailer->next = nullptr;
size = 0;
}
Node* nodeList::begin() {
return header->next;
}
Node* nodeList::end() {
return trailer;
}
int nodeList::Size() {
return size;
}
bool nodeList::isEmpty() {
if (size == 0)
return true;
return false;
}
void nodeList::insert(Node* pos, const int& e) {
Node* newNode = new Node;
newNode->data = e;
newNode->next = pos;
newNode->prev = pos->prev;
pos->prev->next = newNode;
pos->prev = newNode;
++size;
}
void nodeList::erase(Node* pos) {
if (pos == trailer)
return;
pos->prev->next = pos->next;
pos->next->prev = pos->prev;
delete pos;
--size;
}
bool nodeList::nextP(Node* pos) {
if (pos == trailer)
return false;
return true;
}
bool nodeList::prevP(Node* pos) {
if (pos == header->next)
return false;
return true;
}
void nodeList::finddivisor(const int& e) {
Node* curNode = header->next;
int idx = 0;
bool isExist = false;
while (curNode != trailer) {
if (e%curNode->data == 0) {
isExist = true;
cout << idx << ' ';
}
++idx;
curNode = curNode->next;
}
if (!isExist)
cout << -1 << '\n';
else
cout << '\n';
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int N;
cin >> N;
while (N--) {
nodeList* List = new nodeList;
Node* p = List->end();
int e;
cin >> e;
for (int i = 0; i < e; ++i) {
int val;
cin >> val;
List->insert(p, val);
List->end();
}
cin >> e;
p = List->begin();
int curIndex = 0;
for (int i = 0; i < e; ++i) {
int val;
cin >> val;
if (val > 0) {
for (int i = 0; i < val; ++i) {
if (p != List->end()) {
p = p->next;
curIndex++;
}
}
int tmp = p->data;
if (tmp > 0) {
for (int i = 0; i < tmp; ++i) {
if (p != List->end()) {
p = p->next;
curIndex++;
}
}
}
else {
for (int i = 0; i < -tmp; ++i) {
if (p != List->begin()) {
p = p->prev;
curIndex--;
}
}
}
}
else {
for (int i = 0; i < -val; ++i) {
if (p != List->begin()) {
p = p->prev;
curIndex--;
}
}
int tmp = p->data;
if (tmp > 0) {
for (int i = 0; i < tmp; ++i) {
if (p != List->end()) {
p = p->next;
curIndex++;
}
}
}
else {
for (int i = 0; i < -tmp; ++i) {
if (p != List->begin()) {
p = p->prev;
curIndex--;
}
}
}
}
}
if (curIndex*2 < List->Size()-1) {
cout << "header" << '\n';
}
else if (curIndex*2 > List->Size()-1) {
cout << "trailer" << '\n';
}
else {
cout << "middle" << '\n';
}
}
return 0;
}
728x90
'전공 > 자료구조' 카테고리의 다른 글
[자료 구조] C++로 Tree depth 계산 (0) | 2024.04.12 |
---|---|
[자료 구조] C++로 Tree 구현 (0) | 2024.04.12 |
[자료 구조] Doubly Linked List - 2 (0) | 2024.04.05 |
[자료 구조] Doubly Linked List로 Cpp STL List 구현하기 (0) | 2024.04.05 |