링크더리스트 이전 링크값 출력함수.
너만을
질문 제목 : 링크더리스트 이전 링크값 출력함수.혼자 과제삼아 링크더리스트 이전값 출력함수를 만들었는데.. 원하는데로 출력이 안되네요.
원인이 뭔가요! 질문 내용 : 링크의 끝을 헤드와 연결하여 현재값의 위치를 확인하고 확인된 위치에서 한칸뒤의 값을 저장하고 링크의 끝을 다시 null로 되돌려 주면서 함수를 끝내는것입니다.
그런데 생각데로 안되고 출력값은 현재값과 같게 출력되며 마지막 출력값이 나오기 직전에 도스창이 종료되네요... ㅠㅠ
함수!
int fpn(node* ade)
{
char count;
node* now2;
node* now1;
now1=ade;
now2=ade;
count=0;
while(1)
{
now1=now1-next;
if(now1-next==null)
{
now1-next=head;
}
count++;
if(now1==ade)
{
for(count;count==1;--count)
{
now1=now1-next;
}
while(1)
{
now2=now2-next;
if(now2-next==head)
{
now2-next=null;
break;
}
}
return now1-value;
}
}
}
--------------------------전체 코드
#include stdio.h
#includemalloc.h
#includestring.h
// 노드 구조체
#define true 1
#define false 0
typedef struct node
{
int value;
struct node *next;
}node;
node *head;
int fpn(node* ade)
{
char count;
node* now2;
node* now1;
now1=ade;
now2=ade;
count=0;
while(1)
{
now1=now1-next;
if(now1-next==null)
{
now1-next=head;
}
count++;
if(now1==ade)
{
for(count;count==1;--count)
{
now1=now1-next;
}
while(1)
{
now2=now2-next;
if(now2-next==head)
{
now2-next=null;
break;
}
}
return now1-value;
}
}
}// 연결 리스트 초기화 - 머리를 할당한다.
void initlist()
{
head=(node *)malloc(sizeof(node));
head-next=null;
}
// target 다음에 노드를 삽입한다.
node *insertnode(node *target,node *anode)
{
node *new;
new=(node *)malloc(sizeof(node));
*new=*anode;
new-next=target-next;
target-next=new;
return new;
}
// target 다음 노드를 삭제한다.
int deletenode(node *target)
{
node *del;
del=target-next;
if (del==null) {
return false;
}
target-next=del-next;
free(del);
return true;
}
// 연결 리스트의 모든 노드와 머리를 해제한다.
void uninitlist()
{
while (deletenode(head)) {;}
free(head);
head=null;
}
void main()
{
int i;
node *now,temp;
initlist();
// 다섯 개의 노드 삽입
now=head;
for (i=1;i=5;i++) {
temp.value=i;
now=insertnode(now,&temp);
}
// 두 번째 노드 삭제
deletenode(head-next);
// 순회하면서 출력
for (now=head-next;now;now=now-next) {
printf(%d\t,now-value);
printf(%d\n,fpn(now));
}
printf(\n);
uninitlist();
}