단순연결리스트 코드 좀 봐주세요...
움찬
질문 제목 : 단순연결리스트 코드 좀 봐주세요...// testeet.cpp : 콘솔 응용 프로그램에 대한 진입점을 정의합니다.
//
#include stdafx.h
#include stdio.h
#include malloc.h
//#include stdlib.h
//#include limits.h
typedef int element;
typedef struct listnode{
element data;
struct listnode *link;
}listnode;
void insert_node(listnode **phead, listnode *p, listnode *new_node)
{
if(*phead==null)
{
new_node-link=null;
*phead=new_node;
}
else if( p==null)
{
new_node-link=*phead;
*phead=new_node;
}
else
{
new_node-link=p-link;
p-link=new_node;
}
}
void delete_node(listnode **phead, listnode *p, listnode *removed)
{
if( p==null )
*phead=(*phead)-link;
else
p-link=removed-link;
free(removed);
}
void display(listnode *head)
{
listnode *p=head;
while(p!=null)
printf(%d-, p-data);
p=p-link;
}
/*
listnode *search(listnode *head, int x)
{
listnode *p=head;
while(p!=null)
{
if(p-data==x) return p;
p=p-link;
}
return p;
}
listnode *reverse(listnode *head)
{
listnode *p, *q, *r;
p=head;
q=null;
while(p!=null)
{
r=q;
q=p;
p=p-link;
q-link=r;
}
return q;
}
*/
listnode *concat(listnode *head1, listnode *head2)
{
listnode *p;
if(head1==null) return head2;
else if(head2==null) return head1;
else
{
p=head1;
while(p-link!=null)
{
p=p-link;
}
p-link=head2;
}
return head1;
}
listnode *create_node(element data, listnode *link)
{
listnode *new_node=(listnode *)malloc(sizeof(listnode));
new_node-data=data;
new_node-link=link;
return new_node;
}
int main()
{
listnode *list1=null, *list2=null;
//listnode *p;
insert_node(&list1, null, create_node(10, null));
insert_node(&list1, null, create_node(20, null));
insert_node(&list1, null, create_node(30, null));
display(list1);
delete_node(&list1, null, list1);
display(list1);
insert_node(&list2, null, create_node(60, null));
insert_node(&list2, null, create_node(70, null));
insert_node(&list2, null, create_node(80, null));
display(list2);
list1=concat(list1, list2);
display(list1);
return 0;
}질문 내용 : 제가 원하는 출력은
30-20-10
20-10
80-70-60
20-10-80-70-60
인데 뭐가 문제인가요?
그리고 위 코드에서 밑줄 친 while(p-link!=null) 이 부분을 while(p!=null)로 바꾸면 결과에 영향을 미칠까요?
번호 | 제 목 | 글쓴이 | 날짜 |
---|---|---|---|
2692451 | 이 문제좀 풀어주세요 ^^ | 게자리 | 2025-04-23 |
2692424 | 2차원배열 자료입력질문이요! (1) | 똘끼 | 2025-04-22 |
2692401 | 유닉스안에서 C언어를 이용한 명함 만들기 입니다; 이해안가는 부분이있네요 | 2gether | 2025-04-22 |
2692374 | 고수님들 댓글 마니부탁해요!!! (2) | 엄지 | 2025-04-22 |
2692343 | scnaf에 자꾸 선언을 참조하라는데;; (8) | 도래 | 2025-04-22 |
2692282 | 도스상에서 생성된 exe파일에 press~ 뜨게 하기 (4) | 회사원 | 2025-04-21 |
2692256 | scanf("%*c"); ㅠㅠ 고수님들 | 거북이 | 2025-04-21 |
2692230 | 하노이탑 질문입니다. (1) | 미쁘다 | 2025-04-21 |
2692210 | 정보 올림피아드 문제인데.. 풀이 과정이 궁금합니다.(재귀함수) (5) | 물티슈 | 2025-04-20 |
2692144 | C언어와 리눅스에 대한 질문입니다. | 싴흐한세여니 | 2025-04-20 |
2692114 | 컨텍스트 스위칭하는데 걸리는 시간 측정.. | YourWay | 2025-04-19 |
2692086 | 간접참조 연산자, 증감연산자 질문이용! (2) | 블랙캣 | 2025-04-19 |
2692056 | 주석좀 달아주세요. 몇개적엇는데 몇개만달아주세요. (2) | DevilsTears | 2025-04-19 |
2691978 | 진수 쉽게 이해하는법... (3) | 지지않는 | 2025-04-18 |
2691949 | getchar() 한 문자를 입력받는 함수 질문 | 채꽃 | 2025-04-18 |
2691919 | 배열 정렬 및 합치기 질문입니다. | 사과 | 2025-04-18 |
2691845 | c언어왕초보 질문이 있습니다........ | 루나 | 2025-04-17 |
2691815 | void add(int num); 함수... (4) | 살랑살랑 | 2025-04-17 |
2691756 | 명령 프롬프트 스크롤바가 없어요 | 두메꽃 | 2025-04-16 |
2691725 | 자료구조에 관련해서 질문이 있어 글을 올립니다. | 누리알찬 | 2025-04-16 |