我写了一个程序用作线路组织者,但是当显示一个人的名字时,它会将最后一个人添加到所有其他人中。我该如何解决?如果我更改 中的信息struct
,它会出错;但是,如果有人可以帮助我,我会很高兴。
#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");
}
}