단순 연결 리스트인데 출력결과가 이상하게 나와요.
찬늘봄
질문 제목 : 질문 내용 :
#include stdio.h
#include stdlib.h
#include string.h
#define true 1
#define false 0
typedef struct listnodetype
{
int data;
struct listnodetype* plink;
} listnode;
typedef struct linkedlisttype
{
int currentelementcount;// 현재 저장된 원소의 개수
listnode headernode;// 헤더 노드(header node)
} linkedlist;
linkedlist* creaditlist(linkedlist* plink);
int addelement(linkedlist* plink,int position,int data);
void display(linkedlist* plink);
listnode* getelement(linkedlist* plink,int position);
int getlenth(linkedlist* plink);
int main()
{
linkedlist* plink=null;
plink=creaditlist(plink);
//listnode data;
//원소추가
if(plink != null){
addelement(plink,0,1);
addelement(plink,1,3);
addelement(plink,2,5);
display(plink);
}
return 0;
}
linkedlist* creaditlist(linkedlist* plink)
{
plink=(linkedlist*)malloc(sizeof(linkedlist));
if(plink == null){
puts(헤더노드 메모리 할당 오류!);
return null;
}
memset(plink,0,sizeof(linkedlist));
return plink;
}
int addelement(linkedlist* plink,int position,int data)
{
int ret=false;
int i;
listnode* pnewnode=null;
listnode* pprenode=null;
if(plink != null)
{
if(position = 0 &&
position = plink-currentelementcount){
//새로운원소메모리할당
pnewnode=(listnode*)malloc(sizeof(listnode));
if(pnewnode != null){
pnewnode-data = data;
pnewnode-plink=null;
}
else{
puts(새로운 메모리 할당 오류!);
return ret;
}
//노드탐색
pprenode=&(plink-headernode);
for(i=0; iposition ; i++){
pprenode=pprenode-plink;
}
//노드관계재정립
pnewnode-plink=pprenode-plink;
pprenode-plink=pnewnode;
plink-currentelementcount++;
ret=true;
}
else{
printf(인덱스 오류!\n);
printf(현재 인덱스: [%d], 입력 인덱스: [%d]\n,
plink-currentelementcount,position);
}
}
else{
puts(메모리 할당 오류!);
}
return ret;
}
void display(linkedlist* plink)
{
int i,arrlen;
printf(현재 원소 개수: %d\n,plink-currentelementcount);
arrlen=getlenth(plink);
for(i=0; iarrlen; i++){
printf([%d]: %d\n, i,getelement(plink,i)-data);
}
}
listnode* getelement(linkedlist* plink,int position)
{
int i;
listnode* node=null;
listnode* preturn=null;
if(plink != null){
if(position =0 &&
position plink-currentelementcount){
node=&(plink-headernode);
for(i=0; iposition; i++){
node=node-plink;
}
preturn=node;
}
}
return preturn;
}
int getlenth(linkedlist* plink)
{
int len=0;
if(plink != null){
len=plink-currentelementcount;
}
return len;
}
[0]: 1
[1]: 3
[2]: 5
나와야하는데 0,1,3
으로 나오는데..
왜 그런거죠.?
번호 | 제 목 | 글쓴이 | 날짜 |
---|---|---|---|
2700368 | if에 관해서 질문이요... | Orange | 2025-07-04 |
2700339 | 이거 결과값이 왜이런건지.. (4) | 그댸와나 | 2025-07-04 |
2700313 | 파일 읽어서 저장하는데 빈파일일 경우 문재가 발생하네요.. (2) | 크나 | 2025-07-03 |
2700287 | 구조체 동적할당 연습을 하는데 오류가 뜹니다...(해결) (3) | 아련나래 | 2025-07-03 |
2700264 | 문자와 숫자 동시에 입력??? | 글고운 | 2025-07-03 |
2700236 | txt파일로만 쓰고 읽게 하려면 어떻게 해야 하나요..?? (8) | 미국녀 | 2025-07-03 |
2700211 | 전위 연산자 (2) | 어른처럼 | 2025-07-02 |
2700183 | C에서 파일이름을 받고, 그 파일의 사이즈를 출력해줘야하는데 내용이 출력이 안되네요 ;ㅅ; | 피스케스 | 2025-07-02 |
2700150 | 꼭좀 도와주세요ㅠㅠㅠ | 호습다 | 2025-07-02 |
2700095 | 연산문제...질문... | 오빤테앵겨 | 2025-07-01 |
2700070 | while문 , 3의배수 출력하는 프로그램좀 짜주세욤. | 횃불 | 2025-07-01 |
2700041 | 초보인데요 ㅎ 배열안에 배열을 집어넣을수 있나요?? | 헛장사 | 2025-07-01 |
2700012 | 배열// (1) | 전갈자리 | 2025-07-01 |
2699895 | 무한루프에 빠집니다.!! 해결좀부탁드려요 (10) | 선아 | 2025-06-30 |
2699842 | 질문을 너무 많이 하네여.....죄송.... (2) | 해님꽃 | 2025-06-29 |
2699816 | 오류 질문입니다.. (1) | 해비치 | 2025-06-29 |
2699763 | 질문입니다 ! 꼭 좀 도와주세요ㅠㅠ (2) | 미라 | 2025-06-28 |
2699555 | c언어 다항식을 입력을 했는데 왜 출력이 안될까요? | 피스케스 | 2025-06-27 |
2699528 | C언어 포인터연산 질문입니다. (3) | 안녕나야 | 2025-06-26 |
2699476 | 끌어올림;;달력 짜봤는데요 이 소스 줄일 수 있나요? - 스샷첨부 (2) | 클라우드 | 2025-06-26 |