링크드리스트로 전화번호부 만들던중 질문입니다
로지
질문 제목 : 링크드리스트로 전화번호부 만들던중 질문입니다질문 내용 :
입력까진 문제없이 잘되는데...
삭제가 안되구요...출력하면...나오긴나오는데 젤 처음 입력한거는 출력에서 나오지 않네요..
#includestdio.h
#includestdlib.h
#includestring.h
typedef struct data
{
char name[20];
char number[20];
struct data *next;
}phone;
phone *head, *tail;
void init(void)
{
head=(phone*)malloc(sizeof(phone));
tail=(phone*)malloc(sizeof(phone));
head-next=tail;
tail-next=tail;
}
void insert()
{
phone *t;
t=(phone*)malloc(sizeof(phone));
printf(input name: );
scanf(%s,t-name);
fflush(stdin);
printf(input tel number: );
scanf(%s,t-number);
fflush(stdin);
printf(------- data inserted\n);
t-next=head-next;
head-next=t;
}
int remove(char *s)
{
phone *t,*p;
p=head;
t=p-next;
while(strcmp(s,t-name) !=0 && t!= tail)
{
p=p-next;
t=p-next;
}
if(t-next==tail)
return 0;
p-next=t-next;
free(s);
return 1;
}
void print()
{
phone *p;
p=head-next;
while(p-next!=tail)
{
printf(%s\t%s\n, p-name,p-number);
p=p-next;
}
}
int main(int argc,char *argv[])
{
init();
int a;
char buf[20];
while(1)
{
printf(-----menu-----\n);
printf( 1. insert\n);
printf( 2. remove\n);
printf( 3. search\n);
printf( 4. print all\n);
printf( 5. exit\n);
printf(choose the item: );
scanf(%d,&a);
getchar();
switch(a)
{
case 1:
insert();
break;
case 2:
printf(삭제할 이름: );
fgets(buf,20,stdin);
remove(buf);
break;
case 4:
print();
break;
}
}
return 0;
}