链表
2016-07-11 17:00
197 查看
链表的创建
用head,p1,p2三个指针,分别指向头结点,新创建的结点,最后的结点。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cstdlib>
#include<algorithm>
using namespace std;
struct node
{
int a;
struct node *next;
};
int main()
{
int n;
cin>>n;
n--;
node *head,*p1=NULL,*p2=NULL;
p1=(struct node *)malloc(sizeof(struct node));
head=p1;
cin>>p1->a;
p2=p1;
while(n--)
{
p1=(struct node *)malloc(sizeof(struct node));
cin>>p1->a;
p2->next=p1;
p2=p1;
}
p2->next=NULL;
p1=head;
while(p1->next!=NULL)
{
cout<<p1->a<<endl;
p1=p1->next;
}
cout<<p1->a;
free(p1);
p1=NULL;
return 0;
}
链表的比较好的博客文章http://blog.csdn.net/hackbuteer1/article/details/6591486/:
用head,p1,p2三个指针,分别指向头结点,新创建的结点,最后的结点。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cstdlib>
#include<algorithm>
using namespace std;
struct node
{
int a;
struct node *next;
};
int main()
{
int n;
cin>>n;
n--;
node *head,*p1=NULL,*p2=NULL;
p1=(struct node *)malloc(sizeof(struct node));
head=p1;
cin>>p1->a;
p2=p1;
while(n--)
{
p1=(struct node *)malloc(sizeof(struct node));
cin>>p1->a;
p2->next=p1;
p2=p1;
}
p2->next=NULL;
p1=head;
while(p1->next!=NULL)
{
cout<<p1->a<<endl;
p1=p1->next;
}
cout<<p1->a;
free(p1);
p1=NULL;
return 0;
}
链表的比较好的博客文章http://blog.csdn.net/hackbuteer1/article/details/6591486/:
相关文章推荐
- javamail 发送邮件demo
- toggle
- 代码保存好
- WPF自动隐藏的消息框的实例代码
- java的文件操作(1)
- tushare module 1 --获取交易数据
- 【Python学习笔记】IO编程:文件读写
- 记录一下几个Eclipse插件安装地址
- html格式布局
- stack overflow错误分析
- php语言
- Java查看动态代理生成的代码 in Action
- 往ArrayList 传自定义对象并重写Equals()
- JavaScript学习笔记十九:标准对象-RegExp
- http请求加密
- HTML5的IndexedDB增删改查
- 存储过程1-用户登录实现
- 注册表查看串口列表
- Java并发编程:Callable、Future和FutureTask
- 优先队列的使用