링크드리스트에 관해 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 언어 기본....의 수준이 어느정도인지 알지 못하면 설명하기도 힘듭니다. 일단 연결리스트를 이해하려면 포인터에 대한 확실한 이해는 기본이구요, 동적 메모리 할당/해제도 왠만큼은 할 줄 알아야 합니다. 그리고 마지막으로 구조체도 알아야 하구요. 제가 열거한 이 세 가지 항목 중 하나라도 미숙하다고 생각되는 부분이 있다면, 연결리스트를 아무리 연구 해 봤자 다차 방정식도 모른채 미/적분을 공부하려는 것과 다르지 않지요.
번호 | 제 목 | 글쓴이 | 날짜 |
---|---|---|---|
2653014 | 전기 분야에서는 C언어가 어떤, 어느 용도로 쓰이나요? (7) | 처녀자리 | 2024-04-27 |
2652986 | c언어 프로그램~~ (5) | 가장 | 2024-04-27 |
2652907 | mciSendString 함수 플레이 시간, (1) | 해나 | 2024-04-26 |
2652880 | 배열입력을 묻고 싶습니다 (3) | 사에 | 2024-04-26 |
2652852 | strcmp함수 사용 관련,동적영역메모리접근관련 질문..이요. (1) | 민서 | 2024-04-26 |
2652801 | 안좋은 코딩 습관 지적좀요. | Addictive | 2024-04-25 |
2652779 | 문자열의 주소값 출력에 대해서 질문드립니다. | 핑크펄 | 2024-04-25 |
2652748 | 좀 도와주세요 ㅠ | 연체리 | 2024-04-25 |
2652719 | 다음프로그램의 실행결과를 알아보자.. (6) | 분홍이 | 2024-04-25 |
2652641 | 소인수프로그램 오류 (1) | 모람 | 2024-04-24 |
2652555 | 다이아 몬드 설명좀 부탁드립니다 | 갈기슭 | 2024-04-23 |
2652529 | 반복문을 .... (1) | 우시 | 2024-04-23 |
2652417 | 방향키로 커서이동 | 목소리 | 2024-04-22 |
2652339 | 구조체끼리 연결 왜 안될까요? (1) | 도담 | 2024-04-21 |
2652315 | 제발 도와주세요(실행파일까지첨부) | 은소라 | 2024-04-21 |
2652285 | 면산적님 질문요 (1) | 세찬 | 2024-04-21 |
2652253 | 다중루프를 이용한 이등변 삼각형 만들기 질문이요. | 알프레드 | 2024-04-20 |
2652226 | 결과 값이 무조건0 으로 나옵니다...... (5) | 빗줄기 | 2024-04-20 |
2652198 | 연결 list 질문요~ | Sweeety | 2024-04-20 |
2652168 | 배열 정렬에서.. (6) | 비 | 2024-04-20 |