티스토리 뷰

 SLL에 숫자 데이터 추가

 

#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;
 
 }

 

 

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31