/*----------------------------------------------------------------------------------------- * Project Name : Report03 - Linkedlist * Programmed by : Young Min Jo * Student Number : 2008060021 * Date : Wednesday 19 Mar 2014 * (c) Copyright by Department of Computer Education at Chungbuk * National University, Chungbuk, Republic of Korea ------------------------------------------------------------------------------------------*/ #include "stdafx.h" #include #include //±¸Á¶Ã¼ Á¤ÀÇ typedef struct node{ int data; node *link; }node; /*-------------------------------- Retrieve_Linkedlist ------------------------------------ /* Function : Ž»ö ------------------------------------------------------------------------------------------*/ node *Retrieve_Linkedlist(node *Linkedlist, int input){ node *temp; temp = Linkedlist; while (temp != NULL){ if (temp->data == input){ return temp; } temp = temp->link; } return temp; } /*-------------------------------------- Insert ------------------------------------------- /* Function : »ðÀÔ ------------------------------------------------------------------------------------------*/ void Insert_Linkedlist(node **Linkedlist, int input){ node *temp; node *newnode; if (*Linkedlist == NULL){ temp = (node*)malloc(sizeof(node)); temp->data = input; temp->link = NULL; *Linkedlist = temp; } else { temp = *Linkedlist; //ù ³ëµå µ¥ÀÌÅÍ ¿Í input Å©±â ºñ±³ if (temp->data > input){ newnode = (node*)malloc(sizeof(node)); newnode->data = input; newnode->link = temp; *Linkedlist = newnode; } else{ while (temp->link != NULL){ //´ÙÀ½ ³ëµå µ¥ÀÌÅÍ Å©±â¿Í input ºñ±³ if (temp->link->data > input){ break; } temp = temp->link; } //input º¸´Ù Å« ¼ö°¡ ¾øÀ» °æ¿ì if (temp->link == NULL){ temp->link = (node*)malloc(sizeof(node)); temp = temp->link; temp->data = input; temp->link = NULL; } //³ëµå »çÀÌ¿¡ input °ªÀÌ µé¾î°¥ °æ¿ì else{ newnode = (node*)malloc(sizeof(node)); newnode->data = input; newnode->link = temp->link; temp->link = newnode; } } } } /*------------------------------ Delete_Linkedlist ---------------------------------------- /* Function : »èÁ¦ ------------------------------------------------------------------------------------------*/ void Delete_Linkedlist(node **Linkedlist, int input){ node *temp; node *check = Retrieve_Linkedlist(*Linkedlist, input); temp = *Linkedlist; if (check == NULL){ printf("»èÁ¦ÇÒ µ¥ÀÌÅÍ°¡ Á¸ÀçÇÏÁö ¾Ê½À´Ï´Ù.\n"); } else{ printf("%d »èÁ¦ ¿Ï·á.\n", input); if (temp == check){ temp = temp->link; *Linkedlist = temp; } else{ while (temp->link != check){ temp = temp->link; } temp->link = check->link; } } } /*------------------------------- Print_Linkedlist ---------------------------------------- /* Function : ÀúÀåµÈ ÀÚ·á Ãâ·Â ------------------------------------------------------------------------------------------*/ void Print_Linkedlist(node *Linkedlist){ node *temp; temp = Linkedlist; while (temp != NULL){ printf("%d\n", temp->data); temp = temp->link; } } /*--------------------------------------- Menu -------------------------------------------- /* Function : ¸Þ´º ------------------------------------------------------------------------------------------*/ void Menu(){ printf("****Linkedlist Menu****\n"); printf("1. Retrieve\n"); printf("2. Insert\n"); printf("3. Delete\n"); printf("4. Print\n"); printf("5. Exit\n\n"); } void main(){ int menu; int input; node *Linkedlist = NULL; node *check; while (1){ Menu(); printf("¸Þ´º¹øÈ£ : "); scanf("%d", &menu); if (menu == 1){ printf("\n***Retrieve***\n"); printf("°Ë»ö µ¥ÀÌÅÍ : "); scanf("%d", &input); check = Retrieve_Linkedlist(Linkedlist, input); if (check != NULL){ printf("***°Ë»ö °á°ú***\n"); printf("%d\n\n", check->data); } else{ printf("°Ë»ö ½ÇÆÐ\n\n"); } } else if (menu == 2){ printf("\n***Insert***\n"); printf("»ðÀÔ µ¥ÀÌÅÍ : "); scanf("%d", &input); Insert_Linkedlist(&Linkedlist, input); printf("\n"); } else if (menu == 3){ printf("\n***Delete***\n"); printf("»èÁ¦ µ¥ÀÌÅÍ : "); scanf("%d", &input); Delete_Linkedlist(&Linkedlist, input); } else if (menu == 4){ printf("****Print_Linkedlist****\n"); Print_Linkedlist(Linkedlist); printf("\n"); } else if (menu == 5){ exit(0); } else{ printf("¸Þ´º¹øÈ£¸¦ ´Ù½ÃÀÔ·ÂÇØÁÖ¼¼¿ä\n"); } } }