静候佳音的留言



frydsh发表于:2007-12-04

#include "stdio.h"
#include "stdlib.h"
#define null 0
struct node//数据节点
 {
  int num;
  struct node *next;//指向下一个数据节点
 };
 struct head//管理一个数据链的头节点
{
 struct node *next;//指向数据链的第一个节点
 struct node *last;//指向数据链的最后一个节点
};
 
 void main()
 {
  struct head *copyp(int new_nth,int number,struct node *p);//将数number插入到指针p指向的一列数中,排第new_nth,代码在最后,此函数经检验运行正确.
  struct node *a,*b;
  a=(struct node *)malloc(sizeof(struct node));
  a->num=1;
  a->next=null;
  b=(struct node *)malloc(sizeof(struct node));
  b->num=2;
  b->next=a;//把a,b作为一组连起来,b在前面

  struct head s;
  struct node *p;
  s.next=(copyp(1,7,b))->next;
  s.last=(copyp(1,7,b))->last;
  s.last->next=(copyp(2,7,b))->next;
  s.last=(copyp(2,7,b))->last;
  s.last->next=(copyp(3,7,b))->next;
  s.last=(copyp(3,7,b))->last;
 
  p=s.next;
  int e=0;
  do//3个数一行的输出数据
  {
   printf("%d ",p->num);
   e  ;
   if(e%3==0)
   printf("\n");
   
   p=p->next;
   
  }
  while(p!=null);
}



struct head *copyp(int new_nth,int number,struct node *p)//将数number插入到一列数中,排第new_nth.
{
 struct head *head_a;
 head_a=(struct head *)malloc(sizeof(struct head));
 head_a->last=null;
 head_a->next=null;
 struct node *xp;
 struct node *new_node;
 new_node=(struct node *)malloc(sizeof(struct node));
 new_node->num=number;
 new_node->next=null;
 int k=0;
 while(p!=null)
 {
  if(k==0)
  {
   if(new_nth==1)
   {
    head_a->next=new_node;
head_a->last=new_node;
k  ;
   }
   else
   {
    xp=(struct node *)malloc(sizeof(struct node));
xp->num=p->num;
xp->next=null;
head_a->next=xp;
head_a->last=xp;
    p=p->next;
    k  ;
   }
  }
  else 
  {
   if(k!=new_nth-1)
   {
xp=(struct node *)malloc(sizeof(struct node));
xp->num=p->num;
xp->next=null;
    (head_a->last)->next=xp;
    head_a->last=xp;
    p=p->next;
    k  ;
   }
   else
   { 
    (head_a->last)->next=new_node;
    head_a->last=new_node;
k  ;
   }
  }
 }
  return head_a;
}
/*我想看到的结果是7 2 1
                  2 7 1
  2 1 7
此程序按理应该输出3行数,但是却只有一行7 2 1,不知道是怎么回事 求达人解决*/