首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

c语言处理链表出现有关问题了,求帮助

2013-08-04 
c语言处理链表出现问题了,求帮助要求是这样的:假设有两个按元素值递增有序的线性表A和B,均以单链表作存储

c语言处理链表出现问题了,求帮助
要求是这样的:假设有两个按元素值递增有序的线性表A和B,均以单链表作存储结构,试编写算法将A表和B表归并成一个按元素值递增有序的线性表C,并要求利用原表的空间存放C。

下面是我的代码,但是为什么第二个链表的节点超过1个,程序就会出错呢?求大神们帮助

#include<stdio.h>
#include<stdlib.h>
#define len sizeof(node)
struct node
{
    int num;
    node *next;
};
struct node *createlink (node *head)
{
struct node *p1 = NULL; /*p1保存创建的新结点的地址 */
struct node *p2 = NULL; /*p2保存原链表最后一个结点的地址 */
p1 = (struct node *) malloc (len); /*开辟一个新结点 */
    if(p1==NULL)
        printf("节点空间申请失败\n");
    else
    {
        scanf("%d",&(p1->num));
        //p1->num=i;                  //如果想要手动输入就将这行注释,并取消上2行的注释
        if(head==NULL)
        {
            head=p1;
            p1->next=NULL;
        }
        else
        {
            p2=head;
            while( p2->next != NULL)
                p2=p2->next;
            p2->next=p1;
            p1->next=NULL;
        }
    }
    return head;
}
void print (node *head)
{
node *p;
p = head;
if (head != NULL) /*只要不是空链表,就输出链表中所有结点 */
{
do
{
printf ("%d  ",p->num);
p = p->next; /*移到下一个结点 */
}
while (p != NULL);
}
}
node *charu(node *head,node *i)
{
    node *p1=NULL;
    node *p2=NULL;
    //node *i=p;
    int temp,k;
    if(head== NULL)
        printf("链表为空无法操作\n");


    else
    {
        p1=head;
        if(p1->num > i->num)        //如果节点需要插入链表的第一个位置
        {
            head=i;
            i->next=p1;
        }
        else
        {
            while (p1->num < i->num && p1->next != NULL) /*p1指向的结点不是所需要的,并且它不是最后一个结点,继续往下找 */
            {
                p2=p1;          //保存上一个节点
                p1 = p1->next; /*后移一个结点 */
            }
            if (p1->next==NULL && p1->num <= i->num)     //如果i比链表最后一个节点大,直接将i作为链表的最后一个节点
            {
                i->next=NULL ;
                p1->next=i;
            }
            else //if(p1->num >= i->num)  //如果i处于链表的中间位置
            {
                i->next = p2->next;
                p2->next = i;
            }
        }
    }
    return head;
}

node *hebing(node *head1,node *head2)
{
    int n=1;
    node *p2=head2;
    node p;
do
{
    p=*p2;
head1=charu(head1,&p);


p2 = p2->next; /*移到下一个结点 */
}
while( p2 != NULL);
    return head1;
}

int main()
{
    int n=1;
    node *head1=NULL,*head2=NULL;
    printf("请输入第%d个链表(数字递增):",n++);
    for(int i=0;i<2;i++)
        head1=createlink(head1);
    printf("请输入第%d个链表(数字递增):",n++);
    for(int i=0;i<3;i++)
        head2=createlink(head2);
    head1=hebing(head1,head2);
    print(head1);
}

C 链表 struct
[解决办法]
lz 谁教你结构体只写node而不写struct的?gcc编译器通不过你这种写法

没有审核你的charu错误,而是重新实现了hebing!

#include<stdio.h>
#include<stdlib.h>
#define len sizeof(struct node)
struct node
{
    int num;
    struct node *next;
};
struct node *createlink (struct node *head)
{
    struct node *p1 = NULL; /*p1保存创建的新结点的地址 */
    struct node *p2 = NULL; /*p2保存原链表最后一个结点的地址 */
    p1 = (struct node *) malloc (len); /*开辟一个新结点 */
    if(p1==NULL)
        printf("节点空间申请失败\n");
    else
    {
        scanf("%d",&(p1->num));
        //p1->num=i;                  //如果想要手动输入就将这行注释,并取消上2行的注释
        if(head==NULL)
        {
            head=p1;
            p1->next=NULL;
        }


        else
        {
            p2=head;
            while( p2->next != NULL)
                p2=p2->next;
            p2->next=p1;
            p1->next=NULL;
        }
    }
    return head;
}
void print (struct node *head)
{
   struct  node *p;
    p = head;
    if (head != NULL) /*只要不是空链表,就输出链表中所有结点 */
    {
        do
        {
            printf ("%d  ",p->num);
            p = p->next; /*移到下一个结点 */
        }
        while (p != NULL);
    }
}
struct node *charu(struct node *head,struct node *i)
{
}
 
struct node *hebing(struct node *head1,struct node *head2)
{
    struct node *p=head1, *q = head2, *s;
if(p->num > q->num)
{
head1 = q;
q = q->next;
}
else
{
p = p->next;
}

head1->next = NULL;
s = head1;
 while(p && q)
{
if(p->num < q->num)
{
s->next = p;
s = p;
p = p->next;
}
else
{
s->next = q;
s = q;
q = q->next;
}
}
//下面两个if一般只执行一个
if(p)
{
s->next = p;
}
if(q)
{
s->next = q;
}
    return head1;
}
 
int main()
{
    int i, n=1;
    struct node *head1=NULL,*head2=NULL;
    printf("请输入第%d个链表(数字递增):",n++);
    for(i=0;i<2;i++)
        head1=createlink(head1);


    printf("请输入第%d个链表(数字递增):",n++);
    for(i=0;i<3;i++)
        head2=createlink(head2);
    head1=hebing(head1,head2);
    print(head1);
struct node *p;
while(head1)
{
p = head1;
head1 = head1->next;
if(p)
free(p);
}
}



热点排行