숫자를 입력 받고 그만큼 노드를 생성한뒤에
차례로 그 노드의 데이터들을 출력하는 코드입니다. 컴파일은 되는데 실행이 안되는거 보니
print 함수에서 뭐가 잘못된거같은데 도와주세용 ㅜㅜ
#include <stdio.h>
#include <stdlib.h>
void add(struct node *,int,struct node *);
void print(struct node *head,struct node *p);
void erase(struct node *head,struct node *p);
typedef struct node{
int data;
struct node *link;
}node;
int main(void)
{
node *head=NULL;
node *p;
int data,n;
while(1)
{
printf(\"양의 정수를 입력하세요(종료: -1): \");
scanf(\"%d\",&data);
if(data==-1)
break;
else
add(head,data,p);
n++;
}
print(head,p);
erase(head,p);
}
void add(struct node *head, int data,struct node *p)
{
node *newnode= (node *)malloc(sizeof(node));
newnode->data=data;
newnode->link=NULL;
if(head == NULL)
{
head=newnode;
p=head;
}
else
p->link=newnode;
p=newnode;
}
void print(struct node *head,struct node *p)
{
p=head;
while(p->link!= NULL)
{
printf(\"%d->\",p->data);
p=p->link;
}
printf(\"NULL\");
}
void erase(struct node *head,struct node *p)
{
node *erase;
p=head;
while(p->link!= NULL)
{
erase=p->link;
free(p);
p=erase;
}
}
차례로 그 노드의 데이터들을 출력하는 코드입니다. 컴파일은 되는데 실행이 안되는거 보니
print 함수에서 뭐가 잘못된거같은데 도와주세용 ㅜㅜ
#include <stdio.h>
#include <stdlib.h>
void add(struct node *,int,struct node *);
void print(struct node *head,struct node *p);
void erase(struct node *head,struct node *p);
typedef struct node{
int data;
struct node *link;
}node;
int main(void)
{
node *head=NULL;
node *p;
int data,n;
while(1)
{
printf(\"양의 정수를 입력하세요(종료: -1): \");
scanf(\"%d\",&data);
if(data==-1)
break;
else
add(head,data,p);
n++;
}
print(head,p);
erase(head,p);
}
void add(struct node *head, int data,struct node *p)
{
node *newnode= (node *)malloc(sizeof(node));
newnode->data=data;
newnode->link=NULL;
if(head == NULL)
{
head=newnode;
p=head;
}
else
p->link=newnode;
p=newnode;
}
void print(struct node *head,struct node *p)
{
p=head;
while(p->link!= NULL)
{
printf(\"%d->\",p->data);
p=p->link;
}
printf(\"NULL\");
}
void erase(struct node *head,struct node *p)
{
node *erase;
p=head;
while(p->link!= NULL)
{
erase=p->link;
free(p);
p=erase;
}
}
연결리스트? 너 혹시 학교가...