反转链表与指针传递 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;
}
#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;
}
相关文章推荐
- C++ 值传递、指针传递、引用传递详解
- C++中引用传递与指针传递区别
- 给定一个链表的头指针,要求只遍历一次,将单链表中的元素顺序反转过来
- 关于VB的指针以及VB和C/C++间的参数传递
- c++之指针作为函数参数传递的问题
- 数据结构与算法(c++)——反转链表
- C++使用单指针Struct实现双向链表
- 【链表】C++链表反转、链表逆序打印
- C++值传递、指针传递、引用传递
- C++中值传递、指针传递、引用传递的总结
- (c/c++学习笔记九)引用传递、指针传递和值传递
- 【转】【c++】指针参数是如何传递内存的
- c++ 指针和引用传递
- 【C++】反转链表
- C/C++语言函数参数传递:传值,传指针,传引用
- C++链表K个节点K个节点的反转((1,2,3,4),如果k是2,反转结果是(2,1,4,3))
- 【编程题目】反转链表(C++实现)
- C++中引用传递与指针传递区别
- c++指针作为函数参数传递的问题
- 给初学者提个醒:C/C++函数里的指针参数也是按值传递的!