回 帖 发 新 帖 刷新版面

主题:[讨论]为什么不能插入 删除数据呢?

这个不能插入和删除数据 哪里错了啊?爱那个高手帮帮忙
#include<stdio.h>
#include <malloc.h>

//建立单链表
  struct student
  {
  int num;                  
  float score;              
  struct student *next;     
  };
//构造单链表如下:
  struct student *creat()     
  { 
    struct student *head;     
    struct student *p,*tail;  
    float temp;              
    head=NULL;
    do
    { 
      p=(struct student *)malloc(sizeof(struct student));
      printf("Number Score:");
      fflush(stdin);         
       scanf("%d  %f",&p->num,&temp);
      if(p->num==0)
      {
        free(p);
        break;
      };
      p->score=temp;          
      p->next=NULL;
      if(head==NULL)
      {
         head=p;
         tail=p;
      }
      else
      {
         tail->next=p;
         tail=p;       
      }
    }while(p->num!=0);
     return(head);
  }

  //遍利链表
  void display(struct student *head)
{
  struct student *p;
  p=head;
  while(p!=NULL)
  {
    printf("%4d  %5.1f\n",p->num,p->score);
    p=p->next;
  }
}
//插入
struct student *insert(struct student *head,struct student *n)
{
  struct student *p,*q;
  if(head==NULL)
    head=n;
  else
  {
  if(head->num>=n->num)
  {
  n->next=head;
  head=n;
}
else
{
  p=head;
  while(p->num<n->num&&p->next!=NULL)
  {
  q=p;
  p=p->next;
}
if(p->num>n->num)
{
q->next=n;
  n->next=p;
}
else
{
  p->next=n;
  n->next=NULL;
}
}
}
return(head);
}


//删除
struct student *deletel(struct student *head,int num)
{
  struct student *p1,*p2;  
  if(head==NULL) printf("\nList null\n");
  else
  {
  p1=head;
  while(num!=p1->num&&p1->next!=NULL)
  {
  p2=p1;
  p1=p1->next;/*后移一个结点*/
}
if(num==p1->num)
{
  if(p1==head) 
head=p1->next;
  else 
p2->next=p1->next;
  printf("delete:%4d\n",num);
  free(p1);
}
else 
printf("%4d not been found!\n",num);
}
return(head);
}


void main()
{
   student *head1; 
 student *a;
  int pos;
   
    //建表实验 
    head1=creat(); 
    printf("\n链表: "); 
    display(head1);
   //插入实验
     
    printf("\n\n对链表进行插入操作!\n");
    printf("\n请输入插入 元素值 (以空格隔开)"); 
     scanf("%d",&a->num);
    insert(head1,a); 
    display(head1); 
       //删除操作
     printf("\n对链表进行删除操作!\n");
    printf("\n请输入删除元素,输入0结束: "); 
    scanf("%d",&pos); 
    deletel(head1,pos);  
    display(head1);
   
       
   


     
       
    
}

回复列表 (共1个回复)

沙发

#include<stdio.h>
#include <malloc.h>

//建立单链表
struct student
{
  int num;                  
  float score;              
  struct student *next;     
};
//构造单链表如下:
struct student *creat()     

  struct student *head;     
  struct student *p,*tail;  
  float temp;              
  head=NULL;
  do
  { 
    p=(struct student *)malloc(sizeof(struct student));
    printf("Number Score:");
    fflush(stdin);         
    scanf("%d  %f",&p->num,&temp);
    if(p->num==0)
    {
      free(p);
      break;
    }            [color=FF0000]//你这多";"了吧……[/color]    
    p->score=temp;          
    p->next=NULL;
    if(head==NULL)
    {
      head=p;
      tail=p;
    }
    else
    {
      tail->next=p;
      tail=p;       
    }
  }while(p->num!=0);
  return(head);
}

//遍利链表
void display(struct student *head)
{
  struct student *p;
  p=head;
  while(p!=NULL)
  {
    printf("%4d  %5.1f\n",p->num,p->score);
    p=p->next;
  }
}
//插入
struct student *insert(struct student *head,struct student *n)
{
  struct student *p,*q;
  if(head==NULL)
    head=n;
  else
  {
    if(head->num>=n->num)
    {
      n->next=head;
      head=n;
    }
    else
    {
      p=head;
      while(p->num<n->num && p->next!=NULL)
      {
        q=p;
        p=p->next;
      }
      if(p->num>n->num)   [color=FF0000]//这一步多余了吧 while之后(p->num) >= (n->num) 可以一步完成的[/color]      
      {
        q->next=n;
        n->next=p;
      }
      else
      {
        p->next=n;
        n->next=NULL;
      }
    }
  }
  return(head);
}


//删除
struct student *deletel(struct student *head,int num)
{
  struct student *p1,*p2;  
  if(head==NULL) printf("\nList null\n");
  else
  {
    p1=head;
    while(num!=p1->num&&p1->next!=NULL)
    {
      p2=p1;
      p1=p1->next;/*后移一个结点*/
    }
    if(num==p1->num)
    {
      if(p1==head) 
        head=p1->next;
      else 
        p2->next=p1->next;
      printf("delete:%4d\n",num);
      free(p1);
    }
    else 
      printf("%4d not been found!\n",num);
  }
  return(head);
}


int main()
{
  struct student *head1; 
  struct student a;      [color=FF0000]//你这地方定义的有问题 主要就是这 导致你的那个错误[/color]  
  int pos;
   
  //建表实验 
  head1=creat(); 
  printf("\n链表: "); 
  display(head1);
  //插入实验
     
  printf("\n\n对链表进行插入操作!\n");
  printf("\n请输入插入 元素值 (以空格隔开)"); 
  scanf("%d %f",&a.num, &a.score);
  insert(head1,&a); 
  display(head1); 
  //删除操作
  printf("\n对链表进行删除操作!\n");
  printf("\n请输入删除元素,输入0结束: "); 
  scanf("%d",&pos); 
  deletel(head1,pos);  
  display(head1);

  return 0; 
}


好了 没问题了 你的格式注意一下 ……

我来回复

您尚未登录,请登录后再回复。点此登录或注册