0

ラインオーガナイザーとして使用するプログラムを作成しましたが、人の名前を表示するときが来ると、最後に追加された人を他のすべての人に設定します。どうすれば修正できますか?の情報を変更すると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");
  }
}    
4

1 に答える 1

0

Your struct node has an element named "info" which is of type int. Every time you add a person, you use scanf() to read the person's name into a character array, and you then pass the address of that character array to insert() which stores the address in the integer "info". Every struct node that you allocate, you're storing the same address of the same variable in it. Every time you scanf(), you're overwriting the same memory.

Your struct node should have an element char info[20] and when you create a new node, you should strcpy() the person's name into tmp->info.

Also note that the way you're treating a variable alternately as type char* and as type int leads to undefined behaviour.

于 2013-04-24T18:56:54.203 に答える