为什么这个结果都是输出错误呢

题目求单链表数据的插入
【样例输入】5

                78 9 6 56 56

                3 -3

【样例输出】

                78 9 -3 6 56 56

                6

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
using namespace std;
#define error 0
#define ok 1
#define    overflow    -2
typedef int status;
typedef int elemtype;
typedef struct lnode{
    elemtype data;//数据域 
    struct lnode *next;//指针域 
}lnode, *linklist;
//初始化链表
status initlist(linklist & l) {
    lnode * temp;
    temp = (lnode* )malloc(sizeof(lnode));
    if(!temp) exit(overflow);
    l = temp;
    l->next =NULL;
    return ok;
}
//输入(尾部插入)链表
status inputlist(linklist & l) {
   lnode * curPtr, * rearPtr;
   int n;
   cin>>n;
   rearPtr = l;  //初始时头结点为尾节点,rearPtr指向尾巴节点
    for (int i = 1;i <= n;i ++){  //每次循环都开辟一个新节点,并把新节点拼到尾节点后
        curPtr = (lnode*)malloc(sizeof(lnode));//生成新结点
        if(!curPtr) exit(overflow);
        scanf(" %d",&curPtr->data);//输入元素值
        curPtr->next = NULL;  //最后一个节点的next赋空
        rearPtr->next = curPtr;
        rearPtr = curPtr;
    }
    return ok;
}
//销毁链表
void destroylist(linklist &l) {
    linklist p = l;
    while (p)
    {
        l = l->next;
        delete(p);
        p = l;
    }
}
/*status listinsert(linklist &l,int i,elemtype e){
    lnode * p,*s;p=l;
    int j=0;
    while(p&&j<i-1)
    {
        p=p->next;j++;
    }
    if(!p||j>i+1) return error;
    s=(linklist) malloc(sizeof(lnode));
    s->data=e;
    s->next=p->next;
    p->next=s;
    return ok;
}*/
status listinsert(linklist &l,int i,elemtype e)            //在L中第i个位置插入元素e
{
    linklist p, s;
    int j = 0;
    p=l;                                    //使p指向头节点
    while (p && j < i)
    {
        p = p->next;
        ++j;
    }
    if (!p || j > i)
    {
        printf("插入元素失败!\n");
        return error;
    }
    s = (linklist)malloc(sizeof(lnode));
    s->data = e;
    s->next = p->next;                            //将p的后继节点赋值给s的后继
    p->next = s;                                //将s赋值给p的后继
    return 0;
}
status listtraverse(linklist l)
{
    linklist p=l;
    while(p){
        printf("%d",p);
        p=p->next;
    }
}

int main()
{ 
  linklist l;int i; elemtype e;
  initlist(l);
  inputlist(l);
  scanf("%d%d",&i,&e);
  if(listinsert(l,i,e))  listtraverse(l);
  else   printf("error");
  destroylist(l);
  return 0;
}

img

修改,供参考:

#include<iostream>
//#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
using namespace std;
#define error 0
#define ok 1
#define    overflow    -2
typedef int status;
typedef int elemtype;
typedef struct lnode{
    elemtype data;//数据域
    struct lnode *next;//指针域
}lnode, *linklist;
//初始化链表
status initlist(linklist & l) {
    lnode * temp;
    temp = (lnode* )malloc(sizeof(lnode));
    if(!temp) exit(overflow);
    l = temp;
    l->next =NULL;
    return ok;
}
//输入(尾部插入)链表
status inputlist(linklist & l) {
   lnode * curPtr, * rearPtr;
   int n;
   cout<<"请输入链表的结点数:";
   cin>>n;
   rearPtr = l;  //初始时头结点为尾节点,rearPtr指向尾巴节点
   cout<<"请输入"<<n<<"个结点的值:";
   for (int i = 1;i <= n;i ++){  //每次循环都开辟一个新节点,并把新节点拼到尾节点后
        curPtr = (lnode*)malloc(sizeof(lnode));//生成新结点
        if(!curPtr) exit(overflow);
        cin>>curPtr->data;    //scanf(" %d",&curPtr->data);//输入元素值  修改
        curPtr->next = NULL;  //最后一个节点的next赋空
        rearPtr->next = curPtr;
        rearPtr = curPtr;
   }
   return ok;
}
//销毁链表
void destroylist(linklist &l) {
    linklist p = l;
    while (p)
    {
        l = l->next;
        delete(p);
        p = l;
    }
}
/*status listinsert(linklist &l,int i,elemtype e){
    lnode * p,*s;p=l;
    int j=0;
    while(p&&j<i-1)
    {
        p=p->next;j++;
    }
    if(!p||j>i+1) return error;
    s=(linklist) malloc(sizeof(lnode));
    s->data=e;
    s->next=p->next;
    p->next=s;
    return ok;
}*/
status listinsert(linklist &l,int i,elemtype e)            //在L中第i个位置插入元素e
{
    linklist p, s;
    int j = 0;
    if(i < 1 ){                         //修改
        cout<<"插入位置非法!"<<endl;  //修改
        return error;                  //修改
    }
    p=l;                                    //使p指向头节点
    while (p && j < i-1) //修改 while (p && j < i)
    {
        p = p->next;
        ++j;
    }
    if (!p || j > i)
    {
        printf("插入元素失败!\n");
        return error;
    }
    s = (linklist)malloc(sizeof(lnode));
    s->data = e;
    s->next = p->next;                 //将p的后继节点赋值给s的后继
    p->next = s;                       //将s赋值给p的后继
    return ok;         //return 0;    修改
}
void listtraverse(linklist l)      //status listtraverse(linklist l) 修改
{
    linklist p=l->next;  //修改
    while(p){
        cout<<p->data<<" ";//printf("%d ",p->data);//修改
        p=p->next;
    }
    cout<<endl;      //printf("\n");  //修改
}
int main()
{ 
  linklist l;
  int i;
  elemtype e;
  initlist(l);

  inputlist(l);
  listtraverse(l);

  cout<<"请输入插入位置,及插入的数值:";
  scanf("%d%d",&i,&e);
  if(listinsert(l,i,e))
      listtraverse(l);
  else
      cout<<"error"<<endl; //printf("error");  修改
  listtraverse(l);

  destroylist(l);

  return 0;
}