| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 677 人关注过本帖
标题:一个关于链表的程序,我将链表的建立、插入,删除等源程序合在一起却无法运 ...
取消只看楼主 加入收藏
一个人就好
Rank: 1
等 级:新手上路
帖 子:18
专家分:0
注 册:2011-4-11
结帖率:50%
收藏
已结贴  问题点数:20 回复次数: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, *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
一个人就好
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
一个人就好
Rank: 1
等 级:新手上路
帖 子:18
专家分:0
注 册:2011-4-11
收藏
得分:0 
哦,我来看一下
2011-06-10 17:57
快速回复:一个关于链表的程序,我将链表的建立、插入,删除等源程序合在一起却无 ...
数据加载中...
 
   



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

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