c/c++:单向链表顺序输出
coding
#include <iostream>
#include <cstring>
using namespace std;
struct node
{
int data;
struct node *next;
};
int main ()
{
std::ios::sync_with_stdio(false);
struct node *head, *tail, *p;
head = new node;
head->next = NULL;
tail = head;
int N ;
int tmpN;
cin>>N;
{
tmpN = N;
while(tmpN--)
{
p = new node;
p->next = NULL;
cin >> p->data ;
tail->next = p;
tail = p;
}
p = head->next;
while(p->next!=NULL)
{
cout<<p->data<<' ';
p=p->next;
}
cout<<p->data<<' ';
cout<<endl;
}
return 0;
}
评论列表