작업 환경
- OS : Ubuntu 14.04.5 LTS
문제
전화번호부를 이중연결리스트로 완성하시오. (단 등록은 두가지 방법으로 만드시오.)
1) 등록(FIFO , LIFO)
2) 출력
3) 검색
4) 수정
5) 삭제
my.h
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "list.h"
list.h
#ifndef __LIST_H__
#define __LIST_H__
#define MAX_NAME 30
#define MAX_NUMBER 20
typedef struct Data
{
char name[MAX_NAME];
char pnumber[MAX_NUMBER];
}Data;
typedef struct Node
{
Data *data;
struct Node *next;
struct Node *prev;
}Node;
typedef struct List
{
Node *head;
Node *tail;
}List;
Node *make_node();
void list_init(List *list);
void list_push_back(List *list);
void list_push_front(List *list);
void list_all(List *list);
void list_find(List *list);
void list_modi(List *list);
void list_erase(List *list);
void list_allerase(List *list);
#endif
list.c
#include "my.h"
void list_init(List *list)
{
list->head = NULL;
list->tail = NULL;
}
Node *make_node()
{
int num;
Data *data = (Data *)malloc(sizeof(Data));
Node *newNode = (Node *)malloc(sizeof(Node));
newNode->data = data;
newNode->next = NULL;
newNode->prev = NULL;
fputs("Input name : " , stdout);
fgets(data->name , sizeof(data->name)-1 , stdin);
fputs("Input pnumber : " , stdout);
fgets(data->pnumber , sizeof(data->pnumber)-1 , stdin);
return newNode;
}
void list_push_back(List *list)
{
Node *node = make_node();
if(list->head == NULL && list->tail == NULL)
{
list->head = node;
list->tail = node;
}
else if(list->tail != NULL)
{
list->tail->next = node;
node->next = list->tail;
node->next = NULL;
list->tail = node;
}
}
void list_push_front(List *list)
{
Node *node = make_node();
if(list->head == NULL && list->tail == NULL)
{
list->head = node;
list->tail = node;
}
else if(list->head != NULL)
{
list->head->prev = node;
node->next = list->head;
node->prev = NULL;
list->head = node;
}
}
void list_all(List *list)
{
Node *cur;
cur = list->head;
while(cur != NULL)
{
printf("-----------------------------\n");
fputs(cur->data->name , stdout);
fputs(cur->data->pnumber , stdout);
printf("-----------------------------\n");
cur = cur->next;
}
}
void list_find(List *list)
{
char fname[MAX_NAME];
char fnumber[MAX_NUMBER];
Node *cur;
cur = list->head;
fputs("Input name : " , stdout);
fgets(fname , sizeof(fname)-1 , stdin);
fputs("Input number : " , stdout);
fgets(fnumber , sizeof(fnumber)-1 , stdin);
while(cur != NULL)
{
if(!strcmp(cur->data->name , fname) && !strcmp(cur->data->pnumber , fnumber))
{
printf("-----------------------------\n");
fputs(cur->data->name , stdout);
fputs(cur->data->pnumber , stdout);
printf("-----------------------------\n");
}
cur = cur->next;
}
}
void list_modi(List *list)
{
char fname[MAX_NAME];
char fnumber[MAX_NUMBER];
Node *cur;
cur = list->head;
fputs("Input Fname : " , stdout);
fgets(fname , sizeof(fname)-1 , stdin);
fputs("Input Fnumber : " , stdout);
fgets(fnumber , sizeof(fnumber)-1 , stdin);
while(cur != NULL)
{
printf("-----------------------------\n");
if(!strcmp(cur->data->name , fname) && !strcmp(cur->data->pnumber , fnumber))
{
fputs("Input Rname : " , stdout);
fgets(cur->data->name , sizeof(cur->data->name)-1 , stdin);
fputs("Input Rpnumber : " , stdout);
fgets(cur->data->pnumber , sizeof(cur->data->pnumber)-1 , stdin);
}
cur = cur->next;
}
}
void list_erase(List *list)
{
char fname[MAX_NAME];
char fnumber[MAX_NUMBER];
Node *del;
Node *delNext;
del = list->head;
delNext = list->head->next;
fputs("Input Dname : " , stdout);
fgets(fname , sizeof(fname)-1 , stdin);
fputs("Input Dpnumber : " , stdout);
fgets(fnumber , sizeof(fnumber)-1 , stdin);
while(del != NULL)
{
if(!strcmp(del->data->name , fname) && !strcmp(del->data->pnumber , fnumber))
{
if(del->prev == NULL && del->next == NULL)
{
list->head = NULL;
list->tail = NULL;
free(del);
break;
}
else if(del->prev == NULL && del->next != NULL)
{
list->head = delNext;
free(del);
del = delNext;
delNext = del->next;
del->prev = NULL;
break;
}
else if(del->prev != NULL && del->next != NULL)
{
del->prev->next = delNext;
delNext->prev = del->prev;
free(del);
break;
}
else if(del->prev != NULL && del->next == NULL)
{
del->prev->next = delNext;
list->tail = del->prev;
free(del);
break;
}
}
else
{
del = delNext;
if(del != NULL)
delNext = del->next;
}
}
}
void list_allerase(List *list)
{
Node *del;
del = list->head;
while(list->head != NULL)
{
list->head = del->next;
free(del);
del = list->head;
}
list->tail = NULL;
printf("----------data allremove---------\n");
}
main.c
#include "my.h"
void mainView();
void list_add_fifo();
void list_add_lifo();
void list_allsearch();
void list_search();
void list_modify();
void list_remove();
void list_allremove();
List list;
int main()
{
list_init(&list);
mainView();
return 0;
}
void mainView()
{
int choice;
while(1)
{
// system("clear");
printf("[1]add(FIFO) ");
printf("[2]add(LIFO) ");
printf("[3]listall ");
printf("[4]search ");
printf("[5]modify ");
printf("[6]remove ");
printf("[7]allremove ");
printf("[0]exit\n");
scanf("%d" , &choice);
switch(choice)
{
case 1:
list_add_fifo();
break;
case 2:
list_add_lifo();
break;
case 3:
list_allsearch();
break;
case 4:
list_search();
break;
case 5:
list_modify();
break;
case 6:
list_remove();
break;
case 7:
list_allremove();
break;
case 0:
return;
}
}
}
void list_add_fifo()
{
list_push_front(&list);
}
void list_add_lifo()
{
list_push_back(&list);
}
void list_allsearch()
{
list_all(&list);
}
void list_search()
{
list_find(&list);
}
void list_modify()
{
list_modi(&list);
}
void list_remove()
{
list_erase(&list);
}
void list_allremove()
{
list_allerase(&list);
}
실행결과
1. 등록(LIFO , FIFO) 및 조회
2. 검색 및 수정
3. 삭제 및 종료
'Programming Skill > Data Structure & Algorithm' 카테고리의 다른 글
[알고리즘] 삽입정렬(Insertion Sort) (0) | 2017.01.30 |
---|---|
[알고리즘] 선택정렬(Selection Sort) (0) | 2017.01.30 |
[알고리즘] 버블정렬(Bubble Sort) (0) | 2017.01.30 |
[알고리즘] 간접정렬(Indirect Sort) (0) | 2017.01.29 |