Home > Software design >  Logical error in implementing names linked list in c
Logical error in implementing names linked list in c

Time:12-30

I m facing an issue with a program. I have to implement a project to maintain data on names with their meaning. I already have a text file dataset that contains some data. The program I have to code in which I have to do file handling to import that data into the program and create nodes for every name and then display every name with their meaning.

I used a singly linked list with the InsertAtTail() and Display() functions. The display function is only displaying the data of one name from the text file dataset.

Below is my code

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

class Node{
    public:
        string NAME;
        string MEANING;
        char INITIAL;
        Node *link;
        
        Node(){
        }
};

class LinkedList{
    private:
        Node *head, *tail, *current, *temp;
    public:
        LinkedList(){
            head=NULL;
        }
        
        void InsertAtTail(char initial, string name, string meaning){
            if(head==NULL){
                head = new Node;
                head->INITIAL = initial;
                head->NAME = name;
                head->MEANING = meaning;
                head->link = NULL;              
            }
            else{
                temp = new Node;
                temp->INITIAL = initial;
                temp->NAME = name;
                temp->MEANING = meaning;
                temp->link = NULL;
                tail->link = temp;
                tail = temp;
            }
        }
        
        void display(){
            Node *current = head;
            while(current!=NULL){
                cout<<"Initial: "<<current->INITIAL;
                cout<<"\tName: "<<current->NAME;
                cout<<"\tMeaning: "<<current->MEANING<<endl;
                current = current->link;
            }
            
        }
};

int main(){
    LinkedList list;
    fstream boys( "boys.txt", ios::in );
    string name, meaning;
    char initial;
    while(boys >> name >> meaning >> initial)
    {
        list.InsertAtTail (initial, name, meaning);
    }
    list.display();
    boys.close();
    return 0;
}

Here is my output:

Initial: A      Name: Aaidan    Meaning: Young

CodePudding user response:

Answering the comment of OP:

Please check the following code. On my machine it works

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

class Node {
public:
    string NAME;
    string MEANING;
    char INITIAL;
    Node* link;

    Node() {
    }
};

class LinkedList {
private:
    Node* head, * tail, * current, * temp;
public:
    LinkedList() {
        head = NULL;
    }

    void InsertAtTail(char initial, string name, string meaning) {
        if (head == NULL) {
            head = new Node;
            head->INITIAL = initial;
            head->NAME = name;
            head->MEANING = meaning;
            head->link = NULL;
            tail = head;
        }
        else {
            temp = new Node;
            temp->INITIAL = initial;
            temp->NAME = name;
            temp->MEANING = meaning;
            temp->link = NULL;
            tail->link = temp;
            tail = temp;
            }
        }

    void display() {
        Node* current = head;
        while (current != NULL) {
            cout << "Initial: " << current->INITIAL;
            cout << "\tName: " << current->NAME;
            cout << "\tMeaning: " << current->MEANING << endl;
            current = current->link;
        }

    }
};

int main() {
    LinkedList list;
    fstream boys("r:\\boys.txt", ios::in);
    string name, meaning;
    char initial;
    while (boys >> name >> meaning >> initial)
    {
        list.InsertAtTail(initial, name, meaning);
    }
    list.display();
    boys.close();
    return 0;
}

Test data:

1 2 3 4 5 6 7 8 9 
a s d f g h j k l

Output: enter image description here

My assumption is that you maybe have problems with the source data in the file.

Please comment on this.

  • Related