링크드리스트로 전화번호부 만들던중 질문입니다

ch0nn0m의 이미지

만들다가 컴파일 해본건데요

입력까진 문제없이 잘되는데...

삭제가 안되구요...출력하면...나오긴나오는데 젤 처음 입력한거는 출력에서 나오지 않네요..


#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;
}
freestyle의 이미지

예전에 제가 작성했던 링크드 리스트 예제입니다.

이 소스를 손으로 따라가면서 시작 포인터와 네모, 화살표로 그려보시면
본인 소스에서 문제가 되는 내용을 찾을 수 있을 것입니다.

참고로, 링크드 리스트의 시작 포인터(초기값 NULL)와 노드의 개수를 멤버로 가지는
구조체를 하나 선언해서 특정 개수일 때를 처리하시면 매우 편리합니다. ^^

/**
 */
 
#include <stdio.h>
#include <stdlib.h>
 
 
#define MALLOC_ERR_MSG "Not enough memory !!!"
#define NOTHING_INFO_MSG "Nothing !!!"
 
 
 
typedef struct str_node NODE;
 
struct str_node {
    int value;
    NODE *next;
};
 
 
void list_init(void);
void list_print(void);
 
 
void add_head(int value);
void add_tail(int value);
void del_head(void);
void del_tail(void);
 
 
int search(int s_val);
 
/* index : 1 ~ n+1 */
int insert(int index, int value);
 
/* index : 1 ~ n */
int delete(int index);
 
 
 
NODE *head;
 
int main(void)
{
    int i; // for add_head() test
 
 
    list_init();
    list_print();
 
 
    for(i = 1; i < 6; i++)
        add_head(i);
    list_print();
 
 
    del_head();
    list_print();
 
 
    for(i = 0; i < 10; i++)
        del_head();
    list_print();
 
 
    for(i = 6; i < 15; i++)
        add_tail(i);
    list_print();
 
 
    for(i = 0; i < 3; i++)
        del_tail();
    list_print();
 
 
    printf("search 8: %d\n", search(8));
 
 
    insert(7, 55);
    list_print();
 
 
    delete(2);
    list_print();
 
 
    for(i = 0; i < 20;i++)
        del_tail();
    list_print();
 
 
    return 0;
}
 
 
void list_init(void)
{
    head = NULL;
}
 
 
void list_print(void)
{
    NODE *node;
 
 
    if(head == NULL)
    {
        fprintf(stderr, "%s\n", NOTHING_INFO_MSG);
        return;
    }
 
    node = head;
    while(node != NULL)
    {
        printf("%d ", node->value);
        node = node->next;
    }
    printf("\n");
}
 
 
void add_head(int value)
{
    NODE *node;
 
 
    node = (NODE *)malloc(sizeof(NODE));
    if(node == NULL)
    {
        fprintf(stderr, "%s\n", MALLOC_ERR_MSG);
        exit(1);
    }
 
    node->value = value;
    node->next = head;
    head = node;
}
 
 
void add_tail(int value)
{
    NODE *node, *next_node;
 
 
    node = (NODE *)malloc(sizeof(NODE));
    if(node == NULL)
    {
        fprintf(stderr, "%s\n", MALLOC_ERR_MSG);
        exit(1);
    }
    node->value = value;
 
 
    next_node = head;
 
    if(next_node != NULL)
    {
        while(next_node->next != NULL)
            next_node = next_node->next;
 
        node->next = next_node->next;
        next_node->next = node;
    }
    else
    {
        node->next = head;
        head = node;
    }
}
 
 
void del_head(void)
{
    NODE *node;
 
 
    if(head == NULL) return;
 
 
    node = head;
    head = node->next;
    free(node);
}
 
 
void del_tail(void)
{
    NODE *node, *next_node;
 
 
    if(head == NULL) return;
 
 
    node = head;
    if(node->next == NULL)
    {
        head = NULL;
        free(node);
 
        return;
    }
 
 
    next_node = node->next;
    while(next_node->next != NULL)
    {
        node = next_node;
        next_node = next_node->next;
    }
 
    node->next = NULL;
    free(next_node);
}
 
 
int search(int s_val)
{
    NODE *temp = head;
    int idx = 1;
 
 
    if(temp == NULL)
        return -1;
 
    do {
        if(temp->value == s_val)
            return idx;
        temp = temp->next;
        idx++;
    } while(temp != NULL);
 
 
    return -1;
}
 
 
int insert(int index, int value)
{
    NODE *temp = head, *new, *prev_node;
 
 
    while(--index > 0)
    {
        prev_node = temp;
        temp = temp->next;
        if(temp == NULL && index != 1) return -1;
    }
 
 
    new = (NODE *)malloc(sizeof(NODE));
    if(new == NULL)
    {
        fprintf(stderr, "%s\n", MALLOC_ERR_MSG);
        exit(1);
    }
    new->value = value;
 
 
    if(head == temp)
    {
        new->next = head;
        head = new;
    }
    else
    {
        new->next = prev_node->next;
        prev_node->next = new;
    }
 
 
    return 0;
}
 
 
int delete(int index)
{
    NODE *temp = head, *prev_node;
 
 
    while(--index > 0)
    {
        prev_node = temp;
        temp = temp->next;
        if(temp == NULL) return -1;
    }
 
 
    if(head == NULL)
    {
        fprintf(stderr, "%s\n", NOTHING_INFO_MSG);
        return -1;
    }
    else if(head == temp)
    {
        head = head->next;
        free(temp);
    }
    else
    {
        prev_node->next = temp->next;
        free(temp);
    }
 
 
    return 0;
}

----------------------
Go to the U-City

----------------------------------------------------------------------------------------
Don't Feed the Trolls!
----------------------------------------------------------------------------------------

dalili의 이미지

insert하고 print도 제대로 안되네요

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.