!!!!살고시습니다. 공중제비를멈추게하십시오!!!!!!!!!!
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct node {
int id;
char status[100]; // Active, InActive, Maintenance
char name[100];
struct node *prev;
struct node *next;
};
struct node *head = NULL;
struct node *tail = NULL;
void push(int id, char status[], char name[]) {
struct node *new_node = malloc(sizeof(struct node));
struct node *now = head;
while (now != NULL) {
if (!strcmp(now->name, name)) {
printf("중복된 이름입니다.");
return;
}
now = now->next;
}
if (head == NULL) {
new_node->id=id;
strcpy(new_node->status, status);
strcpy(new_node->name, name);
head = new_node;
tail = new_node;
}
else {
new_node->id=id;
strcpy(new_node->status, status);
strcpy(new_node->name, name);
tail->next = new_node;
new_node->prev = tail;
tail = new_node;
}
}
void search() {
struct node *now = head;
int search_sel;
printf("1은 ID로 검색, 2는 이름으로 검색");
scanf("%d", &search_sel);
if (search_sel == 1) {
int search_id;
printf("ID : ");
scanf("%d", &search_id);
while (now != NULL) {
if (now->id == search_id) {
printf("ID : %d ", now->id);
printf("STATUS : %s ", now->status);
printf("NAME : %s\\n", now->name);
}
now = now->next;
}
}
if (search_sel == 2) {
char search_name[100];
printf("NAME : ");
scanf("%s", search_name);
while (now != NULL) {
char nowname[100];
strcpy(nowname, now->name);
for (int i = 0; i < strlen(nowname); i++) {
if (nowname[i] >= 'A' && nowname[i] <= 'Z') {
nowname[i] += 32;
}
}
for (int i = 0; i < strlen(search_name); i++) {
if (search_name[i] >= 'A' && search_name[i] <= 'Z') {
search_name[i] += 32;
}
}
if (!strcmp(nowname, search_name)) {
printf("ID : %d ", now->id);
printf("STATUS : %s ", now->status);
printf("NAME : %s\\n", now->name);
}
now = now->next;
}
}
}
void pop() {
struct node *now = head;
while (now->next != NULL) {
now = now->next;
}
now->prev->next = NULL;
}
void print_node() {
struct node *now = head;
while (now != NULL) {
printf("ID : %d ", now->id);
printf("STATUS : %s ", now->status);
printf("NAME : %s\\n", now->name);
now = now->next;
}
}
void delete() {
struct node *now = head;
int delete_id;
printf("ID : ");
scanf("%d", &delete_id);
while (now != NULL) {
if (now->id == delete_id) {
now->prev->next = now->next;
now->next->prev = now->prev;
}
now = now->next;
}
}
void delete_via_status() {
struct node *now = head;
int statuscode;
char delete_via_status[100];
printf("\\nSTATUS 1은 Active, 2는 InActive, 3은 Maintenance : ");
scanf("%d", &statuscode);
if (statuscode == 1) {
strcpy(delete_via_status, "Active");
}
else if (statuscode == 2) {
strcpy(delete_via_status, "InActive");
}
else if (statuscode == 3) {
strcpy(delete_via_status, "Maintenance");
}
while (now != NULL) {
if (!strcmp(now->status, delete_via_status)) {
now->prev->next = now->next;
now->next->prev = now->prev;
}
now = now->next;
}
}
int main() {
int now_id = 0;
while(1) {
int select;
printf("\\n1은 삽입, 2는 삭제, 3은 검색, 4는 POP, 5는 출력, 6은 특성 상태 삭제");
scanf("%d", &select);
if (select == 1) { // 삽입
int statuscode;
char status[50], name[50];
printf("\\nSTATUS 1은 Active, 2는 InActive, 3은 Maintenance : ");
scanf("%d", &statuscode);
if (statuscode == 1) {
strcpy(status, "Active");
}
else if (statuscode == 2) {
strcpy(status, "InActive");
}
else if (statuscode == 3) {
strcpy(status, "Maintenance");
}
printf("\\nNAME : ");
scanf("%s", name);
push(++now_id, status, name);
}
else if (select == 2) { //delete
delete();
}
else if (select == 3) { //search
search();
}
else if (select == 4) { //pop
pop();
printf("\\nPOP!");
}
else if (select == 5) { // 출력
print_node();
}
else if (select == 6) { // 특정 상태 삭제
delete_via_status();
}
}
}