2013-04-24 48 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