링크드리스트에 관해 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 언어 기본....의 수준이 어느정도인지 알지 못하면 설명하기도 힘듭니다. 일단 연결리스트를 이해하려면 포인터에 대한 확실한 이해는 기본이구요, 동적 메모리 할당/해제도 왠만큼은 할 줄 알아야 합니다. 그리고 마지막으로 구조체도 알아야 하구요. 제가 열거한 이 세 가지 항목 중 하나라도 미숙하다고 생각되는 부분이 있다면, 연결리스트를 아무리 연구 해 봤자 다차 방정식도 모른채 미/적분을 공부하려는 것과 다르지 않지요.
번호 | 제 목 | 글쓴이 | 날짜 |
---|---|---|---|
2654293 | 첫시작값과 끝값의 사이에 존재하는 정수들의 합을 구하는 것 (5) | 해긴 | 2024-05-09 |
2654185 | 64bit 환경에서 자료형의 크기가 좀궁그해요(완료) 더궁금한게 생겻어요(추가) (2) | 빈길 | 2024-05-08 |
2654152 | 확장자 exe로 변경 (1) | 한 | 2024-05-08 |
2654128 | c언어 간단하게 자판기만들려는데 ㅜㅜ | 유1혹 | 2024-05-08 |
2654074 | 배열에 들어있는값을 포인터에 옮기고 싶어요.. | 소라 | 2024-05-07 |
2654043 | 이차원 배열에 있는 값들 더하기.. | 선아 | 2024-05-07 |
2654011 | c언어 대문자소문자, 소문자대문자 변형하기, | 샤르 | 2024-05-07 |
2653985 | 실수 자료형 표현범위있잖아요 (1) | 패틱 | 2024-05-06 |
2653957 | 뭐가틀린건가요?.. (2) | 볼1매그녀 | 2024-05-06 |
2653934 | malloc을 이용한 출력 (1) | 하예 | 2024-05-06 |
2653906 | 숫자 야구 게임인데요.. (2) | 상큼한캔디 | 2024-05-06 |
2653883 | 구조체 질문이요.. 연결리스트. (1) | 별 | 2024-05-05 |
2653802 | 배열에 저장된 데이터와 원하는 데이터비교하는것좀.. | 리카 | 2024-05-05 |
2653727 | 고수님들 봐주세요~ (2) | 아담 | 2024-05-04 |
2653702 | C언어 의 단계가 어떻게 되나요???????????????? (3) | 길가온 | 2024-05-04 |
2653645 | 저....while인데..이게무슨뜻인지좀.. | 설희 | 2024-05-03 |
2653613 | 성별변수 | 뿡뿡 | 2024-05-03 |
2653584 | 16진수 변환 관련 질문 드립니다. | 매미꽃 | 2024-05-03 |
2653557 | 한글 완성형과 조합형의 전환 | 해지개 | 2024-05-02 |
2653506 | void형 포인터 이해안가요 ㅠㅠ (2) | 뭘봐요 | 2024-05-02 |