단일연결리스트 질문이요 !!
바로찬글
질문 제목 : 입력부분에 문제가 있는것 같은데
잘 모르겠어서 여기에 질문해 봅니다..ㅠㅠ / insert() 요 부분이요!단일연결리스트를 동적할당을 통해서 입력시켜볼려구하는데 어렵네요 ㅠ질문 내용 :
#include stdio.h
#include string.h
#include stdlib.h
typedef struct book{
char name[10];
char number[20];
char adr[50];
struct book *link;
}book;
struct book *head = null;
struct book *tail = null;
struct book *temp = null;
void insert();
void print();
void search();
void del();
void main()
{
int n=0;
while(1)
{
printf( ★★★주소록 프로그램 ★★★\n);
printf( ★★원하시는 기능을 선택 해주세요★★\n);
printf( 1.입력 \n);
printf( 2.출력 \n);
printf( 3.검색 \n);
printf( 4.삭제 \n);
printf( 5.종료 \n);
printf( );
scanf_s(%d,&n);
switch(n)
{
case 1:
system(cls);
insert();
break;
case 2:
system(cls);
print();
break;
case 3:
system(cls);
search();
break;
case 4:
system(cls);
del();
break;
case 5:
printf(프로그램을 종료합니다. \n);
exit(1);
default:
printf(1~5 숫자만 입력하세요.\n);
}
}
}
void insert()
{
book *item;
item = head;
temp=(book *)malloc(sizeof(book));
printf(이름:);
scanf_s(%s, head-name);
printf(\n);
printf(번호:\n);
scanf_s(%s, head-number);
printf(\n);
printf(주소:\n);
scanf_s(%s, head-adr);
printf(\n);
temp-link = null;
if(head==null){
head = temp;
}
else{
while(item != null)
{
item-link = temp;
}
}
}
void print()
{
book *item;
item = head;
printf(저장된 주소록\n);
if(item == null){
printf(출력할 값이 없습니다.\n);
return;
}
while(item != null)
{
printf(이름: %s, item-name);
printf(주소: %s, item-adr);
printf(전화번호: %s, item-number);
item = item-link;
}
}
void search()
{
char search[10];
book *item;
item = head;
if(item == null){
printf(검색할 값이 없습니다.\n);
return;
}
printf(검색할 주소록의 이름 입력: );
scanf_s(%s, search);
while(item != null)
{
if(strcmp(item-name,search) == 0){
printf(이름: %s, item-name);
printf(주소: %s, item-adr);
printf(전화번호: %s, item-number);br /r);
return;
}
}
printf(일치하는 주소록이 없습니다.);
}
void del()
{
char search[10];
book *item;
item = head;
temp=(book *)malloc(sizeof(book));
if(item == null){
printf(삭제할 값이 없습니다.\n);
return;
}
printf(주소록에서 삭제할 이름: );
scanf(%s, search);
if(strcmp(head-name, search) == 0){
head = head-link;
printf(삭제 완료);
return;
}
while(item != null)
{
if(strcmp(item-name, search) == 0){
temp = (item-link)-link;
free(item-link);
item-link = temp;
printf(삭제완료);
return;
}
}
printf(일치하는 이름이 없습니다.);
}