我寫了一個程序作爲行組織者使用,但是當顯示一個人的名字的時候,它設置了最後一個人添加到所有其他人。我該如何解決它? 如果我更改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");
}
}
首先,'tmp-> info = person;',** ** **,'tmp-> info'是一個'int'。其次,你將所有的名字都讀入同一個數組中,讓'tmp-> info'指向'(如果sizeof(int)
2013-04-24 18:48:14
那麼,我必須把「tmp-> info = person」放在什麼位置?任何想法如何複製名稱?我在這新...謝謝你。 – 2013-04-24 18:53:01
如果您將'info *'作爲一個參數,則將'info'設爲'char *',然後查看'strlen','malloc'和'strcpy'來複制名稱。 – 2013-04-24 18:55:41