Skip to content

Instantly share code, notes, and snippets.

@hanjae-jea
Last active June 7, 2018 09:01
Show Gist options
  • Save hanjae-jea/c18914f04595cdc82c23c03bc1dbc11e to your computer and use it in GitHub Desktop.
Save hanjae-jea/c18914f04595cdc82c23c03bc1dbc11e to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct student {
int id;
char name[20];
student *next;
}student;
int main() {
// 예제 1
/*int n;
scanf("%d", &n);
int *arr = (int*)malloc(n * sizeof(int));
for (int i = 0; i < n; i++) {
*(arr + i) = i;
printf("%d\n", *(arr + i));
}*/
// 예제 2
struct student hanjae;
hanjae.id = 2014210110;
strcpy(hanjae.name, "제한재");
printf("학번: %d, 이름: %s\n", hanjae.id, hanjae.name);
// 예제 3
struct student *study = (struct student*)malloc(9 * sizeof(struct student));
study->id = 2018320202; // (study+0) == study
strcpy(study->name, "김재민");
(study + 1)->id = 2018320210;
strcpy((study + 1)->name, "정윤지");
printf("%d %d\n", (study + 1)->id, study[1].id); // study[1].id -> (*(study+1)).id
// 예제 4
hanjae.next = &study[0]; // study[0] : 재민
// hanjae.next = study;
study[0].next = &study[1]; // 정윤지
study[1].next = NULL;
student* t = &hanjae;
printf("예제 4\n");
while (t != NULL) {
printf("%d %s\n", t->id, t->name);
t = t->next;
}
// 예제 5
student *gunwoo = (student*)malloc(sizeof(student));
gunwoo->id = 2016320143;
strcpy(gunwoo->name, "이건우");
gunwoo->next = hanjae.next; // 김재민
hanjae.next = gunwoo;
student* start = &hanjae;
t = start;
printf("예제 5\n");
while (t != NULL) {
printf("%d %s\n", t->id, t->name);
t = t->next;
}
// 예제 6
// gunwoo->next = &study[1];
gunwoo->next = gunwoo->next->next;
printf("예제 6\n");
t = start;
while (t != NULL) {
printf("%d %s\n", t->id, t->name);
t = t->next;
}
// 예제 7
printf("예제 7\n");
// 출력
t = start;
while (t != NULL) {
printf("%d %s\n", t->id, t->name);
t = t->next;
}
printf("==========\n");
while (true) {
/*student nn; // -> 이런식으로 하면 메모리 주소가 항상 같더라.
student *new_student = &nn; */
student *new_student = (student*)malloc(sizeof(student));
printf("새 학생의 메모리 주소 = %x\n", new_student);
printf("학번을 입력하세요 = ");
scanf("%d", &new_student->id);
if (new_student->id == 0) break;
printf("이름을 입력하세요 = ");
scanf("%s", new_student->name);
new_student->next = NULL;
t = start;
if (new_student->id < start->id) {
// 맨 앞에 학생이 추가될때
new_student->next = start;
start = new_student;
}
else {
// 중간에 학생이 추가될때
while (t->next != NULL) {
if (t->id < new_student->id && new_student->id < t->next->id)
break;
t = t->next;
}
new_student->next = t->next;
t->next = new_student;
}
}
printf("========\n");
t = start;
while (t != NULL) {
printf("%d %s\n", t->id, t->name);
t = t->next;
}
// 예제 8
printf("예제 8\n");
int num;
printf("지울 학생의 학번을 입력 = ");
scanf("%d", &num);
if (start->id == num) {
start = start->next;
}
else {
t = start;
while (t != NULL) {
if (t->next != NULL && t->next->id == num) {
t->next = t->next->next;
break;
}
}
}
printf("========\n");
t = start;
while (t != NULL) {
printf("%d %s\n", t->id, t->name);
t = t->next;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment