#include<iostream>
using namespace std;
struct node{
int data;
node*next;
};
node*create(int n){
node*head,*tail,*p;
head=tail=new node;
head->data=0;
//可用浪费一小段空间的方法节省时间。
while(n--){
p=new node;
cin>>p->data;
p->next=NULL;
tail->next=p;
tail=p;
}
return head;
}
int main() {
int n=3;
node*p;
p=create(3);
p=p->next;//跳过表头指针
while(p){
cout<<p->data<<" ";
p=p->next;
}
return 0;
}
C++尾插入链表_循环实现
最新推荐文章于 2025-01-18 23:13:11 发布
本文详细介绍了一种使用C++实现链表数据结构的方法。通过示例代码,讲解了如何创建链表节点,如何通过输入数据动态生成链表,以及如何遍历并输出链表中的所有元素。该文适合初学者理解链表的基本操作。
1066

被折叠的 条评论
为什么被折叠?



