创建链表

代码
#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;    
}
原文地址:https://www.cnblogs.com/ManMonth/p/1763951.html