合并单链表出现的问题

来源:互联网 发布:数据库开发培训机构 编辑:程序博客网 时间:2024/06/05 18:21

今天做了一下书上的作业,但是函数运行完之后,总是提示:访问的 内存出错,源代码如下,各位大虾看下,帮我解释下吧

#include<stdio.h>
#include<stdlib.h>
struct node{
       int data;
       struct node*next;
       };

struct node*creat();/*创建单链表*/
struct node*concentrate(struct node*i,struct node*j);/*合并单链表*/
void print(struct node*head);/*偏历单链表*/
int main()
{
    struct node*first,*second,*third;
    first=creat();
    second=creat();
    third=concentrate(first,second);
    print(third);
    system("pause");
    return 0;
}

struct node*creat()
{
       struct node*head,*ne,*tail;
       int n;
      
       scanf("%d",&n);
       head=NULL;
       while(n!=-1){
                    ne=(struct node*)malloc(sizeof(struct node));
                    ne->data=n;
                    ne->next=NULL;
                    if(head==NULL)
                     head=ne;
                    else
                      tail->next=ne;
                    tail=ne;
                     scanf("%d",&n);                    
                    }
           return(head);
}

struct node*concentrate(struct node*i,struct node*j)
{
       struct node*p;
       p=i;
       while(i!=NULL){
                   i=i->next;
                   }      
       if(i->next==NULL) i->next=j;
       return(p);
}

void print(struct node*head)
{
     struct node*p;
     p=head;
     while(p!=NULL)
     {
        printf("%d  ",p->data);
        p=p->next;
     }
}         

原创粉丝点击