링크드리스트에 관해 c언어 수다님들!!! 질문 하나 하겠습니다.!!!
영빈이
질문 제목 : 링크드리스트라는 개념을 배우는데... c언어의 기본은 알겠는데(알아도 왕초보입니다.)
이 소스를 보고는 도저히 이해가 안 가서 질문합니다.
각 줄이 무엇을 뜻하는지 도저히 모르겠네요.
질문 내용 :소스파일 2개 .c로 된거... 헤더파일 1개 .h로 된거... 비주얼 스튜디오 2008에서 구동했구요.
c언어 수다님들 주석 좀 달아주셨으면 좋겠습니다. 감사합니다.
exampl03_02.c 파일
#include stdio.h
#include stdlib.h
#include linkedlist.h
void displaylinkedlist(linkedlist* plist)
{
int i = 0;
if (plist != null) {
printf(현재 원소 개수: %d\n, plist-currentelementcount);
for(i = 0; i plist-currentelementcount; i++) {
printf([%d],%d\n, i, getllelement(plist, i)- data);
}
}
else {
printf(원소가 없습니다.\n);
}
}
int main(int argc, char *argv[])
{
int i = 0;
int arraycount = 0;
linkedlist *plist = null;
listnode* pnode = null;
listnode node;
plist = createlinkedlist();
if (plist != null) {
node.data = 1;
addllelement(plist, 0, node);
node.data = 3;
addllelement(plist, 1, node);
node.data = 5;
addllelement(plist, 2, node);
displaylinkedlist(plist);
removellelement(plist, 0);
displaylinkedlist(plist);
deletelinkedlist(plist);
}
return 0;
}
linkedlist.c 파일
#include stdio.h
#include stdlib.h
#include string.h
#include linkedlist.h
linkedlist* createlinkedlist()
{
linkedlist *preturn = null;
int i = 0;
preturn = (linkedlist *)malloc(sizeof(linkedlist));
if (preturn != null) {
memset(preturn, 0, sizeof(linkedlist));
}
else {
printf(오류, 메모리할당 createlinkedlist()\n);
return null;
}
return preturn;
}
int addllelement(linkedlist* plist, int position, listnode element)
{
int ret = false;
int i = 0;
listnode* pprenode = null;
listnode* pnewnode = null;
if (plist != null) {
if (position = 0
&& position = plist - currentelementcount) {
pnewnode = (listnode*)malloc(sizeof(listnode));
if (pnewnode != null) {
*pnewnode = element;
pnewnode-plink = null;
pprenode = &(plist-headernode);
for(i = 0; i position; i++) {
pprenode = pprenode-plink;
}
pnewnode-plink = pprenode - plink;
pprenode - plink = pnewnode;
plist- currentelementcount++;
ret = true;
}
else {
printf(오류, 메모리할당 addllelement()\n);
return ret;
}
}
else {
printf(오류, 위치 인덱스-[%d], addllelement()\n, position);
}
}
return ret;
}
int removellelement(linkedlist* plist, int position)
{
int ret = false;
int i = 0;
int arraycount = 0;
listnode* pnode = null;
listnode* pdelnode = null;
if (plist !=plist != null) {
arraycount = getlinkedlistlength(plist);
if (position = 0 && position arraycount)
{
pnode = &(plist - headernode);
for(i = 0; i position; i++) {
pnode = pnode - plink;
}
pdelnode = pnode-plink;
pnode-plink = pdelnode-plink;
free(pdelnode);
plist-currentelementcount--;
ret = true;
}
else {
printf(오류, 위치 인덱스-[%d] removellelement()\n, position);
}
}
return ret;
}
listnode* getllelement(linkedlist* plist, int position)
{
listnode* preturn = null;
int i = 0;
listnode* pnode = null;
if(plist != null) {
if (position =0 && position plist-currentelementcount) {
pnode = &(plist-headernode);
for(i = 0; i = position; i++) {
pnode = pnode-plink;
}
preturn = pnode;
}
}
return preturn;
}
void deletelinkedlist(linkedlist* plist)
{
int i = 0;
if (plist != null) {
clearlinkedlist(plist);
free(plist);
}
}
void clearlinkedlist(linkedlist* plist)
{
if (plist != null) {
if (plist-currentelementcount 0) {
removellelement(plist,0);
}
}
}
int getlinkedlistlength(linkedlist* plist)
{
int ret = 0;
if (plist != null) {
ret = plist - currentelementcount;
}
return ret;
}
int isempty(linkedlist* plist)
{
int ret = false;
if (plist != null) {
if (plist-currentelementcount == 0) {
ret = true;
}
}
return ret;
}
linkedlist.h 파일
#ifndef _linkedlist_
#define _linkedlist_
typedef struct listnodetype
{
int data;
struct listnodetype* plink;
} listnode;
typedef struct linkedlisttype
{
int currentelementcount;// 현재 저장된 원소의 개수
listnode headernode;// 헤더 노드(header node)
} linkedlist;
linkedlist* createlinkedlist();
int addllelement(linkedlist* plist, int position, listnode element);
int removellelement(linkedlist* plist, int position);
listnode* getllelement(linkedlist* plist, int position);
void clearlinkedlist(linkedlist* plist);
int getlinkedlistlength(linkedlist* plist);
void deletelinkedlist(linkedlist* plist);
#endif
#ifndef _common_list_def_
#define _common_list_def_
#define true1
#define false0
#endif
실행 화면
-
가을 2024-03-24
일단 질문자체부터... 이렇게 올리는건 숙제 좀 해달라는 것과 비견댈 정도의 질문이네요..-_-;;
\난 아무것도 모르겠으니, 대신 좀 해주시죠?\ 라는 말과 다를바가 없습니다.
적어도 자신이 아는 범위는 어느정도 이며, 어느부분이 잘 이해가 안가는지 스스로 알아야 질문도 체계적으로 할 수 있습니다.
전혀 모른다면 윗분 말씀대로 아무리 공부를 해봤자 그저 이리저리 헤매기만 할겁니다.
아니면 좀 더 간단한 소스를 찾아서 공부하시는게 더 도움이 되실겁니다 -
소심한녀자 2024-03-24
알고 계시는 C 언어 기본....의 수준이 어느정도인지 알지 못하면 설명하기도 힘듭니다. 일단 연결리스트를 이해하려면 포인터에 대한 확실한 이해는 기본이구요, 동적 메모리 할당/해제도 왠만큼은 할 줄 알아야 합니다. 그리고 마지막으로 구조체도 알아야 하구요. 제가 열거한 이 세 가지 항목 중 하나라도 미숙하다고 생각되는 부분이 있다면, 연결리스트를 아무리 연구 해 봤자 다차 방정식도 모른채 미/적분을 공부하려는 것과 다르지 않지요.
번호 | 제 목 | 글쓴이 | 날짜 |
---|---|---|---|
2691304 | 포인터배열에 대해 질문요 | 달님 | 2025-04-12 |
2691279 | float-정수변환-2진수변환 | 핫블루 | 2025-04-12 |
2691224 | 인자관련 질문입니다. (2) | 아잉형님 | 2025-04-11 |
2691196 | 원형 큐와 스택에서의 문제해결 (2) | 반혈 | 2025-04-11 |
2691169 | 동적할당 어디가 잘못된걸까요. | 츠키 | 2025-04-11 |
2691082 | puts vs printf | 리네 | 2025-04-10 |
2691058 | (C언어) 구구단을 작성해 봤는데 문제가 발생했습니다. (6) | 제나 | 2025-04-10 |
2690947 | 윈도우 메모장에서 버츄얼박스 우분투 붙여넣기 질문 (1) | 유우 | 2025-04-09 |
2690920 | 무슨 의미 인가요? | 딸기맛캔디 | 2025-04-09 |
2690891 | c++ 은행계좌 프로그램 | 알찬마루 | 2025-04-08 |
2690834 | c언어 if문에서 출력 질문합니다 ㅜㅜ (2) | 파랑새 | 2025-04-08 |
2690809 | c언어 질문좀 부탁드리겠습니다 | 개구리 | 2025-04-08 |
2690777 | 말 표현에서 궁금한점이 있습니다. 배열을 통째로 넘겨받으려면 매개변수로 배열을 선언할 수 있어야한다? (8) | 별햇님 | 2025-04-07 |
2690749 | 피라미드 질문드립니다 (for문 한개) | 도래솔 | 2025-04-07 |
2690718 | 함수 포인터반환 함수 질문 | 에드워드 | 2025-04-07 |
2690685 | 2중for문 도와주세여..;; | 흰추위 | 2025-04-07 |
2690655 | 팩토리얼 계산문제..질문~ (9) | 옆집꼬마야 | 2025-04-06 |
2690627 | c 변수 선언후 변수값 저장안하고 출력 | 방방 | 2025-04-06 |
2690600 | 릴리즈 모드로 컴파일해서 다른 컴퓨터에서도 실행파일을 실행할수 있는 방법 알려주세요 (5) | 제나 | 2025-04-06 |
2690576 | bin파일 저장 | 다올 | 2025-04-06 |