| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 746 人关注过本帖
标题:帮忙找错,拆分单链表。
只看楼主 加入收藏
xp251746222
Rank: 1
等 级:新手上路
帖 子:22
专家分:2
注 册:2010-4-8
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:5 
帮忙找错,拆分单链表。
将单链表拆分为2个,原表保留偶数,奇数结点安原来次序组成新表。
不知道是不是这样做啊,高手帮忙看看。
有错是肯定的,就是不知道错在哪。
#include <stdio.h>
#include <malloc.h>
#define M 5
typedef int datatype;
typedef struct link_node{
    datatype info;
    struct link_node *next;
}node;
// 建立空单链表。
node *init(){
    return NULL;//建立单链表。
}
//输出各结点的值。
void display(node *head){
    node *p;
    p=head;
    if(!p)printf("\n单链表是空的");
    else{
        while(p){
            printf("%5d",p->info);
            p=p->next;}
        printf("\n");
    }
}
//查找第i个结点的地址。
node *find(node *head,int i){
 int j=1;
 node *p=head;
 if(i<1)return NULL;
 while(p&&i!=j){
     p=p->next;j++;
 }
 return p;
}
//建立单链表。
node insert(node *head,datatype x,int i){
    node *p,*q;
    q=find(head,i);
    if(!q&&i!=0)
        printf("\n找不到第%d个结点,不能插入%d!",i,x);
    else{
        p=(node*)malloc(sizeof(node));
        p->info=x;
        if(i==0){
            p->next=head;
            head=p;
        }
        else{
            p->next=NULL;
            q->next=p;

        }
    }
    return *head;
}
//删除结点
node *dele(node *head,datatype x){
    node *pre=NULL,*p;
    if(!head){printf("单链表是空的!");return head;}
    p=head;
    while(p&&p->info!=x)
    {pre=p;p=p->next;}
    if(p)
    {
        if(!pre) head=head->next;
        else pre->next=p->next;
        free(p);
    }
    return head;
}
//拆分链表
void sy3_2(node *head1,node *head2){
    int i,j;
    node *p,*q;
    p=head1;
    for(j=0,i=0;i<M;i++)
    {if(p->info%2)
        {
        *head2=insert(head2,p->info,j);
        printf("%d\n",head2->info);
        p->next=q;
        head1=dele(head1,p->info);      
        j++;
    }
    p=q;}
}
//主函数
void main(){
    node biao1,biao2;
    int i;
    datatype x;
    init();
    printf("输入原单链表。\n");
    for(i=0;i<M;i++)
    {scanf("%d",&x);
     biao1=insert(&biao1,x,i);
    }
    sy3_2(&biao1,&biao2);
        printf("\n单链表1各结点值为:\n");
        display(&biao1);
        printf("\n单链表2各结点值为:\n");
        display(&biao2);
}


搜索更多相关主题的帖子: 单链 拆分 
2010-11-28 21:28
chenhaiquanw
Rank: 2
等 级:论坛游民
帖 子:9
专家分:70
注 册:2010-11-28
收藏
得分:20 
#include <stdlib.h>
#include <stdio.h>
/*将单链表拆分为2个,原表保留偶数,奇数结点安原来次序组成新表。*/

struct list
{
    int num;
    struct list * next;
};
typedef struct list node;
typedef node * llink;

void printllist(llink p)
{
   
    while(p!=NULL)
    {
        printf("%4d",p->num);
        p=p->next;
    }
    printf("\n");

}
llink createllist(int * array,int len)
{
    llink head;
    llink ptr;
    llink ptr1;
   
    int i;

    head=(llink)malloc(sizeof(node));
    if(!head)
    {
        return NULL;
    }
    head->num=array[0];
    head->next=NULL;
    ptr=head;
  
    for(i=1;i<len;i++)
    {
        ptr1=(llink)malloc(sizeof(node));
        if(!ptr1)
        {
            printf("内存分配失败\n");

        }
        else
        {
            ptr1->num=array[i];
            ptr1->next=NULL;
            ptr->next=ptr1;
            ptr=ptr->next;
        }
    }

    return head;

}

llink insertllist(llink ptr,int value)
{
    llink ptr1;  
    llink newnode;
    newnode=(llink) malloc(sizeof(node));
    if(!newnode)
    {
        printf("内存分配失败\n");
        return NULL;
    }
    else
    {
        newnode->next=NULL;
        newnode->num=value;
    }
    ptr1=ptr;

    if(ptr==NULL)
    {
      return newnode;
     
    }
    else
    {
        while(ptr->next!=NULL)
        {
            ptr=ptr->next;
        }
        ptr->next=newnode;

    }
    return ptr1;
}

llink dividllist(llink ptr1)
{
    int i=0;
    llink ptr2=NULL;
    llink ptr;
    llink temp;
   
    temp=ptr1;

   
    if(ptr1==NULL)
    {
        return NULL;

    }
   
    ptr=NULL;
    while(ptr1->next != NULL)
    {
        i++;
   
        if(i%2==0)
        {   
            ptr=ptr1;
            ptr1=ptr1->next;
        }
        else
        {
            
        
            ptr2=insertllist(ptr2,ptr1->num);

            if(ptr==NULL)
            {
              ptr1=ptr1->next;
              temp=ptr1;
               
            }
            else
            {

             ptr->next=ptr1->next;
             free(ptr1);
             ptr1=ptr->next;
            }
        
        }

    }
    printllist(ptr2);
    return temp;
}

void main()
{
    int llist1[6]={1,2,3,4,5,6};
    llink ptr1;
      
   ptr1=createllist(llist1,6);
    if(ptr1==NULL)
    {
        printf("分配内存失败\n");
    }
      
    printllist(ptr1);
    ptr1=dividllist(ptr1);
    printllist(ptr1);

}

/*以上是用数组的方法建立的单链表(无头指针),思想是用一个初值为0的i(int 型)控制分离...虽然不是你想要的答案,不过你可以参考一下....还有就是你的代码格式应注意一下*/

2010-11-29 08:54
xp251746222
Rank: 1
等 级:新手上路
帖 子:22
专家分:2
注 册:2010-4-8
收藏
得分:0 
额,能报我原来的代码指点下吗?
我原来的代码错哪了?
2010-11-29 12:36
xp251746222
Rank: 1
等 级:新手上路
帖 子:22
专家分:2
注 册:2010-4-8
收藏
得分:0 
还有这是什么错误啊
图片附件: 游客没有浏览图片的权限,请 登录注册
2010-11-29 12:56
xp251746222
Rank: 1
等 级:新手上路
帖 子:22
专家分:2
注 册:2010-4-8
收藏
得分:0 
好吧,我的问题解决了,3Q
2010-11-29 13:16
chenhaiquanw
Rank: 2
等 级:论坛游民
帖 子:9
专家分:70
注 册:2010-11-28
收藏
得分:0 
呵呵,以后学学调试吧....我也只是刚开始学,加油...
2010-11-29 21:30
快速回复:帮忙找错,拆分单链表。
数据加载中...
 
   



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

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