3
1 2 3
1 2 3
이 문제에 대한 해답은 아래와 같다.
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *head;
void addToSLL(int n)
{
struct node *cur;
cur = (struct node *)malloc(sizeof(struct node));
cur->data = n;
cur->next = 0;
if (head == 0)
{
head = cur;
return;
}
else
{
struct node *temp = head;
while (temp->next != 0)
{
temp = temp->next;
}
temp->next = cur;
return;
}
}
void showSLL()
{
struct node *cur = head;
while (cur != 0)
{
printf("%d ", cur->data);
cur = cur->next;
}
return;
}
int main(void)
{
int num;
int data;
int i;
scanf("%d", &num);
for (i = 0; i < num; i++)
{
scanf("%d", &data);
addToSLL(data);
}
showSLL();
return 0;
}