안녕하세요. 링크드리스트 관련 질문 드립니다...
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include <cstring>
#include <cstdlib>
using namespace std;
ifstream inputfile;
ofstream outputfile;
class DNode {
private:
string name;
int score;
int tot;
int lowcnt;
int highcnt;
DNode* prev;
DNode* next;
friend class DLinkedList;
};
class DLinkedList {
public:
DLinkedList();
~DLinkedList();
bool empty() const;
void remove(const string& name, const int& score);
void removeFront();
DNode* findposition(const string& name,const int& point);
void add(const string& name, const int& score);
void print();
void frontprint();
void backprint();
private:
DNode* header;
DNode* trailer;
};
bool DLinkedList::empty() const
{
return (header->next == trailer);
}
DLinkedList::DLinkedList() {
header = new DNode;
trailer = new DNode;
header->next = trailer;
trailer->prev = header;
}
DLinkedList::~DLinkedList() {
while (!empty()) removeFront();
delete header;
delete trailer;
}
DNode* DLinkedList::findposition(const string& name,const int& point) { // 입력된 점수가 header다음 노드부터 시작하여 그 노드보다 더 낮으면 다음 노드를 다시 비교, 그렇게해서 다음 노드보다 더 높은 스코어면 마지막에 비교한 노드를 반환,처음엔 헤더의 다음인 꼬리의 score(=0)을 비교하므로 해더와 꼬리의 중간에 값이 들어가게 됨.
DNode* fp = header;
while (1) {
fp = fp->next;
if ((fp->score) < point) {
return fp;
}
else if (fp->score == point) {
if (fp == trailer)
return fp;
if ((name.compare((fp->name)) == -1) || (name.compare((fp->name)) == 0))
return fp;
}
}
}
void DLinkedList::add(const string& name, const int& point) {
DNode* u = new DNode;
trailer->score = 0;
u->tot = 0;
u->highcnt = 0;
u->lowcnt = 0;
u->name = name;
u->score = point;
//findposition(name, score)//여기서 add 시킬 위치가 나옴.(findpo)의 앞에 add하면됨.
DNode* x = findposition(name, point);//findposi는 DNode 주소값을 반환함.
u->next = x;
u->prev = x->prev;
x->prev->next = x->prev = u;
}
void DLinkedList::remove(const string& name, const int& point) {
int k = 0;
DNode* ex = header;
while (ex!=trailer) {
DNode* ex = header;
for(int i =0 ; i<=k;i++)
ex = ex->next;
if (ex == trailer)
break;
if (ex->score == point) {
if ((ex->name).compare(name) == 0) {
DNode* u = ex->prev;
DNode* w = ex->next;
u->next = w;
w->prev = u;
k++;
delete ex;
}
}
}
}
void DLinkedList::removeFront()
{
remove(header->next->name,header->next->score);
}
void DLinkedList::print()
{
DNode* u = header->next;
while (1) {
if (u == trailer)
break;
u->tot = (u->prev->tot) + 1;
trailer->tot = (trailer->prev->tot) + 1;
if ((u->score) != (u->prev->score))
u->highcnt = (u->tot);
else
u->highcnt = (u->prev->highcnt);
u = u->next;
}
DNode* w = trailer->prev; //이 방법은 10명이 모두 같은 점수면 low일때 모두 10등으로 나올듯. QnA에 따르면 뒤에서부터 줄이는게 맞을듯.
while (1) {
if (w == header)
break;
if ((w->score) != (w->next->score))
w->lowcnt = w->tot;
else
if (w->next = trailer)
w->lowcnt = trailer->tot - 1;
else if (w->next != trailer)
w->lowcnt = w->next->lowcnt;
w = w->prev;
}
frontprint();
outputfile << endl;
backprint();
outputfile << endl;
}
void DLinkedList::frontprint()
{
DNode* f = header->next;
for (int i = 0; i < 10;i++) {
if (f == trailer)
break;
outputfile << f->highcnt << "\t" << f->name << "\t" << f->score << endl;
f = f->next;
}
}
void DLinkedList::backprint()
{
DNode* b = trailer->prev;
for (int i = 0; i < 10;i++) {
if (b == header)
break;
outputfile << b->lowcnt << "\t" << b->name << "\t" << b->score << endl;
b = b->prev;
}
}
int main (int argc, char* argv[]){
if(argc < 3) {
cout <<"Usage: " << argv[0] << "inputfile output_file" <<endl;
}
inputfile.open(argv[1]);
if(inputfile.fail())
cout << "Cannot open inpufile" <<endl;
outputfile.open(argv[2]);
if(outputfile.fail())
cout << "Cannot open outputfile" << endl;
if(inputfile.is_open()) {
string line;
DLinkedList dll;
string name;
int point;
char type;
while (getline(inputfile, line)) {
istringstream iss(line);
iss >> type >> name >> point;
if(type == 'a')
dll.add(name, point);
else if (type == 'r')
dll.remove(name, point);
else if (type == 'p')
dll.print();
}
}
inputfile.close();
outputfile.close();
return 0;
}a Bill 750
a Joe 130
a Lucy 940
a Leanne 1000
a Amy 950
a Andy 830
a Colin 790
a Dave 300
a Eddy 100
a Stephanie 810
a Olivia 520
a Mia 630
p
r Bill 750
p
이런 입력 파일을 받아서 아웃풋 파일로 상위 10등 하위 10등을 출력하는 것인데
1 Leanne 1000
2 Amy 950
3 Lucy 940
4 Andy 830
5 Stephanie 810
6 Colin 790
7 Bill 750
8 Mia 630
9 Olivia 520
10 Dave 300
12 Eddy 100
11 Joe 130
10 Dave 300
9 Olivia 520
8 Mia 630
7 Bill 750
6 Colin 790
5 Stephanie 810
4 Andy 830
3 Lucy 940
1 Leanne 1000
2 Amy 950
3 Lucy 940
4 Andy 830
5 Stephanie 810
6 Colin 790
7 Mia 630
8 Olivia 520
9 Dave 300
10 Joe 130
11 Eddy 100
10 Joe 130
9 Dave 300
8 Olivia 520
7 Mia 630
6 Colin 790
5 Stephanie 810
4 Andy 830
3 Lucy 940
2 Amy 950
리눅스 환경에서 컴파일만되고 실행이 되질 않네요...
감사합니다.

경고 메시지는 없었나요?
컴파일 되고 실행 안 되면 컴파일 할 때 나오는 경고 메시지가 도움될 때도 있어요.
세벌 https://sebuls.blogspot.kr/
댓글 달기