简体   繁体   English

链接节点列表,分段FAULT

[英]List of linked nodes , segmentation FAULT

I'd to create a list of linked nodes. 我要创建一个链接节点列表。 The idea is separate a word by letters, and creating a list of linked nodes by every different letter. 这个想法是用字母将单词分开,然后用每个字母创建一个链接节点列表。 Also I try to add the last node in the last position. 我也尝试将最后一个节点添加到最后一个位置。

I've tried a lot of things, Theorically it works, but I don't find where the segmentation fault origins. 从理论上讲,我已经尝试了很多方法,但是找不到分段错误的起源。

    struct nodo{
        int frecuencia;
        nodo *next;
        char letra;
};

int main(){

    string recibir; cin>>recibir; // recibir is the input.
    nodo *n;
    for(size_t i=0;i< recibir.size();i++){
    agregarnodoalista(n,recibir[i]);}

Add node to list Function:(If there is exist a node with the same letter, add +1 to the frequence). 将节点添加到列表功能:(如果存在一个具有相同字母的节点,则将+1添加到频率)。

void agregarnodoalista(nodo *&n, char caracter){
if(buscarletra(n,caracter)) localizarnodo(n,caracter)->frecuencia++;
    else{
    nodo *nodito;
    nodito= new nodo;
    nodo *ptr;
    while(ptr->next!=NULL){
            ptr=ptr->next;
    }
    ptr->next = nodito;
    nodito->letra=caracter;
    }}

Find letter in list and says if it is there or not. 在列表中找到字母并说是否存在。 function: 功能:

bool buscarletra(nodo *n, char letrita){
    nodo *ptr;
    for(ptr=n; ptr!=NULL;ptr=ptr->next){
            if(ptr->letra==letrita) return true;}   
    return false;}

Returns the pointer's node thar it has the letter. 返回指针所在节点的字母。

nodo *localizarnodo(nodo *n, char letrita){
    nodo *ptr;
    for(ptr=n; ptr!=NULL; ptr=ptr->next){
            if(ptr->letra==letrita) return ptr;
    }
    return NULL;}

Could you give me some help? 你能给我些帮助吗?

As mentioned in the comments you are accessing uninitialized pointers which is undefined behavior. 如注释中所述,您正在访问未初始化的指针,这是未定义的行为。 This code should work but you have memory leaks . 该代码应该可以工作,但是您会发生内存泄漏 I didn't check for other potential issues you might have. 我没有检查您可能遇到的其他潜在问题。

#include <iostream>
#include <string>
using namespace std;

struct nodo {
    int frecuencia;
    nodo *next;
    char letra;
};

bool buscarletra(nodo* n, char letrita) {
    nodo *ptr = n;
    for (; ptr != NULL; ptr = ptr->next) {
        if (ptr->letra == letrita) 
            return true;
    }
    return false;
}

nodo *localizarnodo(nodo *n, char letrita) {
    nodo *ptr;
    for (ptr = n; ptr != NULL; ptr = ptr->next) {
        if (ptr->letra == letrita) return ptr;
    }
    return NULL;
}

void agregarnodoalista(nodo *n, char caracter) {
    if (buscarletra(n, caracter)) localizarnodo(n, caracter)->frecuencia++;
    else {
        nodo *nodito;
        nodito = new nodo;
        nodo *ptr = new nodo;
        while (ptr->next != NULL) {
            ptr = ptr->next;
        }
        ptr->next = nodito;
        nodito->letra = caracter;
    }
}

int main() {

    string recibir; 
    cin >> recibir; // recibir is the input.
    nodo* n = new nodo;
    for (size_t i = 0; i < recibir.size() - 1; i++) {
        n->letra = recibir[i];
        nodo* next = new nodo;
        n->next = next;
        n = n->next;
    }
    n->letra = recibir[recibir.size() - 1];
    n->next = NULL;
    for (size_t i = 0; i < recibir.size(); i++) {
        agregarnodoalista(n, recibir[i]);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM