您的位置:首页 > 编程语言 > C语言/C++

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

2013-05-14 20:13 302 查看
#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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: