* 2D 연결 리스트
- 한 방향이 아닌 두 방향으로 연결이 이루어짐
Anode.h - 알파벳 클래스 노드
#pragma once
#include "Anode.h"
#include "Wnode.h"
class Anode
{
private:
Anode* pNextAlphabet;
Wnode* pNextWord;
char m_alphabet;
public:
Anode(void) { pNextAlphabet=NULL;
pNextWord=NULL;
m_alphabet=NULL;}
Anode(char ch) { m_alphabet=ch; }
char get_Alphabet() { return m_alphabet; }
Anode* get_pNextAlphabet() { return pNextAlphabet; }
Wnode* get_pNextWord() { return pNextWord; }
void set_Alphabet(char _m_alphabet) { m_alphabet=_m_alphabet; }
void set_pNextAlphabet(Anode *_pNextAlphabet) { pNextAlphabet = _pNextAlphabet; }
void set_pNextWord(Wnode *_pNextWord) { pNextWord = _pNextWord; }
};
Wnode.h - 단어 클래스 노드
#pragma once
#include <iostream>
#include <string.h>
class Wnode
{
private:
char m_word[64];
Wnode* pNext;
public:
Wnode() { pNext=NULL;
for(int i=0; i<64; i++)
m_word[i]=NULL;
}
char *getWord() { return m_word; }
Wnode *getpNext() { return pNext; }
void set_word(char* _m_word) { strcpy(m_word,_m_word); }
void set_pNext(Wnode *_pNext) { pNext = _pNext; }
};
AL.h - 알파벳 노드 연결 리스트 클래스
#pragma once
#include "Wnode.h"
#include "Anode.h"
class AL
{
private:
Anode *alphabet_pHead;
public:
AL(void) { alphabet_pHead=NULL; }
Anode *get_alphabet_pHead() { return alphabet_pHead; }
void insert_alphabet_list(char alphab);
};
AL.c
#include "AL.h"
void AL::insert_alphabet_list(char alphab)
{
Anode *pNew = new Anode;
pNew->set_Alphabet(alphab);
if(alphabet_pHead==NULL)
{
alphabet_pHead=pNew;
}
else
{
Anode *al_pCur;
al_pCur=alphabet_pHead;
while(1)
{
if(al_pCur->get_pNextAlphabet()==NULL)
break;
al_pCur=al_pCur->get_pNextAlphabet();
}
al_pCur->set_pNextAlphabet(pNew);
}
}
WL.h - 단어 노드 연결 리스트 클래스
#pragma once
#include "Wnode.h"
#include "Anode.h"
#include "AL.h"
class WL
{
private:
AL *apl;
Wnode *word_pHead;
public:
WL(void) { word_pHead=NULL; }
Wnode *get_word_pHead() { return word_pHead; }
void insert_word(Wnode *pNew,Anode *alpha_head);
};
WL.c
#include "WL.h"
void WL::insert_word(Wnode *pNew, Anode *alpha_head)
{
if(alpha_head->get_pNextWord()==NULL)
{
alpha_head->set_pNextWord(pNew);
}
else
{
Wnode *wordlist_Cur;
wordlist_Cur=alpha_head->get_pNextWord();
while(1)
{
if(wordlist_Cur->getpNext()==NULL)
break;
wordlist_Cur=wordlist_Cur->getpNext();
}
wordlist_Cur->set_pNext(pNew);
}
}
LL.h - 전체 연결 리스트 관리 클래스
#pragma once
#include "Anode.h"
#include "Wnode.h"
#include "AL.h"
#include "WL.h"
#include <fstream>
using namespace std;
class LL
{
private:
WL wl;
public:
int Insert(char* input,AL al);
void Show(AL al);
};
LL.c
#include "LL.h"
#include <iostream>
using namespace std;
int LL::Insert(char* input,AL al)
{
Anode*alphabet_cur;
alphabet_cur=al.get_alphabet_pHead();
while(1)
{
if(input[0] > 64 && input[0] < 91)
input[0] = input[0] + 32;
if(alphabet_cur->get_Alphabet()==input[0])
{
Wnode *pNew_word;
pNew_word= new Wnode;
pNew_word->set_word(input);
wl.insert_word(pNew_word,alphabet_cur);
break;
}
if(alphabet_cur->get_pNextAlphabet()==NULL)
break;
alphabet_cur=alphabet_cur->get_pNextAlphabet();
}
return 0;
}
void LL::Show(AL al)
{
Anode *alpcur=al.get_alphabet_pHead();
Wnode *pW;
while(1)
{
cout<<alpcur->get_Alphabet()<<" -> ";
pW=alpcur->get_pNextWord();
while(pW)
{
cout<<pW->getWord();
if(pW->getpNext()!=NULL)
cout<<" -> ";
pW=pW->getpNext();
}
cout<<endl;
if(alpcur->get_pNextAlphabet()==NULL)
break;
alpcur=alpcur->get_pNextAlphabet();
}
}
main.c
#include "LL.h"
#include <fstream>
using namespace std;
void main()
{
ifstream _in;
char em_str[50]={0,};
char arr[50]={0,};
LL link;
AL alpha;
_in.open("input.dat");
if(_in.fail())
{
cout<<"error!!!!!!! "<<endl;
return;
}
for(int i=97; i<97+26; i++)
alpha.insert_alphabet_list(i);
while(_in.getline(em_str,50))
link.Insert(em_str,alpha);
link.Show(alpha);
}