[Q&A]돌려보게
- 397 조회
![activity-badge](/assets/images/activity-level-icon/LV01.png)
Node.h
#pragma once
#include <stdio.h>
#include <stdlib.h>
#define MAX_STACK_SIZE 100
using namespace std;
inline void error( char* str ) {
fprintf(stderr, "%s\n", str);
exit(1);
};
class Node
{
Node* link;
const char* data;
public:
Node(const char* val = 0) : data(val), link(NULL) { }
Node* getLink() { return link; }
void setLink(Node* next) { link = next; }
void display() { printf(" <%s>", data); }
bool hasData(char* val) { return data == val; }
void insertNext( Node *n ) {
if( n != NULL ) {
n->link = link;
link = n;
}
}
Node* removeNext( ) {
Node* removed = link;
if( removed != NULL )
link = removed->link;
return removed;
}
};
LinkedList.h
#pragma once
#include "Node.h"
class LinkedList
{
Node org;
public:
LinkedList(): org(0) { }
~LinkedList() { clear(); }
void clear() { while(!isEmpty()) delete remove(0); }
Node* getHead() { return org.getLink(); }
bool isEmpty( ) { return getHead()==NULL; }
Node* getEntry(int pos) {
Node* n = &org;
for(int i=-1 ; igetLink())
if( n == NULL ) break;
return n;
}
void insert(int pos, Node *n) {
Node* prev = getEntry(pos-1);
if( prev != NULL )
prev->insertNext( n );
}
Node* remove(int pos) {
Node* prev = getEntry(pos-1);
return prev->removeNext();
}
Node* find(char* val) {
for( Node *p = getHead() ; p != NULL ; p=p->getLink() )
if( p->hasData( val ) ) return p;
return NULL;
}
void replace(int pos, Node *n) {
Node* prev = getEntry(pos-1);
if( prev != NULL ) {
delete prev->removeNext( );
prev->insertNext( n );
}
}
int size( ){
int count = 0;
for( Node *p = getHead() ; p != NULL ; p=p->getLink() )
count++;
return count;
}
void display( ) {
printf( "[You know what I'm say?] : ");
for( Node *p = getHead() ; p != NULL ; p=p->getLink() )
p->display();
printf("\n");
}
void display(const char* c) {
printf("[%s] : ", c);
for (Node* p = getHead(); p != NULL; p = p->getLink())
p->display();
printf("\n");
}
void merge(LinkedList* that) {
while (!that->isEmpty()) {
Node* n = that->remove(0);
insert(size(), n);
}
}
};
QA BAHA.cpp
#include "LinkedList.h"
void main() {
LinkedList list, list2;
list.insert(0, new Node("B"));
list.insert(0, new Node("L"));
list.insert(1, new Node("A"));
list.insert(2, new Node("H"));
list2.insert(0, new Node("O"));
list2.insert(0, new Node("V"));
list2.insert(1, new Node("A"));
list2.insert(2, new Node("E"));
list.display();
list2.display();
list.merge(&list2);
list.display();
Node *n = list.remove(list.size() - 1);
list.insert(1, n);
n = list.remove(list.size() - 1);
list.insert(2, n);
n = list.remove(list.size() - 2);
list.insert(2, n);
n = list.remove(list.size() - 2);
list.insert(list.size() - 3, n);
list.display("I know what you say.");
}
위의 코드를 실행한 결과를 써보시오. //헤더파일 2, cpp파일 1 (참고로 직접한 거라서 맞게 한지는 모르겠습니다ㅎㅎ)
댓글 (0)
등록된 댓글이 없습니다.