标题:为什么不能插入数字呢?
取消只看楼主
zyx1989
Rank: 1
等 级:新手上路
帖 子:86
专家分:2
注 册:2011-9-17
结帖率:83.33%
已结贴  问题点数:5 回复次数:1 
为什么不能插入数字呢?
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#include<string.h>
typedef struct student
{
    int num;
   
    struct student *link;
}stud;

stud* creat(int n)
{
    stud *h,*p;
    int i;
    h=(stud *)malloc(sizeof(stud));
    if(h==NULL)
    {
        printf("not enough memory!");
        return(0);
    }
    h=NULL;
    for(i=0;i<=n;i++)
    {
        
        
        if((p=(struct student *)malloc(sizeof(stud)))==NULL)
            
            
            
        {
            printf("内存不足!");
            return(0);
        }
        scanf("%d",&p->num);
        
        p->link=h;
        h=p;
        
    }
    return(h);
}
  void insert(stud *h)
  {
      stud *p,*q,*d;
      p=h;
      int n,k;
      q=h->link;
      
      printf("请输入插入位置的前一个数!");
      scanf("%d",&n);
      

      if(n==p->num)
      {
      printf("请输入要插入的数 !");
      scanf("%d",&d->num);
      d=h;
      p=d->link;
      }
      else if
      
          (n==q->num)
      {
       printf("请输入要插入的数 !");
      scanf("%d",&d->num);
      d->link=p->link;
      p->link=d;
      }
      else if(n!=p->num&&n!=q->num)
      {
         while(q->num!=NULL&&p->num!=NULL)
         {
              
              
              p=p->link;
              q=q->link;
              if(n==q->num)
              printf("请输入要插入的数字!");
              scanf("%d",&d->num);
              p->link=d->link;
              p->link=d;
         }
      }
      
  }  
  void search(stud *h)
  {
    stud *p;
    int m;
    printf("请输入需查找数据!");
    scanf("%d",&m);
    p=h;
    while(p!=NULL)
    {
   
        if(m==p->num)
        {printf("%d\n",m);
        
         break;
        }
        else
         p=p->link;
        
    }
         if(p==NULL)
            printf("无此数!");
   
   
   
  }
  stud *delet(stud *h)
  {
      stud *q,*p;
      p=h;
      int n;
      q=h->link;
      printf("请输入要删除的数字!");
      scanf("%d",&n);
      if(n==p->num)
      h=p->link;
      else if(n==q->num)
          p->link=q->link;
      else if(n!=p->num&&n!=q->num)
         while(q->link!=NULL&&p->link!=NULL)
         {
              
              
              p=p->link;
              q=q->link;        
             if(n==q->num)
              p->link=q->link;
         
         }
         
         
      return(h);
  }
void print(stud *h)
{
     stud *p;
     p=h;
     while(p!=NULL)
     {
         printf("%3d",p->num);
         p=p->link;
     }
}
void main()
{
    struct student *h;
   
    h=(struct student *)creat(3);
    insert(h);
    search(h);
    h=delet(h);
    print(h);
}

搜索更多相关主题的帖子: include memory return 
2011-09-27 15:12
zyx1989
Rank: 1
等 级:新手上路
帖 子:86
专家分:2
注 册:2011-9-17
得分:0 
void insert(stud *h)
  {
      stud *p,*q,*d;
      h=(stud *)malloc(sizeof(stud));
      if(h==NULL)
      {
          printf("内存不足!");
      }
      p=h;
      int n,k;
      q=h->link;
      
      printf("请输入插入位置的前一个数!");
      scanf("%d",&n);
      

      if(n==p->num)
      {
      printf("请输入要插入的数 !");
      scanf("%d",&d->num);
      d=h;
      p=d->link;
      }
      else if
      
          (n==q->num)
      {
       printf("请输入要插入的数 !");
      scanf("%d",&d->num);
      d->link=p->link;
      p->link=d;
      }
      else if(n!=p->num&&n!=q->num)
      {
         while(q->num!=NULL&&p->num!=NULL)
         {
              
              
              p=p->link;
              q=q->link;
              if(n==q->num)
              printf("请输入要插入的数字!");
              scanf("%d",&d->num);
              p->link=d->link;
              p->link=d;
         }
      }
      
  }  改成这样了也不行
2011-09-27 19:01



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-351014-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.146277 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved