尾插法简历单链表

void create(ListNode *l,int a[]) {
    l->val = a[0];
    ListNode  *pnew,*ptemp = l;//指针temp 用于指向当前结点
    for (int i =1; i <s+1; i++) {        //将值一个一个插入单链表中
        cout<<"l->val:"<<ptemp->val<<endl;
        pnew = new ListNode(a[i], NULL);//新节点的下一个地址为NULL//将new结点插入到tmp指针当前指向的结点的后面
        ptemp->next = pnew;
        ptemp = ptemp->next  ;
    }
    ptemp = l;
    for(int i =0;i<3;i++){
        cout<<ptemp->val<<" ";
        ptemp= ptemp->next;
    }
    cout<<"执行借宿"<<endl;
}

 

posted @ 2022-04-20 17:41  Dsad123FFFG6645  阅读(31)  评论(0)    收藏  举报