c++ 링크드리스트 질문이 잇습니다

익명 사용자의 이미지

현재 음원관리프로그램을 짜는중인데 링크드리스트 부분에서 막혀서 3일동안 진전이 없어서

이렇게 질문을 남깁니다

어플리케이션 클래스

public:

Application()
{
m_nCurCommand = 0;
GenreType indata;
indata.setInfo(1, "발라드");
g_List5.Add(indata);
indata.setInfo(2, "힙합");
g_List5.Add(indata);
indata.setInfo(3, "팝송");
g_List5.Add(indata);
}
private:
LinkedList m_List;
LinkedList g_List;

MusicType class 안에 있는 private
int m_id; // 곡번호
string m_music; //음악명
string m_artist; //가수
string m_album; //앨범
string m_genre; //장르
string m_lyrics; //가사

GenreType 안에 있는 private
int m_id; // 곡번호
string m_genre; //음악명
LinkedList g_List;

SimpleMusicType 안에 있는 private
int m_id; ///< Primary key
string m_music;

노드 클래스와 링크드리스트 클래스
template
struct NodeType
{
T data;
NodeType* next;
};

template
class LinkedList
{

private:
NodeType* m_pList;
NodeType* m_pCurPointer;
int m_nLength;
};

링크드 리스트 안에 있는 구현 함수 코드 입니다

template
LinkedList::LinkedList()
{
m_nLength = 0;
m_pList = NULL;
m_pCurPointer = NULL;
}

// Class destructor
template
LinkedList::~LinkedList()
{
MakeEmpty();
}

template
void LinkedList::MakeEmpty()
{
NodeType* tempPtr;

while (m_pList != NULL)
{
tempPtr = m_pList;
m_pList = m_pList->next;
delete tempPtr;
}

m_nLength = 0;
}

template
int LinkedList::GetLength() const
{
return m_nLength;
}

template
int LinkedList::Add(T item)
{
ResetList();

NodeType *node = new NodeType;
NodeType *pre;
T dummy;
bool bFound = false;

node->data = item;
node->next = NULL;

if (!m_nLength)
{
m_pList = node;
}
// list 에 node 가 하나 이상 존재하는 경우
else
{
// 가장 마지막 node 로 이동 후 삽입
while (1)
{
// 이전 노드를 가리키는 포인터 갱신
pre = m_pCurPointer;

// iteration 을 이용해 node 포인터 갱신.
GetNextItem(dummy);

if (m_pCurPointer->data>node->data)
{
if (pre == NULL)
{
node->next = m_pCurPointer;
m_pList = node;
break;
} //넣을 자리 앞 원소가 존재하지 않을 때 첫번째 원소로 삽입.
node->next = m_pCurPointer;
pre->next = node;
break;
} //지금 가리키는 원소의 data값이 node의 data값보다 클 경우 pre 뒷자리에 삽입.

// node 포인터가 마지막 node 를 가리키면 그 뒤에 새로운 node 삽입.
if (m_pCurPointer->next == NULL)
{
// 마지막 node 와 새로운 node 연결
m_pCurPointer->next = node;
break;
}
}
}

m_nLength++;

return 1;
}

template
int LinkedList::Get(T& item)
{
bool moreToSearch, found;
NodeType* location; //변수 선언

location = m_pList;
found = false;
moreToSearch = (location != NULL); //변수 초기화

while (moreToSearch && !found) //리스트의 끝이 아니면서 아직 찾지 않았으면 반복한다.
{
if (item == location->data)
{
found = true;
item = location->data;
} //일치하는 항목을 찾았을 때 found의 값을 변경해주고 item에 해당 항목을 복사해준다.
else
{
location = location->next;
moreToSearch = (location != NULL);
} //찾지 못했을 때 다음 항목으로 location을 옮기고 그 값이 NULL이면 리스트의 끝이므로 moreToSearch의 값을 변경해준다.
}

if (found)
return 1;
else
return 0; //찾으면 1, 그렇지 못하면 0을 리턴한다.
}

// Initializes current pointer for an iteration through the list.
template
void LinkedList::ResetList()
{
// current pointer 초기화
m_pCurPointer = NULL;
}

// Gets the next element in list.
template
void LinkedList::GetNextItem(T& item)
{
// current pointer 이 NULL이라면 처음 node를 가리킴.
if (m_pCurPointer == NULL)
{
m_pCurPointer = m_pList;
}
else
//current position 을 다음 노드로 이동
m_pCurPointer = m_pCurPointer->next;

//item 에 current position 의 info 를 삽입
item = m_pCurPointer->data;
}

template
T* LinkedList::GetPoint(T data)
{

bool moreToSearch, found;
NodeType* location; //변수 선언

location = m_pList;
found = false;
moreToSearch = (location != NULL); //변수 초기화

while (moreToSearch && !found) //리스트의 끝이 아니면서 아직 찾지 않았으면 반복한다.
{
if (data == location->data)
{
found = true;
data = location->data;
return &location->data;
} //일치하는 항목을 찾았을 때 found의 값을 변경해주고 item에 해당 항목을 복사해준다.
else
{
location = location->next;
moreToSearch = (location != NULL);
} //찾지 못했을 때 다음 항목으로 location을 옮기고 그 값이 NULL이면 리스트의 끝이므로 moreToSearch의 값을 변경해준다.
}

return NULL; //찾지 못하면 0을 리턴한다.
}

GenreType 안에 들어가 있는 함수입니다.

void GenreType::AddMusicInGenre(SimpleMusicType indata)
{
g_List.Add(indata);
}

-----어플리케이션 class 안에 있는 음원정보를 추가하는 함수입니다.
MusicType item;
SimpleMusicType items;
item.SetRecordFromKB(); // 추가할 음원의 정보를 입력받음 곡번호,음악명,장르명,가사 등등
items.SetInfo(item.GetId(), item.GetMusic()); // 임시객체 items에 item의 곡번호와 음악명을 가져와서 넣어줌

GenreType temp; // 입력된 음악이 어느 장르에 속하는지 찾기 위한 더미
GenreType *pData; // mGenreList의 해당 장르를 포인터로 가르키기 위한 더미
g_List5.ResetList();

for (int i = 0; i < g_List5.GetLength(); i++)
{
g_List5.GetNextItem(temp);
if (item.GetGenre() == temp.GetGenre())
{
pData = g_List5.GetPoint(temp); // 입력된 음악과 일치하는 장르를 찾아 포인터로 가르킨다
pData->AddMusicInGenre(items); // 리스트 안 리스트에 음악 추가
break;
}
}---------------------------------------

현재 막히는 부분이 이부분입니다. LinkedList g_List는 처음에 생성자를 통해
발라드,힙합,팝송 이라는 정보를 가지고 있습니다 리스트 안에 또 다른 리스트가 들어가 있기에
for문을 통하여 입력받은 장르명과 동일한 장르명을 찾앗을시 포인터 객체를 통하여 그 노드주소를 저장하고 AddMusicInGenre (장르타입 클래스) 함수를 이용하여 리스트 안에 리스트에 해당 정보를 저장하려 하는데 첫번째 데이터는 잘들어가지만 2번째 데이터를 넣으려고 하면 콘솔창에서 에러가 나옵니다. 노드의 범위가 문제가 되는것 같은데 아무리 해결하려 해봐도 도저히 답이 안나와서 여쭤봅니다

리스트 안에 리스트 없이 하면 쉽게 할수 있는데 리시트 안에 리스트로 하려고 보니 노드의 포인터가 문제가 되는건지 도저히 모르겟네요...

3일내내 이 부분 하나를 못넘어가고 있습니다 ㅠㅠ

세벌의 이미지

소스코드에 code 태그 안 쓰면 들여쓰기가 무시되어서 읽기 어렵네요.

<code lang="cpp">
public:

Application()
{
m_nCurCommand = 0;
GenreType indata;
indata.setInfo(1, "발라드");
g_List5.Add(indata);
indata.setInfo(2, "힙합");
g_List5.Add(indata);
indata.setInfo(3, "팝송");
g_List5.Add(indata);
}
private:
LinkedList m_List;
LinkedList g_List;

</code>
이런 식으로 code 태그 쓰면 아래와 같이 나타납니다.

public:
 
Application()
{
m_nCurCommand = 0;
GenreType indata;
indata.setInfo(1, "발라드");
g_List5.Add(indata);
indata.setInfo(2, "힙합");
g_List5.Add(indata);
indata.setInfo(3, "팝송");
g_List5.Add(indata);
}
private:
LinkedList m_List;
LinkedList g_List;

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.