문제 번호 1111. -- 데이터구조. SLL에 숫자 데이터 추가하기

1111: 데이터구조. SLL에 숫자 데이터 추가하기

시간 제한: 1 Sec  메모리 제한: 128 MB
제출: 1271  해결 문제 수: 805
[제출][채점 상황 열람][게시판]

문제 설명

주어진 정수들을 SLL에 저장하고, 차례로 출력하는 프로그램을 작성하시오.

입력

두 줄로 이루어져 있다. 첫 번째 줄에는 입력되는 숫자의 개수. 두 번째 줄에는 입력되는 숫자들이 공백으로 분리되어 주어진다.

출력

Singly linked list의 앞에서부터 차례로 저장되어 있는 숫자를 출력한다.

입력 예시

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;

}

출처

[제출][채점 상황 열람]