| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3483 人关注过本帖
标题:c++ 链表 插入节点迷惑
只看楼主 加入收藏
shining小南
Rank: 2
等 级:论坛游民
威 望:1
帖 子:47
专家分:42
注 册:2010-9-16
结帖率:80%
收藏
已结贴  问题点数:10 回复次数:11 
c++ 链表 插入节点迷惑
void insert(node * &head,char keyWord,char newdata)//keyWord 是查找关键字符
{
node *newnode=new node;//新建结点
newnode->data=newdata;//newdata 是新结点的数据
node *pGuard=search(head,keyWord);//pGuard 是插入位置前的结点指针
if (head==NULL || pGuard==NULL)//如果链表没有结点或找不到关键字结点
{//则插入表头位置
newnode->next=head;//先连
head=newnode;//后断
}
else//否则
{//插入在pGuard 之后
newnode->next=pGuard->next;//先连
pGuard->next=newnode;//后断
}
}
中的void insert(node * &head,char keyWord,char newdata)改成void insert(node * head,char keyWord,char newdata)好像也没问题,到底有什么区别呢?
int*&是什么意思呢?引用?
搜索更多相关主题的帖子: 节点 链表 
2010-10-08 22:25
hahayezhe
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:湖南张家界
等 级:贵宾
威 望:24
帖 子:1386
专家分:6999
注 册:2010-3-8
收藏
得分:0 
区别还是有的
一个是指针,指针可以改变其本身的指向,而不影响到实参
一个是指针的引用,改变本身的同时也改变到了实参
2010-10-09 08:49
crazy_pro
Rank: 2
等 级:论坛游民
帖 子:13
专家分:12
注 册:2010-9-17
收藏
得分:0 
以下是引用shining小南在2010-10-8 22:25:37的发言:

void insert(node * &head,char keyWord,char newdata)//keyWord 是查找关键字符
{
node *newnode=new node;//新建结点
newnode->data=newdata;//newdata 是新结点的数据
node *pGuard=search(head,keyWord);//pGuard 是插入位置前的结点指针
if (head==NULL || pGuard==NULL)//如果链表没有结点或找不到关键字结点
{//则插入表头位置
newnode->next=head;//先连
head=newnode;//后断
}
else//否则
{//插入在pGuard 之后
newnode->next=pGuard->next;//先连
pGuard->next=newnode;//后断
}
}
中的void insert(node * &head,char keyWord,char newdata)改成void insert(node * head,char keyWord,char newdata)好像也没问题,到底有什么区别呢?
int*&是什么意思呢?引用?
  楼上的朋友,int*&的意思不是引用,而是先求地址然后解引用哈!
2010-10-09 12:57
shining小南
Rank: 2
等 级:论坛游民
威 望:1
帖 子:47
专家分:42
注 册:2010-9-16
收藏
得分:0 
指针比试可以直接操作内存中的数据,用来修改实参???
2010-10-09 14:23
ciweitou163
Rank: 7Rank: 7Rank: 7
来 自:河北 石家庄
等 级:黑侠
威 望:1
帖 子:144
专家分:528
注 册:2008-10-4
收藏
得分:0 
回复 4楼 shining小南
是的,一个很经典的例子swap(int a,int b),swap(int * a,int * b),swap(int & a,int & b);具体的区别看看资料吧。


  • 满眼生机转化钧;天工人巧日争新。
2010-10-09 14:35
shining小南
Rank: 2
等 级:论坛游民
威 望:1
帖 子:47
专家分:42
注 册:2010-9-16
收藏
得分:0 
以下是引用hahayezhe在2010-10-9 08:49:25的发言:

区别还是有的
一个是指针,指针可以改变其本身的指向,而不影响到实参
一个是指针的引用,改变本身的同时也改变到了实参
node*&head 是先&head取head的地址,再*操作指针指向head的地址?是这个意思吗?
下面是我运行的程序eg输入:music# 结果四 music u u mulsic
写成node*head和node*&head结果一样 我想搞明白的是 这两种写法对程序的影响
#include "iostream.h"
struct node//定义结点结构类型
{
char data;//用于存放字符数据
node *next;//用于指向下一个结点(后继结点)
};
node * create();//创建链表的函数,返回表头
void showList(node *head);//遍历链表的函数,参数为表头
node *search(node *pRead,char keyword);
void insert(node * head,char keyWord,char newdata);
int main()
{
node *head;
head=create();//以head 为表头创建一个链表
showList(head);//遍历以head 为表头的链表
search(head,'u');
insert(head, 'u','l');
showList(head);
return 0;
}
node * create()
{
node *head=NULL;//表头指针,一开始没有任何结点,所以为NULL
node *pEnd=head;//表为指针,一开始没有任何结点,所以指向表头
node *pS;//创建新结点时使用的指针
char temp;//用于存放从键盘输入的字符
cout <<"Please input a string end with '#':" <<endl;
do//循环至少运行一次
{
cin >>temp;
if (temp!='#')//如果输入的字符不是结尾符#,则建立新结点
{
pS=new node;//创建新结点
pS->data=temp;//新结点的数据为temp
pS->next=NULL;//新结点将成为表尾,所以next 为NULL
if (head==NULL)//如果链表还没有任何结点存在
{
head=pS;//则表头指针指向这个新结点
}
else//否则
{
pEnd->next=pS;//把这个新结点连接在表尾
}
pEnd=pS;//这个新结点成为了新的表尾
}
}while (temp!='#');//一旦输入了结尾符,则跳出循环
return head;//返回表头指针
}
void showList(node *head)
{
node *pRead=head;//访问指针一开始指向表头
cout <<"The data of the link list are:" <<endl;
while (pRead!=NULL)//当访问指针存在时(即没有达到表尾之后)
{
cout <<pRead->data;//输出当前访问结点的数据
pRead=pRead->next;//访问指针向后移动
}
cout <<endl;
}
node * search(node *head,char keyword)//返回结点的指针
{
node *pRead=head;
while (pRead!=NULL)//采用与遍历类似的方法,当访问指针没有到达表尾之后
{
if (pRead->data==keyword)//如果当前结点的数据和查找的数据相符
{
    cout<<pRead->data<<endl;
return pRead;//则返回当前结点的指针
}
pRead=pRead->next;//数据不匹配,pRead 指针向后移动,准备查找下一个结点
}
return NULL;//所有的结点都不匹配,返回NULL
}
void insert(node * head,char keyWord,char newdata)//keyWord 是查找关键字符
{
node *newnode=new node;//新建结点
newnode->data=newdata;//newdata 是新结点的数据
node *pGuard=search(head,keyWord);//pGuard 是插入位置前的结点指针
if (head==NULL || pGuard==NULL)//如果链表没有结点或找不到关键字结点
{//则插入表头位置
newnode->next=head;//先连
head=newnode;//后断
}
else//否则
{//插入在pGuard 之后
newnode->next=pGuard->next;//先连
pGuard->next=newnode;//后断
}
}
2010-10-09 14:59
shining小南
Rank: 2
等 级:论坛游民
威 望:1
帖 子:47
专家分:42
注 册:2010-9-16
收藏
得分:0 
以下是引用hahayezhe在2010-10-9 08:49:25的发言:

区别还是有的
一个是指针,指针可以改变其本身的指向,而不影响到实参
一个是指针的引用,改变本身的同时也改变到了实参
node*&head 是先&head取head的地址,再*操作指针指向head的地址?是这个意思吗?
下面是我运行的程序eg输入:music# 结果四 music u u mulsic
写成node*head和node*&head结果一样 我想搞明白的是 这两种写法对程序的影响
#include "iostream.h"
struct node//定义结点结构类型
{
char data;//用于存放字符数据
node *next;//用于指向下一个结点(后继结点)
};
node * create();//创建链表的函数,返回表头
void showList(node *head);//遍历链表的函数,参数为表头
node *search(node *pRead,char keyword);
void insert(node * head,char keyWord,char newdata);
int main()
{
node *head;
head=create();//以head 为表头创建一个链表
showList(head);//遍历以head 为表头的链表
search(head,'u');
insert(head, 'u','l');
showList(head);
return 0;
}
node * create()
{
node *head=NULL;//表头指针,一开始没有任何结点,所以为NULL
node *pEnd=head;//表为指针,一开始没有任何结点,所以指向表头
node *pS;//创建新结点时使用的指针
char temp;//用于存放从键盘输入的字符
cout <<"Please input a string end with '#':" <<endl;
do//循环至少运行一次
{
cin >>temp;
if (temp!='#')//如果输入的字符不是结尾符#,则建立新结点
{
pS=new node;//创建新结点
pS->data=temp;//新结点的数据为temp
pS->next=NULL;//新结点将成为表尾,所以next 为NULL
if (head==NULL)//如果链表还没有任何结点存在
{
head=pS;//则表头指针指向这个新结点
}
else//否则
{
pEnd->next=pS;//把这个新结点连接在表尾
}
pEnd=pS;//这个新结点成为了新的表尾
}
}while (temp!='#');//一旦输入了结尾符,则跳出循环
return head;//返回表头指针
}
void showList(node *head)
{
node *pRead=head;//访问指针一开始指向表头
cout <<"The data of the link list are:" <<endl;
while (pRead!=NULL)//当访问指针存在时(即没有达到表尾之后)
{
cout <<pRead->data;//输出当前访问结点的数据
pRead=pRead->next;//访问指针向后移动
}
cout <<endl;
}
node * search(node *head,char keyword)//返回结点的指针
{
node *pRead=head;
while (pRead!=NULL)//采用与遍历类似的方法,当访问指针没有到达表尾之后
{
if (pRead->data==keyword)//如果当前结点的数据和查找的数据相符
{
    cout<<pRead->data<<endl;
return pRead;//则返回当前结点的指针
}
pRead=pRead->next;//数据不匹配,pRead 指针向后移动,准备查找下一个结点
}
return NULL;//所有的结点都不匹配,返回NULL
}
void insert(node * head,char keyWord,char newdata)//keyWord 是查找关键字符
{
node *newnode=new node;//新建结点
newnode->data=newdata;//newdata 是新结点的数据
node *pGuard=search(head,keyWord);//pGuard 是插入位置前的结点指针
if (head==NULL || pGuard==NULL)//如果链表没有结点或找不到关键字结点
{//则插入表头位置
newnode->next=head;//先连
head=newnode;//后断
}
else//否则
{//插入在pGuard 之后
newnode->next=pGuard->next;//先连
pGuard->next=newnode;//后断
}
}
2010-10-09 15:24
shining小南
Rank: 2
等 级:论坛游民
威 望:1
帖 子:47
专家分:42
注 册:2010-9-16
收藏
得分:0 
以下是引用ciweitou163在2010-10-9 14:35:21的发言:

是的,一个很经典的例子swap(int a,int b),swap(int * a,int * b),swap(int & a,int & b);具体的区别看看资料吧。

是这个吗?
#include"iostream.h"
void swap(int a,int b);
void swap1(int &a,int &b);
void swap(int *a,int *b);
void display(int a,int b);
int main()
{
    int a,b;
    cout<<"请输入两个数:";
    cin>>a>>b;
    int *m=&a,*n=&b;
    swap(a,b);
    display(a,b);
    swap1(a,b);
    display(a,b);
    swap(m,n);
    display(a,b);
    return 0;

   
}
void swap(int a,int b)
{
    int temp;
    temp=a;
    a=b;
    b=temp;
    return;
}
void swap1(int &a,int &b)
{
    int temp;
    temp=a;
    a=b;
    b=temp;
    return;
}
void swap(int *a,int *b)
{
    int temp;
    temp=*a;
    a=b;
    b=&temp;
    return;
}
void display(int a,int b)
{
    cout<<"a="<<a<<" "<<"b="<<b<<endl;
}
上面这个明白了,刚看了,这是根据书上该的
下面的是不明白的,帖子是截取的其中一部分
#include "iostream.h"
struct node//定义结点结构类型
{
char data;//用于存放字符数据
node *next;//用于指向下一个结点(后继结点)
};
node * create();//创建链表的函数,返回表头
void showList(node *head);//遍历链表的函数,参数为表头
node *search(node *pRead,char keyword);
void insert(node * head,char keyWord,char newdata);
void Delete(node * &head,char keyWord);
void destroy(node * &head);
int main()
{
node *head;
head=create();//以head 为表头创建一个链表
showList(head);//遍历以head 为表头的链表
search(head,'u');
insert(head, 'u','l');
showList(head);
Delete(head,'l');
showList(head);
destroy(head);
return 0;
}
node * create()
{
node *head=NULL;//表头指针,一开始没有任何结点,所以为NULL

node *pEnd=head;//表为指针,一开始没有任何结点,所以指向表头
node *pS;//创建新结点时使用的指针
char temp;//用于存放从键盘输入的字符
cout <<"Please input a string end with '#':" <<endl;
do//循环至少运行一次
{
cin >>temp;
if (temp!='#')//如果输入的字符不是结尾符#,则建立新结点
{
pS=new node;//创建新结点
pS->data=temp;//新结点的数据为temp
pS->next=NULL;//新结点将成为表尾,所以next 为NULL
if (head==NULL)//如果链表还没有任何结点存在
{
head=pS;//则表头指针指向这个新结点
}
else//否则
{
pEnd->next=pS;//把这个新结点连接在表尾
}
pEnd=pS;//这个新结点成为了新的表尾
}
}while (temp!='#');//一旦输入了结尾符,则跳出循环
return head;//返回表头指针
}
void showList(node *head)
{
node *pRead=head;//访问指针一开始指向表头
cout <<"The data of the link list are:" <<endl;
while (pRead!=NULL)//当访问指针存在时(即没有达到表尾之后)
{
cout <<pRead->data;//输出当前访问结点的数据
pRead=pRead->next;//访问指针向后移动
}
cout <<endl;
}
node * search(node *head,char keyword)//返回结点的指针
{
node *pRead=head;
while (pRead!=NULL)//采用与遍历类似的方法,当访问指针没有到达表尾之后
{
if (pRead->data==keyword)//如果当前结点的数据和查找的数据相符
{
return pRead;//则返回当前结点的指针
}
pRead=pRead->next;//数据不匹配,pRead 指针向后移动,准备查找下一个结点
}
return NULL;//所有的结点都不匹配,返回NULL
}
void insert(node * head,char keyWord,char newdata)//keyWord 是查找关键字符
{
node *newnode=new node;//新建结点
newnode->data=newdata;//newdata 是新结点的数据
node *pGuard=search(head,keyWord);//pGuard 是插入位置前的结点指针
if (head==NULL || pGuard==NULL)//如果链表没有结点或找不到关键字结点
{//则插入表头位置
newnode->next=head;//先连
head=newnode;//后断
}
else//否则
{//插入在pGuard 之后
newnode->next=pGuard->next;//先连
pGuard->next=newnode;//后断
}
}
void Delete(node * &head,char keyWord)//可能要操作表头指针,所以head 是引用
{
    if (head!=NULL)//如果链表没有结点,就直接输出提示
    {
        node *p;
        node *pGuard=head;//初始化pGuard 指针
        if (head->data==keyWord)//如果头结点数据符合关键字
        {
            p=head;//头结点是待删除结点
            head=head->next;//先连
            delete p;//后断
            cout <<"The deleted node is " <<keyWord <<endl;
            return;//结束函数运行
        }
        else//否则
        {
            while (pGuard->next!=NULL)//当pGuard 没有达到表尾
            {
                if (pGuard->next->data==keyWord)//如果pGuard 后继结点数据符合关键字
                {
p=pGuard->next;//pGuard 后继结点是待删除结点
pGuard->next=p->next;//先连
delete p;//后断
cout <<"The deleted node is " <<keyWord <<endl;
return;//结束函数运行
                }
                pGuard=pGuard->next;//pGuard 指针向后移动
            }
        }
    }
    cout <<"The keyword node is not found or the link list is empty!" <<endl;//输出提示信息
}
void destroy(node * &head)
{
    node *p;
    while (head!=NULL)//当还有头结点存在时
    {
        p=head;//头结点是待删除结点
        head=head->next;//先连
        delete p;//后断
    }
    cout <<"The link list has been deleted!" <<endl;
}




Delete(head,'l');调用head是不是相当于参数&head,那Delete函数中head中操作的不是main函数的head而是head的地址吗?

将声明和定义都改成
void showList(node *headnode *search(node *pRead,char keyword);
void insert(node * head,char keyWord,char newdata););//*&head改为*head
void Delete(node * head,char keyWord);//*&head改为*head
void destroy(node * head););//*&head改为*head
但对结果输入没有影响,我想问的是*&head对函数的影响
2010-10-09 17:25
无名可用
Rank: 4
等 级:业余侠客
帖 子:79
专家分:259
注 册:2010-7-27
收藏
得分:10 
'int* &a'是指针的引用,就像int &b表达的意思一样,这里b只是一个别名。同样的道理a也是一个别名,只不过是指针的别名.
你试试这两个的区别,也许你就会明白了.
1.   void Test(int* a)
    {
        int x=5;
        a=&x;
    }
    int main()
    {
        int* p;
        int b=3;
        p=&b;
        cout<<p<<endl;
        Test(p);
        cout<<p<<endl;
        return 0;
    }
2.   void Test(int* &a)
    {
        int x=5;
        a=&x;
    }
    int main()
    {
        int* p;
        int b=3;
        p=&b;
        cout<<p<<endl;
        Test(p);
        cout<<p<<endl;
        return 0;
    }
2010-10-09 21:12
肖付
Rank: 2
等 级:论坛游民
帖 子:53
专家分:24
注 册:2010-9-11
收藏
得分:0 
其实我也不太懂这个的,不过看了这些回复我还是大概懂了。因为我今天刚好学了这个。
2010-10-10 00:13
快速回复:c++ 链表 插入节点迷惑
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018165 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved