只用一个头结点一个头指针的链表写法
#include<iostream> #include<stdlib.h> using namespace std; struct Node { int data; struct Node *next; }; int main() { int num,n; cin>>num; Node *head,*p; head=NULL; head = p = (Node*)malloc(sizeof(struct Node)); for(int i=0;i<num;i++) { p->next=(Node*)malloc(sizeof(struct Node)); cin>>n; p->data=n; p=p->next; } p->next=NULL; for(p=head;p->next!=NULL;p=p->next) { cout<<p->data; } }