空等闲

导航

链表的创建

#include<iostream>
using namespace std;
 struct ListNode {
    int val;
     ListNode *next;
 };
void main()
{                            
    ListNode *p1, *p2,*phead1;
    //建立头结点
    int i = 0;
    phead1 = (ListNode*)malloc(sizeof(ListNode));
    phead1->val = 3;
    phead1->next = NULL;
    p1= phead1;
    //创建链表
      for (i = 1; i<4; i++) 
    {
        p2 = (ListNode*)malloc(sizeof(ListNode));
        p2->val = 1;
        p2->next = p1->next;
        p1->next =p2 ;
    }
    //打印链表 
    p1 = phead1;
    while (p1!=NULL)
    {
        cout << p1->val << endl;
        p1 = p1->next;
    }


}

 

posted on 2017-01-08 18:26  空等闲  阅读(97)  评论(0编辑  收藏  举报