帮助将数据存储在链表中 - 具有3种数据类型的节点 [英] Help with storing data in linked list - - node with 3 data types

查看:139
本文介绍了帮助将数据存储在链表中 - 具有3种数据类型的节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。我已经对这个问题感到困惑了一段时间,并且已经筋疲力尽了youtube和google。我在将数据插入多数据链表节点时遇到问题。我不确定我是在吹指针还是什么,但我非常困惑。





<如果我打电话< br $>


inFile1>> S>> N;

insertnode(head,S,N);



单独没有循环,它将数据存储在链表中并打印它(带有额外的非预期值==> 6.83919e + 025,我认为它是一个内存位置地址)>



任何帮助都将是非常感谢。



我的尝试:



 #include< iostream> 
#include< fstream>
#include< string>
#include< iomanip>
#include< cstring>使用namespace std

;

struct node
{
string element;
双倍重量;
node * next;
};

void insertnode(node * cp,string,double);
void printlist(node * cp);

node * head;

ifstream inFile1,inFile2;
ofstream outFile;

int main()
{
inFile1.open(Element.txt,ios :: in);
inFile2.open(Formula.txt,ios :: in);

string S;
双N;
int i;
head =新节点;
head->元素;
node * c;

c = head;

// while(inFile1>> S)
// {
inFile1>> S>> N;
insertnode(head,S,N);
inFile1>> S>> N;
insertnode(head,S,N);
inFile1>> S>> N;
insertnode(head,S,N);
//}


printlist(head);


inFile1.close();
inFile2.close();

返回0;
}
节点*
newnode()
{
node * t;

t =新节点;
t-> element =;
t-> weight = 0;
t-> next = NULL;
返回t;
}
void insertnode(node * cp,string S,double N)
{
node * p,* t;
p = NULL;

if(head == NULL)
{
head = newnode();
head-> element = S;
head-> weight = N;
}
其他
{
cp = head;
while(cp!= NULL&& cp-> element.compare(S)> 0)
{
p = cp;
cp = cp-> next;
}
if(p!= NULL)
{
t = newnode();
t-> element = S;
t-> weight = N;
p-> next = t;
t-> next = cp;
}
其他
{
t = newnode();
t-> element = S;
t-> weight = N;
t-> next = cp;
head = t;
}
}
}
void printlist(node * cp)
{
while(cp)
{
cout< ;< cp->元素<< setw(8)<< cp->重量<< ENDL;
cp = cp-> next;
}
}

解决方案

在我看来,问题在于初始化列表。首先在其声明中将Node设置为nullptr。接下来,将值分配给一个结构的成员,并且只在一个地方。这将简化其余逻辑。这是一个小例子 -

  struct 节点
{
string element;
double 重量;
node * next;

node() // 构造函数
{
设置( 0 nullptr );
}

void 设置(字符串e, double w, node * pnext = nullptr
{
element = e;
weight = w;
next = pnext;
}

静态节点* Create() // < span class =code-comment>这将替换newnode函数
{ // 用法是node * pn = node :: Create();
return new 节点;
}
};

typedef node * pnode;

pnode Head = nullptr ; // 我将全局变量的首字母大写

插入函数可以相当大现在简化了。但是还有另一个问题。也就是说,您将head作为参数传递给insert函数,并将其作为全局变量访问。这几乎总是很糟糕。通常,传递指向列表头部的指针,但这不是绝对必要的。这是一种仅将其作为全局变量处理的方法。

  void  insertnode(string s, double  n)
{
if (!Head)
{
Head = node ::创建();
Head-> Set(s,n);
return ;
}

node * prv = NULL;
node * cur = Head;
while (cur&&(cur-> element.compare(s)> 0 ))
{
prv = cur;
cur = cur-> next;
}

node * pnew = node :: Create();
pnew-> Set(s,n,cur);

if (prv)
prv-> next = pnew;
else
Head = pnew; // 这是新的元素
}

这个代码,你没有将列表的头部传递给函数。你很容易,但你必须传递指针的地址。我认为你还没准备好。



如果指向列表头部的指针被传递,那么这将是原型:

  void  insertnode(pnode * pHead,string e, double  w); 


Hello everyone. I've been pretty stumped with this problem for a little while and have exhausted youtube and google. I'm having trouble inserting data into a multi data linked list node. I'm not sure if I am blowing a pointer or what, but I am severely confused.


<If I call

inFile1 >> S >> N;
insertnode(head,S,N);

separately without the loop, it stores the data in the linked lists and prints it out (with an extra unintended value ==> 6.83919e+025, which I'm thinking is a memory location address)>

Any help will be greatly appreciated.

What I have tried:

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <cstring>

using namespace std;

struct node
{
    string element;
    double weight;
    node *next;
};

void insertnode(node *cp,string,double);
void printlist(node *cp);

node *head;

ifstream inFile1, inFile2;
ofstream outFile;

int main()
{
    inFile1.open("Element.txt", ios::in);
    inFile2.open("Formula.txt", ios::in);

    string S;
    double N;
    int i;
    head = new node;
    head->element;
    node *c;

    c = head;

    //while(inFile1 >> S)
    //{
        inFile1 >> S >> N;
        insertnode(head,S,N);
        inFile1 >> S >> N;
        insertnode(head,S,N);
        inFile1 >> S >> N;
        insertnode(head,S,N);
    //}


    printlist(head);


    inFile1.close();
    inFile2.close();

    return 0;
}
node *
newnode()
{
    node *t;

    t = new node;
    t->element = "";
    t->weight = 0;
    t->next = NULL;
    return t;
}
void insertnode(node *cp, string S, double N)
{
    node *p, *t;
    p = NULL;

    if(head == NULL)
    {
        head = newnode();
        head->element = S;
        head->weight = N;
    }
    else
    {
        cp = head;
        while(cp != NULL && cp->element.compare(S) > 0)
        {
            p = cp;
            cp = cp->next;
        }
        if(p != NULL)
        {
            t = newnode();
            t->element = S;
            t->weight = N;
            p->next = t;
            t->next = cp;
        }
        else
        {
            t = newnode();
            t->element = S;
            t->weight = N;
            t->next = cp;
            head = t;
        }
    }
}
void printlist(node *cp)
{
    while(cp)
    {
        cout << cp->element << setw(8) << cp->weight << endl;
        cp = cp->next;
    }
}

解决方案

It appears to me the problem is in initializing the list. Start by setting Node to nullptr at its declaration. Next, assign values to the members of a structure in one and ONLY one place. This will simplify the rest of your logic. Here's a small sample of that -

struct node
{
    string element;
    double weight;
    node * next;

    node()   // constructor
    {
        Set( "", 0, nullptr );
    }

    void Set( string e, double w, node* pnext=nullptr )
    {
        element = e;
        weight = w;
        next = pnext;
    }

    static node * Create()   // this replaces the newnode function
    {                        // usage is node *pn = node::Create();
        return new node;
    }
};

typedef node * pnode;

pnode Head = nullptr;  // I capitalize first letter of global variables

The insert function can be considerably simplified now. There is another problem though. That is you are passing head as an argument to the insert function and also accessing it as a global variable. That will almost always work out badly. Usually, a pointer to the head of the list is passed but that isn't absolutely necessary. Here's a way to deal with it only as a global variable.

void insertnode( string s, double n )
{
    if( ! Head )
    {
        Head = node::Create();
        Head->Set( s, n );
        return;
    }

    node * prv = NULL;
    node * cur = Head;
    while( cur && ( cur->element.compare(s) > 0 ) )
    {
        prv = cur;
        cur = cur->next;
    }

    node * pnew = node::Create();
    pnew->Set( s, n, cur );

    if( prv )
        prv->next = pnew;
    else
        Head = pnew;   // this is the new head element
}

With this code, you don't pass the head of the list to the function. You easily could but you have to pass the address of the pointer. I didn't think you are quite ready for that yet.

This would be the prototype if a pointer to the head of the list is passed:

void insertnode( pnode * pHead, string e, double w );


这篇关于帮助将数据存储在链表中 - 具有3种数据类型的节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆