링크드리스트로 전화번호부 만들던중 질문입니다
글쓴이: ch0nn0m / 작성시간: 화, 2009/07/28 - 4:33오후
만들다가 컴파일 해본건데요
입력까진 문제없이 잘되는데...
삭제가 안되구요...출력하면...나오긴나오는데 젤 처음 입력한거는 출력에서 나오지 않네요..
#include<stdio.h> #include<stdlib.h> #include<string.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; }
Forums:
예전에 제가
예전에 제가 작성했던 링크드 리스트 예제입니다.
이 소스를 손으로 따라가면서 시작 포인터와 네모, 화살표로 그려보시면
본인 소스에서 문제가 되는 내용을 찾을 수 있을 것입니다.
참고로, 링크드 리스트의 시작 포인터(초기값 NULL)와 노드의 개수를 멤버로 가지는
구조체를 하나 선언해서 특정 개수일 때를 처리하시면 매우 편리합니다. ^^
----------------------
Go to the U-City
----------------------------------------------------------------------------------------
Don't Feed the Trolls!
----------------------------------------------------------------------------------------
일단 입력하고 출력부터 제대로.....
insert하고 print도 제대로 안되네요
댓글 달기