2013-04-24 71 views
0

我寫了一個程序作爲行組織者使用,但是當顯示一個人的名字的時候,它設置了最後一個人添加到所有其他人。我該如何解決它? 如果我更改struct中的信息,它會出錯;然而,如果有人能幫助我,我會很高興。鏈接列表C字符集姓氏添加到每個人

#include<stdio.h> 
#include<stdlib.h> 

struct node { 
    int priority; 
    int info; 
    struct node *link; 
} *front = NULL; 

void insert(char person[20], int person_priority); 
int del(); 
void display(); 
int isEmpty(); 

int main() //!! fixed 
{ 
    int choice, person_priority; 
    char person[20]; 

    while (1) { 
    printf("1.Insert Person\n"); 
    printf("2.Attend Client\n"); 
    printf("3.Show Queue\n"); 
    printf("4.Exit\n"); 
    printf("Type choice : "); 
    scanf("%d", &choice); 

    switch (choice) { 
    case 1: 
     printf("Type persons name:"); 
     scanf("%s", &person); 
     printf("Its Priority:\n1 Pregnant\n2 Older\n3 Standard: "); 
     scanf("%d", &person_priority); 
     insert(person, person_priority); 
     system("cls"); 
     break; 
    case 2: 
     system("cls"); 
     printf("Person was attended", del()); 
     break; 
    case 3: 
     system("cls"); 
     display(); 
     break; 
    case 4: 
     exit(1); 
    default: 
     printf("Invalid Choice\n"); 
    }       /*end of switch */ 
    }        /*end of while */ 
    return 0; //!! fixed 
}        /*end of main() */ 


void insert(char person[20], int person_priority) 
{ 
    struct node *tmp, *p; 

    tmp = (struct node *) malloc(sizeof(struct node)); 

    if (tmp == NULL) { 
    printf("No Memory available\n"); 
    return; 
    } 

    tmp->info = person; 
    tmp->priority = person_priority; 
/*Starting list*/ 
    if (isEmpty() || person_priority < front->priority) { 
    tmp->link = front; 
    front = tmp; 
    } 
    else { 
    p = front; 
    while (p->link != NULL && p->link->priority <= person_priority) 
     p = p->link; 
    tmp->link = p->link; 
    p->link = tmp; 
    } 
}        /*end of insere() */ 

int del() 
{ 
    struct node *tmp; 
    int person; 

    if (isEmpty()) { 
    printf("Empty Queue\n"); 
    exit(1); 
    } 
    else { 
    tmp = front; 
    person = tmp->info; 
    front = front->link; 
    free(tmp); 
    } 

    return person; 
}        /*end of del() */ 

int isEmpty() 
{ 
    if (front == NULL) 
    return 1; 
    else 
    return 0; 
}        /*end of emtpy verification (isEmpty()) */ 

void display() 
{ 
    struct node *ptr; 
    ptr = front; 
    if (isEmpty()) 
    printf("Empty Queu\n"); 
    else { 
    printf("Line :\n"); 
    printf("Priority  Name\n"); 
    while (ptr != NULL) { 
     printf("%5d  %5s\n", ptr->priority, ptr->info); 
     ptr = ptr->link; 
    } 
    printf("\n\n\n"); 
    } 
}  
+1

首先,'tmp-> info = person;',** ** **,'tmp-> info'是一個'int'。其次,你將所有的名字都讀入同一個數組中,讓'tmp-> info'指向'(如果sizeof(int) 2013-04-24 18:48:14

+0

那麼,我必須把「tmp-> info = person」放在什麼位置?任何想法如何複製名稱?我在這新...謝謝你。 – 2013-04-24 18:53:01

+0

如果您將'info *'作爲一個參數,則將'info'設爲'char *',然後查看'strlen','malloc'和'strcpy'來複制名稱。 – 2013-04-24 18:55:41

回答

0

你的結構節點有一個名爲「info」的元素,它的類型爲int。每次添加人員時,都使用scanf()將人員姓名讀入字符數組,然後將該字符數組的地址傳遞給insert(),將地址存儲在整數「info」中。你分配的每個struct node,你存儲的是同一個變量的地址。每次你scanf(),你都覆蓋相同的內存。

你的結構節點應該有一個元素char info[20],當你創建一個新節點時,你應該把strcpy()這個人的名字改成tmp-> info。

另請注意,您將交替處理變量爲char *類型和int類型的方式導致未定義的行爲。

+0

以及如何做到這一點?你可以請編碼這部分?我很想學習,但我無法理解一切。 – 2013-04-24 20:43:46