[자료구조]원형 리스트 질문
즐겨찾기
질문 제목 : [자료구조] 원형 리스트 질문질문 내용 : 생능출판사 천인국저 c언어로 풀어쓴 자료구조 에서 원형리스트 예제인데
#include stdio.h
#include stdlib.h
typedef int element;
typedef struct listnode{
element data;
struct listnode *link;
}listnode;
void error(char *message)
{
fprintf(stderr, %s\n, message);
exit(1);
}
listnode *create_node(element data, listnode *link)
{
listnode *new_node;
new_node=(listnode *)malloc(sizeof(listnode));
if(new_node==null)
error(error);
new_node-data=data;
new_node-link=link;
return (new_node);
}
void display(listnode *head)
{
listnode *p;
if(head==null)
return;
p=head;
do{
printf(%d-, p-data);
p=p-link;
}while(p!=head);
printf(\n);
}
void insert_first(listnode **phead, listnode *node)
{
if(*phead==null)
{
*phead=node;
node-link=node;
}
else
{
node-link=(*phead)-link;
(*phead)-link=node;
}
}
void insert_last(listnode **phead, listnode *node)
{
if(*phead==null){
*phead=node;
node-link=node;
}
else{
node-link=(*phead)-link;
(*phead)-link=node;
*phead=node;
}
}
main()
{
listnode *list1=null;
insert_first(&list1, create_node(10, null));
insert_first(&list1, create_node(20, null));
insert_last(&list1, create_node(30, null));
display(list1);
}
20-10-30- 출력되야하는데
위 코드로 하면 30-20-10- 출력됨....