| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2062 人关注过本帖
标题:在单链表中插入新节点,运行结果不是预期的怎么改呀?
取消只看楼主 加入收藏
风铃之美
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2008-4-2
收藏
 问题点数:0 回复次数:0 
在单链表中插入新节点,运行结果不是预期的怎么改呀?
#include "stdio.h"
#include "malloc.h"
typedef struct node
{
  int data;
  int *next;
  };
  int sum;
  struct node * creat(void)
{
  struct node *p1,*p2,*head;
  p2=p1=head=(struct node *)malloc(sizeof(struct node));
  scanf("%d",&p1->data);
  head=p1;
  while(p1->data!=NULL)
  {
    p1=(struct node *)malloc(sizeof(struct node));
    scanf("%d",&p1->data);
    p2->next=p1;
    p2=p1;
   }
    p2->next=NULL;
   return(head);
  }
struct node * print(struct node *head)
{
  struct node  *p;
  sum=0;
  p=head;
  while(p->next!=NULL)
  {
    printf("%d ",p->data);
    p=p->next;sum++;
  }
}
struct node * insert(struct node *head,struct node * p)
{
  struct node *p0,*p1,*p2;
  p1=head;
  p0=p;
  if(head==NULL)
  {head=p0;p0->next=NULL;}
  else
  {
    while((p0->data>p1->data)&&(p1->next!=NULL))
    {p2=p1;p1=p1->next;}
    if(p0->data<=p1->data)
    {
     if(head==p1)head=p0;
     else p2->next=p0;
     p0->next=p1;
      }
  if(p1->next=NULL)
  {
    p1->next=p0;p0->next=NULL;
  }
  ++sum;
    return(head);
  }
  }
  main()
{
  struct node *head,*p;
  printf("please enter the data of list:\n");
  head=creat();
  printf("the data of the list are:\n");
  print(head);
  printf("please enter the data you want to insert:\n");
  p=(struct node *)malloc(sizeof(struct node));
  scanf("%d",&p->data);
  head=insert(head,p);
  printf("the new list is:\n");
  print(head);
}
搜索更多相关主题的帖子: node struct 单链 节点 head 
2008-04-05 15:09
快速回复:在单链表中插入新节点,运行结果不是预期的怎么改呀?
数据加载中...
 
   



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

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