反转链表与指针传递 c++



#include<iostream>
#include<fstream>
using namespace std;

//链表结构
struct Node{
int data;
Node *next;
};
Node *head;

Node *create(int &n)
{//创建链表
ifstream cin("content.txt");
Node *curr;
head=NULL;
for(int i=0;i<n;i++)
{
curr=new Node;
cin>>curr->data;
curr->next=NULL;
cout<<"  第"<<i+1<<"个数据是"<<curr->data<<endl;
if(head==NULL)
{
head=curr;
}
else{
curr->next=head;
head=curr;
}
cout<<curr<<"  "<<curr->data<<"  "<<curr->next<<endl;
}
cout<<endl;
return head;
}


void show(Node *head)
{//打印链表
cout<<"list:head="<<head<<endl<<endl;
while(head!=NULL){
cout<<head->data<<"  ";
head=head->next;
};
}
//验证head前面的值 发现show只是作用在head的拷贝上,并没有改变head的值


void main()

ifstream cin("in.txt");
int n;
cout<<head<<endl;
cout<<"input n:";
cin>>n;
show(create(n));//show后的head值跟create后head的值相等
cout<<head;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值