单向链表
来源:互联网 发布:站内搜索数据库 编辑:程序博客网 时间:2024/06/05 18:38
#include<iostream>
#include<stdlib.h>
using namespace std;
typedef struct Node
{
int score;
char name[10];
struct Node *next;
}node,*link;//node是Node,link是node*
int main()
{
link pnew,pold,head,temp;
head=new node;//111 头结点初始化及赋值
if(!head)
{
cout<<"申请内存失败!"<<endl;
return 0;
}
cout<<"请输入名字";
cin>>head->name ;
cout<<"请输入成绩";
cin>>head->score ;
pold=head;//222 将头结点赋为pold
for(int i=1;i<4;i++)
{
pnew=new node;// 333 为pold分配内存及赋值
if(!pnew)
{
cout<<"申请内存失败!"<<endl;
return 0;
}
cout<<"请输入名字";
cin>>pnew->name ;
cout<<"请输入成绩";
cin>>pnew->score ;
pnew->next =NULL;//444 每一个pnew的next都首先赋值为NULL,以防该节点为最后一个节点
pold->next=pnew ;//555 pnew与pold的相互转化
pold=pnew;
}
pold=head;//666 以头节点为其实节点输出,将head转化为pold
while(pold!=NULL)
{
cout<<pold->name <<" "<<pold->score <<endl;
temp=pold;
pold=pold->next ;
delete temp;
}
system("pause");
return 0;
}
#include<stdlib.h>
using namespace std;
typedef struct Node
{
int score;
char name[10];
struct Node *next;
}node,*link;//node是Node,link是node*
int main()
{
link pnew,pold,head,temp;
head=new node;//111 头结点初始化及赋值
if(!head)
{
cout<<"申请内存失败!"<<endl;
return 0;
}
cout<<"请输入名字";
cin>>head->name ;
cout<<"请输入成绩";
cin>>head->score ;
pold=head;//222 将头结点赋为pold
for(int i=1;i<4;i++)
{
pnew=new node;// 333 为pold分配内存及赋值
if(!pnew)
{
cout<<"申请内存失败!"<<endl;
return 0;
}
cout<<"请输入名字";
cin>>pnew->name ;
cout<<"请输入成绩";
cin>>pnew->score ;
pnew->next =NULL;//444 每一个pnew的next都首先赋值为NULL,以防该节点为最后一个节点
pold->next=pnew ;//555 pnew与pold的相互转化
pold=pnew;
}
pold=head;//666 以头节点为其实节点输出,将head转化为pold
while(pold!=NULL)
{
cout<<pold->name <<" "<<pold->score <<endl;
temp=pold;
pold=pold->next ;
delete temp;
}
system("pause");
return 0;
}
0 0
- 单向链表
- 单向链表倒序
- 单向链表
- 单向链表逆转
- 逆转单向链表
- 单向链表反转
- 单向链表
- 单向链表(ZT)
- 反转单向链表
- 单向链表反转
- 单向链表
- 创建单向链表
- 单向链表反转
- 单向链表
- 单向链表
- 单向链表
- 单向链表
- C++ 单向链表
- 2016年1月24日的比赛总结
- 转载_推荐!国外程序员整理的 C++ 资源大全
- Android-onInterceptTouchEvent()和onTouchEvent()总结
- Android manifest文件中的标签详细介绍
- 数据通信系统的模型
- 单向链表
- nodejs-CentOS64下载安装配置
- 【Java】下载网络上的图片并保存到本地目录
- Android studio 创建签名文件
- 响应式和自适应区别
- Android中弱引用与软引用的应用场
- 机房重构——继承窗体之谜
- 4.0第二十章 线程,任务和同步
- Jquery获取父级元素的三种方法