짝수가 먼저 출력이 되고, 홀수가 나중에 출력이 되는 Linked_List
728x90
#include <stdio.h>
#include <stdlib.h>
typedef struct link {
int key;
struct link *next;
} link_list;
link_list *head;
link_list *tail;
int count = 1;
void insert_node() {
link_list *temp, *p = head;
temp = (link_list *)malloc(sizeof(link_list));
temp->key = count++;
switch(temp->key % 2) {
case 0:
while(p->next != tail && (p->next->key % 2) == 0) {
p = p->next;
}
break;
case 1:
while(p->next != tail) {
p = p->next;
}
break;
}
temp->next = p->next;
p->next = temp;
}
void delete_node(int key) {
link_list *temp, *p;
p = head;
if(head->key == key) {
temp = head;
head = head->next;
} else {
while(true) {
if(p->next->key == key) {
temp = p->next;
p->next = p->next->next;
break;
} else if(p->next == NULL){
printf("데이터가 존재하지 않습니다");
return;
} else {
p = p->next;
}
}
}
free(temp);
}
void main() {
link_list *pointer;
head = (link_list *)malloc(sizeof(link_list));
tail = (link_list *)malloc(sizeof(link_list));
head->next = tail;
tail->next = NULL;
for(int i = 0 ; i < 100 ; i++) {
insert_node(); // ①
}
do {
head = head->next;
printf("%d ",head->key);
}while(head->next != tail);
}
#include <stdlib.h>
typedef struct link {
int key;
struct link *next;
} link_list;
link_list *head;
link_list *tail;
int count = 1;
void insert_node() {
link_list *temp, *p = head;
temp = (link_list *)malloc(sizeof(link_list));
temp->key = count++;
switch(temp->key % 2) {
case 0:
while(p->next != tail && (p->next->key % 2) == 0) {
p = p->next;
}
break;
case 1:
while(p->next != tail) {
p = p->next;
}
break;
}
temp->next = p->next;
p->next = temp;
}
void delete_node(int key) {
link_list *temp, *p;
p = head;
if(head->key == key) {
temp = head;
head = head->next;
} else {
while(true) {
if(p->next->key == key) {
temp = p->next;
p->next = p->next->next;
break;
} else if(p->next == NULL){
printf("데이터가 존재하지 않습니다");
return;
} else {
p = p->next;
}
}
}
free(temp);
}
void main() {
link_list *pointer;
head = (link_list *)malloc(sizeof(link_list));
tail = (link_list *)malloc(sizeof(link_list));
head->next = tail;
tail->next = NULL;
for(int i = 0 ; i < 100 ; i++) {
insert_node(); // ①
}
do {
head = head->next;
printf("%d ",head->key);
}while(head->next != tail);
}