命令被信号11终止

时间:2018-10-17 07:46:35

标签: c data-structures queue

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct node{
  char data;
  int p;
  struct node *ptr;
};
struct node *start=NULL;
struct node *insert()
{
  struct node *new_node,*z;
  int num;
  char s;
  printf("\nEnter -1 to stop.");
  printf("\nEnter the characters and their priorities: ");
  scanf("\n%c %d",&s,&num);
  while(num!=-1)
  {
    new_node=(struct node *)malloc(sizeof(struct node));
    new_node->data=s;
    new_node->p=num;
    if(start==NULL)
    {
      start=new_node;
      z=start;
      new_node->ptr=NULL;
    }
    else
    {
      z->ptr=new_node;
      new_node->ptr=NULL;
      z=z->ptr;
    }
    scanf("%c %d",&s,&num);
  }
  return start;
}
struct node *display()
{
  struct node *z;
  z=start;
  printf("\nDisplaying elements:\n");
  while(z!=NULL)
  {
    printf("\t%c [Priority=%d]\n",z->data,z->p);
    z=z->ptr;
  }
  return start;
}
struct node *sortit()
{
  struct node *z,*q;
  int t;
  char x;
  z=start;
  while(z->ptr!=NULL)
  {
    q=z->ptr;
    while(q!=NULL)
    {
      if(z->p>q->p)
      {
        t=z->p;
        x=z->data;
        z->p=q->p;
        z->data=q->data;
        q->p=t;
        q->data=x;
      }
      q=q->ptr;
    }
    z=z->ptr;
  }
  return start;
}
struct node *new_insert()
{
  struct node *y,*z;
  int n;
  char s;
  y=(struct node *)malloc(sizeof(struct node));
  printf("\nEnter character and priority for new node:");
  scanf("%c %d",&s,&n);
  printf("%c",s);
  y->data=s;
  y->p=n;
  printf("%c",y->data);
  printf("%d",y->p);
  if(n<start->p)
  {
    y->ptr=start;
    start=y;
    printf("%d",y->p);
  }
  else
  {printf("\nff");
    z=start;
  while(z->ptr->p<=n&&z->ptr!=NULL)
    z=z->ptr;
  y->ptr=z->ptr;
  z->ptr=y;
}
  return start;
}
int main()
{
  insert();
  display();
  sortit();
  display();
  new_insert();
  display();
  return 0;
}

在此C代码中,我尝试实现优先级队列。

new_insert()功能外,其他所有功能都可以正常工作。 y->p=n;函数中new_insert()之后的打印语句打印0

因此,该功能无法正常运行。另外,在display()函数中,print语句将[Priority]打印两次。

因此,我无法在优先级队列中添加外部节点。

1 个答案:

答案 0 :(得分:0)

好吧,我认为您的代码尚不可行。

有些问题可以纠正:

  • scanf的使用有问题

    • %c选择器必须谨慎使用,使用" %c %d"以防止行尾问题
    • 必须检查返回值
  • 在函数插入中,z可能未初始化(如果在起始不是insert()时调用NULL

  • 您不需要cast malloc返回值。

我用此输入测试了以下代码:

a 2
b 3
c 1
d -1
e 0 

我得到了这个结果:

Enter -1 to stop.
Enter the characters and their priorities: 
Displaying elements:
    a [Priority=2]
    b [Priority=3]
    c [Priority=1]

Displaying elements:
    c [Priority=1]
    a [Priority=2]
    b [Priority=3]

Enter character and priority for new node:
s: 'e' n: '0'
e00
Displaying elements:
    e [Priority=0]
    c [Priority=1]
    a [Priority=2]
    b [Priority=3]    

似乎没有工作吗?您可以online

对其进行测试

这是更正的代码:

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct node{
    char data;
    int p;
    struct node *ptr;
};
struct node *start=NULL;

struct node *insert()
{
    /* initialize z in case of start is not null */
    struct node *new_node,*z = start;
    int num, ok;
    char s;
    printf("\nEnter -1 to stop.");
    printf("\nEnter the characters and their priorities: ");

    /* read the scanf return value */
    ok = scanf(" %c %d",&s,&num);
    /* and assert two elements have been read*/
    while(ok == 2 && num!=-1)
    {
        new_node=malloc(sizeof(struct node));
        new_node->data=s;
        new_node->p=num;
        if(start==NULL)
        {
            start=new_node;
            z=start;
            new_node->ptr=NULL;
        }
        else
        {
            z->ptr=new_node;
            new_node->ptr=NULL;
            z=z->ptr;
        }
        ok = scanf(" %c %d",&s,&num);
    }
    return start;
}
struct node *display()
{
    struct node *z;
    z=start;
    printf("\nDisplaying elements:\n");
    while(z!=NULL)
    {
        printf("\t%c [Priority=%d]\n",z->data,z->p);
        z=z->ptr;
    }
    return start;
}
struct node *sortit()
{
    struct node *z,*q;
    int t;
    char x;
    z=start;
    while(z->ptr!=NULL)
    {
        q=z->ptr;
        while(q!=NULL)
        {
            if(z->p>q->p)
            {
                t=z->p;
                x=z->data;
                z->p=q->p;
                z->data=q->data;
                q->p=t;
                q->data=x;
            }
            q=q->ptr;
        }
        z=z->ptr;
    }
    return start;
}
struct node *new_insert()
{
    struct node *y,*z;
    int n, ok;
    char s;

    printf("\nEnter character and priority for new node:");
    ok = scanf(" %c %d",&s,&n);
    if (2 == ok)
    {
        /* alloc y after having check that user gave usage stuff */
        y = malloc(sizeof(struct node));
        y->data=s;
        y->p=n;
        printf("%c",y->data);
        printf("%d",y->p);
        if(n<start->p)
        {
            y->ptr=start;
            start=y;
            printf("%d",y->p);
        }
        else
        {
            printf("\nff");
            z=start;
            while(z->ptr->p<=n&&z->ptr!=NULL)
                z=z->ptr;

            y->ptr=z->ptr;
            z->ptr=y;
        }
    }
    return start;
}
int main()
{
    insert();
    display();
    sortit();
    display();
    new_insert();
    display();
    return 0;
}