gzpk

创建链表

代码
#include <iostream>
using namespace std;
struct student  //定义结构体
{
    
int data;
    student 
*next;
};

//创建链表
student *CreateList()
{
    student 
*head=NULL;
    student 
*p=new student;
    cin
>>p->data;    
    student 
*end=p;
    
while(p->data!=0)
    {
        
if(head==NULL)
            head
=p;
        
else
            end
->next=p;
        end
=p;
        p
=new student;
        cin
>>p->data;
    }
    end
->next=NULL;
    delete p;
    
return(head);
}
int main()
{
    student 
*Head = CreateList();
    student 
*temp=Head;
    
while(temp!=NULL)
    {
        cout
<<temp->data<<" ";
        temp 
= temp->next;
    }
    
return 1;    
}

 

posted on 2010-06-23 21:15  gzpk  阅读(264)  评论(0)    收藏  举报

导航