| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1132 人关注过本帖
标题:数据结构中的next
只看楼主 加入收藏
jjg
Rank: 2
等 级:论坛游民
帖 子:67
专家分:42
注 册:2009-8-19
结帖率:46.15%
收藏
 问题点数:0 回复次数:9 
数据结构中的next
问题一:编译器怎么知道next是指向下一个节点,能不能给个例子说明一下。
问题二:下面这段代码
 typedef   struct   file   
    {   
    int   file_id;   
    char   file_name[10];   
    int   file_size;   
    char   content[50];   
    struct   file   *next;   
    }file,*myfile;
看不懂红色部分,没有见过这样的表示方法,给个解释吧,按我的理解呢,struct和class差不多,设置访问级别就一样了,我可不可以这样改呢: typedef   class  file   
    {   
    int   file_id;   
    char   file_name[10];   
    int   file_size;   
    char   content[50];   
    struct   file   *next;   
    }file,*myfile;
按平时typedef的用法,如typedef name float;那么前面的代码是不是将
class  file   
    {   
    int   file_id;   
    char   file_name[10];   
    int   file_size;   
    char   content[50];   
    struct   file   *next;   
    }
定义为了file类类型了,后面的*myfile又怎么解释呢?
搜索更多相关主题的帖子: next 数据结构 
2009-10-13 23:01
zodiac207
Rank: 2
等 级:论坛游民
帖 子:16
专家分:38
注 册:2009-10-14
收藏
得分:0 
netx指针指向下一个结点这一功能要自己实现的,就是你新建了一个结点NODE,将前一结点的NEXT指针指向你新建的结点NODE

至于你说的第二个问题,
typedef   struct   file   
    {   
    int   file_id;   
    char   file_name[10];   
    int   file_size;   
    char   content[50];   
    struct   file   *next;   
    }file,*myfile;
是定义一个结构,名字为file,同时声明一个结构变量file(怀疑你输少了东西,使这个变量和定义重名),和一个结构体file的指针*myfile
2009-10-14 10:27
jjg
Rank: 2
等 级:论坛游民
帖 子:67
专家分:42
注 册:2009-8-19
收藏
得分:0 
回复 2楼 zodiac207
能不能实现一下
2009-10-14 10:40
zodiac207
Rank: 2
等 级:论坛游民
帖 子:16
专家分:38
注 册:2009-10-14
收藏
得分:0 
/code
#include<iostream>
using namespace std;
struct file   //定义结点
    {
        int id;
        char name[10];
        struct file *next;
    };
int output(struct file *head);//定义输出链表函数
int main()
{
    struct file *p,*head,*q;
    head=NULL;
    p=NULL;
    p=new file;
    head=p;
    q=p;
cout<<"please input thd data of id and name:"<<endl;
cin>>p->id>>p->name;
while(p->id!=0)    //循环创建结点并输入结点数据,当ID=0时结束输入
{
 
  p=new file;
  q->next=p;
  cout<<"please input thd data of id and name:"<<endl;
  cin>>p->id>>p->name;
  q=p;
 
}
    q->next=NULL;
    output(head);
    return 0 ;
}
int output(struct file *head)//输出链表
{
    file *p,*q;
    p=head;
    q=head;
    while(p)
    {
      cout<<"id: "<<p->id<<"name: "<<p->name<<endl;
     p=q->next;
     q=p;
    }
    return 0;
}
/code

类似就是这样
2009-10-14 17:55
jjg
Rank: 2
等 级:论坛游民
帖 子:67
专家分:42
注 册:2009-8-19
收藏
得分:0 
谢了,你学C++多久了?这么厉害
2009-10-14 21:59
zodiac207
Rank: 2
等 级:论坛游民
帖 子:16
专家分:38
注 册:2009-10-14
收藏
得分:0 
可以说很久,也可以说不久,看态度和用心程序罢了.

还后悔着以前不用心学,唉~~~~~~~
2009-10-15 09:04
jjg
Rank: 2
等 级:论坛游民
帖 子:67
专家分:42
注 册:2009-8-19
收藏
得分:0 
回复 6楼 zodiac207
我要有你那么厉害就好了,我都大三了
2009-10-15 09:20
jjg
Rank: 2
等 级:论坛游民
帖 子:67
专家分:42
注 册:2009-8-19
收藏
得分:0 
回复 6楼 zodiac207
我自己写了一个,你能不能给我改一下错,我实在改不出来,代码如下:
#include <iostream>
using namespace std;
struct node
{
    int data;
    node *next;
};

int init()
{
    node * head;
    head=NULL;
    return head;
}
int insert(node a)
{

    while(p->data!=0)
    {
        p=new node;
        cout>>"please enter some node:";cin>>a;
        head=p;
        p->data=a.data;
        p=p->next;
    }
    return head;
}
void find(node b)
{
    cout<<"please enter b:"<<endl;cin>>b;
    while(p!=NULL)
    {
        if(b.data==p->data)
        {
            cout<<"have find node b"<<endl;
            break;
        }
        else
            p=p->next;
    }
}
void main()
{
    node *p;

    init();
    insert(a);
    find(b);
}








2009-10-16 17:00
zodiac207
Rank: 2
等 级:论坛游民
帖 子:16
专家分:38
注 册:2009-10-14
收藏
得分:0 
最近少上网,才看到你的回复.

我不知道你的算法有没有错,就说说我看出的语法上的错误吧
#include <iostream>
using namespace std;
struct node
{
    int data;
    node *next;
};
 
struct node* init() //和insert()函数一样的错误.
{
    node * head;
    head=NULL;
    return head;
}
struct node* insert(node *a) //这里你返回的是一个node型指针,所以函数的返回值是node*型,而你的函数形参也应该是一样,应该是指针型.
{
 
    while(p->data!=0)
    {
        p=new node;
        cout>>"please enter some node:";cin>>a; //这里输入的数据是什么,a没见到定义.如果是给node结构定义的的a 变量,那么就应该赋值给a->data,而不是a.下面的find也应该注意这个问题.
        head=p;
        p->data=a.data;
        p=p->next;
    }
    return head;
}
void find(node *b) //这里的形参也应该是node型指针.
{
    cout<<"please enter b:"<<endl;cin>>b;
    while(p!=NULL)
    {
        if(b.data==p->data)
        {
            cout<<"have find node b"<<endl;
            break;
        }
        else
            p=p->next;
    }
}
void main()
{
    node *p;
 
    init();
    insert(a); //这个函数中的实参a是怎么来,主函数没定义,
    find(b); //和上面一样的错误.
}
2009-10-17 14:14
sdj501
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2009-8-31
收藏
得分:0 
回复 楼主 jjg
各位举得struct和class哪个更好用点呢?哎,现在我都把C和C++搞混了,郁闷啊!!!!
2009-10-18 04:04
快速回复:数据结构中的next
数据加载中...
 
   



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

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