Skip to content

[DT]LinkedList : 링크드리스트 while문 순회가 안됬던 문제!  #1

Description

@chldydgh4687

printf 로 디버깅을 해본 결과 NULL 이 안들어간 것이 확인.. 초기 포인터노드에 NULL값을 넣어주니 됬다. 알고리즘 자체가 원래 노드의 pLink를 새로 드러온 노드이 링크로 넣다보니 에러 전파가 된 듯하다!

int addLLElement(LL *pList, int pos, LN data) {
	
	int ret = FALSE;
	LN *new_node = (LN*)malloc(sizeof(LN));
	LN *pnode = NULL;
	if (pList != NULL) {
		//맨 처음
		if (pos == 0) {
			if (pList->currentdata == 0) {
				pList->header_node = data;
				//while문이 작동안됬던 이유!의 NULL
				pList->header_node.pLink = NULL;
				pList->currentdata++;

				ret = TRUE;
				return ret;
			}
			*new_node = (pList->header_node);
			pnode = &data;
			pnode->pLink = new_node;
			pList->header_node = *pnode;
			pList->currentdata++;

			ret = TRUE;
			return ret;
		}
		else if (pos >= 1 && pos <= pList->currentdata) {
			if (new_node != NULL) {
				*new_node = data;
				new_node->pLink = NULL;

				pnode = &(pList->header_node);
				for (int i = 0; i < pos-1; i++) {
					pnode = pnode->pLink;
				}
				new_node->pLink = pnode->pLink;
				pnode->pLink = new_node;
				pList->currentdata++;

				ret = TRUE;
			}
		}
		
	}
	else{
		printf("addLLElement_error : 리스트 동적 할당 오류\n");
		return ret;
	}

	return ret;
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions