| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 677 人关注过本帖
标题:一个关于链表的程序,我将链表的建立、插入,删除等源程序合在一起却无法运 ...
只看楼主 加入收藏
一个人就好
Rank: 1
等 级:新手上路
帖 子:18
专家分:0
注 册:2011-4-11
结帖率:50%
收藏
已结贴  问题点数:20 回复次数:6 
一个关于链表的程序,我将链表的建立、插入,删除等源程序合在一起却无法运行,求解!
#include <stdio.h>
#include <stdio.h>
#include <malloc.h>
struct node *creat()
struct node
{int num;
 struct node *next;};
{int n,i;
struct node *head, *new;
struct node *p;
print("input the node num\n");
if (n>0)
    {printf(input %d date:\n",n):
    head=(struct node*)malloc(sizeof(struct node));
    p=head;
    if(head!=NULL)
      { scanf("%d",&head->num);           head->next=NULL;      }
    else
     { printf("out of momery\n");       exit(0);      }
    for(i=1;i<n;i++)
    { new=(struct node*)malloc(sizeof(struct node));
      if(new!=NULL)
            { scanf("%d",&new->num);
               p->next=new;                 /*  新结点连在表尾   */
               p=new;                           /*  新结点为表尾结点*/
             }
      else
             { printf("out of momery\n");       exit(0);       }
       p->next=NULL;                   /*   表尾置 NULL     */
     } }
 else
     { printf("n<=0\n");       head=NULL;      }
return head;
}
struct node *insert(struct node *head, int num)
{ struct node *p, *new, *q;
    new=(struct node*)malloc(sizeof(struct node));
      if(new!=NULL)
    { new->num=num;
    if (num<head->num || head= =NULL)
       {  new->next=head;     head=new;           /* 插入表头   */       }
      else
      { p=head;
        while (p!=NULL&&p->num<num)       /*查找插入位置**/
           { q=p;  p=p->next;   }
        new->next=p;        q->next=new;     /*插入表中或表尾*/
        }
       return(head);
     }
      else
    { printf("out of momery\n");              exit(0);                }
   }
struct node *delete(struct node *head,int n)
{ struct node *p, *q;
  if (head!=NULL)
    {q=head;
     if(head->num= =n)
       { head=head->next; free(q);        }   /*  删除头结点  */     else
      { while (q!=NULL&&q->num!=n)
    { p=q;  q=q->next;  }             /*  查找待删结点 q  */
          if (q= =NULL)
             printf("%d not been found\n",n);     /*  没找到待删结点   */
         else
        { p->next=q->next; free(q); }                    /*  删除结点q   */
     }}
   else
   printf("the list empty\n");              /*  表空  */
   return head;
}


搜索更多相关主题的帖子: 源程序 
2011-06-08 16:18
laigaoat2005
Rank: 4
等 级:业余侠客
帖 子:388
专家分:226
注 册:2007-4-5
收藏
得分:10 
程序代码:
#include <stdio.h>
#include <stdio.h>
#include <malloc.h>
struct node
{
    int num;
    struct node *next;
};

struct node *creat()
{
    int n,i;
    struct node *head, *new;
    struct node *p;
    printf("input the node num\n"); //这里应该是printf
    if (n>0)
        {
        printf("input %d date:\n",n);
        head=(struct node*)malloc(sizeof(struct node));
        p=head;
        if(head!=NULL)
          { scanf("%d",&head->num);           head->next=NULL;      }
        else
         { printf("out of momery\n");       exit(0);      }
        for(i=1;i<n;i++)
        { new=(struct node*)malloc(sizeof(struct node));
          if(new!=NULL)
                { scanf("%d",&new->num);
                   p->next=new;                 /*  新结点连在表尾   */
                   p=new;                           /*  新结点为表尾结点*/
                 }
          else
                 { printf("out of momery\n");       exit(0);       }
           p->next=NULL;                   /*   表尾置 NULL     */
         } }
    else
         { printf("n<=0\n");       head=NULL;      }
    return head;
} 

struct node *insert(struct node *head, int num)
{ struct node *p, *new, *q;
    new=(struct node*)malloc(sizeof(struct node));
      if(new!=NULL)
    { new->num=num;
    if (num<head->num || head==NULL)
       {  new->next=head;     head=new;           /* 插入表头   */       }
      else
      { p=head;
        while (p!=NULL&&p->num<num)       /*查找插入位置**/
           { q=p;  p=p->next;   }
        new->next=p;        q->next=new;     /*插入表中或表尾*/
        }
       return(head);
     }
      else
    { printf("out of momery\n");              exit(0);                }
   }
struct node *delete(struct node *head,int n)
{ struct node *p, *q;
  if (head!=NULL)
    {q=head;
     if(head->num==n)
       { head=head->next; free(q);        }   /*  删除头结点  */     else
      { while (q!=NULL&&q->num!=n)
    { p=q;  q=q->next;  }             /*  查找待删结点 q  */
          if (q==NULL)
             printf("%d not been found\n",n);     /*  没找到待删结点   */
         else
        { p->next=q->next; free(q); }                    /*  删除结点q   */
     }}
   else
   printf("the list empty\n");              /*  表空  */
   return head;
}

int main()
{
   
    //这里加上你要调用的函数
   
    return 0;
   
}


楼主几乎都是笔误。写代码要小心。学会看编译器给你的提示吧。

[ 本帖最后由 laigaoat2005 于 2011-6-8 16:43 编辑 ]
2011-06-08 16:37
一个人就好
Rank: 1
等 级:新手上路
帖 子:18
专家分:0
注 册:2011-4-11
收藏
得分:0 
谢谢你呀!确实有笔误,但还有不理解的地方
2011-06-08 17:22
一个人就好
Rank: 1
等 级:新手上路
帖 子:18
专家分:0
注 册:2011-4-11
收藏
得分:0 
上面回复的写改好的代码没有错误,可以运行,但是没有结果,是空的,为什么?
2011-06-09 19:28
laigaoat2005
Rank: 4
等 级:业余侠客
帖 子:388
专家分:226
注 册:2007-4-5
收藏
得分:0 
主函数里根本没有调用函数,当然不会有结果。main函数里有注解,你看看
2011-06-09 20:09
一个人就好
Rank: 1
等 级:新手上路
帖 子:18
专家分:0
注 册:2011-4-11
收藏
得分:0 
哦,我来看一下
2011-06-10 17:57
小菜小C
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:70
专家分:111
注 册:2011-3-18
收藏
得分:10 
回复 3楼 一个人就好
#include <stdio.h>
#include <stdio.h>
#include <malloc.h>
struct node *creat()
{
    struct node
    {
        int num;
        struct node *next;
    };
    int n,i;
    struct node *head, *ne;         //我这个是VC++编译器,不知道你的是那个,我的这个有意义了,本身,百度知道,重新定一个给你
    struct node *p;
    print("input the node num\n");
    if (n>0)
    {
        printf(input %d date:\n",n):
        head=(struct node*)malloc(sizeof(struct node));
        p=head;
        if(head!=NULL)                                      //你这个明显把头结点给赋值了
          {
            scanf("%d",&head->num);      
            head->next=NULL;
        }
        else                                    //你这个是不可能的,因为head明显永远都不可能为空,除非你给它赋值
        {
            printf("out of momery\n");
            exit(0);
        }
    for(i=1;i<n;i++)                                                    //这点也错了,下面就不看了,先把上面的改下再看上面的吧
    {
        ne=(struct node*)malloc(sizeof(struct node));
      if(ne!=NULL)                                 //这个不可能会出现
      {
                scanf("%d",&ne->num);
               p->next=ne;                 /*  新结点连在表尾   */
               p=ne;                           /*  新结点为表尾结点*/
      }
      else
      {
            printf("out of momery\n");   
            exit(0);  
      }
               p->next=NULL;                   /*   表尾置 NULL     */
     }
    }
        else
    {
        printf("n<=0\n");
        head=NULL;  
    }
        return head;
}
struct node *insert(struct node *head, int num)
{
    struct node *p, *new, *q;
    new=(struct node*)malloc(sizeof(struct node));
      if(new!=NULL)
    {
          new->num=num;
        if (num<head->num || head= =NULL)
       {  
            new->next=head;   
            head=new;           /* 插入表头   */       }
      else
      {
          p=head;
        while (p!=NULL&&p->num<num)       /*查找插入位置**/
           {
                q=p;  
                p=p->next;
            }
        new->next=p;
        q->next=new;     /*插入表中或表尾*/
        }
       return(head);
     }
      else
    {
          printf("out of momery\n");
          exit(0);
     }
   }
struct node *delete(struct node *head,int n)
{
    struct node *p, *q;
  if (head!=NULL)
    {
      q=head;
     if(head->num= =n)
       {
         head=head->next;
         free(q);
        }   /*  删除头结点  */     else
      {
            while (q!=NULL&&q->num!=n)
            {
                p=q;
                q=q->next;
            }             /*  查找待删结点 q  */
          if (q= =NULL)
             printf("%d not been found\n",n);     /*  没找到待删结点   */
         else
        {
             p->next=q->next;
             free(q);
         }                    /*  删除结点q   */
     }
  }
   else
   printf("the list empty\n");              /*  表空  */
   return head;
}


菜鸟一名,准备起飞
2011-06-10 18:22
快速回复:一个关于链表的程序,我将链表的建立、插入,删除等源程序合在一起却无 ...
数据加载中...
 
   



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

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