Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6924947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:43:37+00:00 2026-05-27T10:43:37+00:00

I am making a program for singly linked lists using multiple files and classes.

  • 0

I am making a program for singly linked lists using multiple files and classes.

I have to have a Node.h, LinkedList.h, Node.cpp, LinkedList.cpp, and a main.cpp

I was having other problems but now my printList() function just prints “List()” instead of “List(node 1, node2, etc…)”

I think my insert might be the problem because my searchNode() doesn’t work right either, it always says node not found.

Here is the code I have: (I can’t change the Node.h and LinkedList.h files)

Node.h:

//
//  Node.h
//  Linked Lists
//

#ifndef Linked_Lists_Node_h
#define Linked_Lists_Node_h

class Node
{
public:
    Node(int data);
    int data;
    Node *next;

};

#endif

LinkedList.h:

//
//  LinkedList.h
//  Linked Lists
//

#ifndef Linked_Lists_LinkedList_h
#define Linked_Lists_LinkedList_h

#include "Node.h"

class LinkedList
{
private:
    Node *head;

public:
    LinkedList();
    void addNode(int data);
    void removeNode(int data);
    bool searchNode(int data);
    void printList();

};

#endif

Node.cpp

//
//  Node.cpp
//  Linked Lists
//

#include <iostream>
#include <cstdlib>

#include "LinkedList.h"
#include "Node.h"

using namespace std;

Node::Node(int data) {};

LinkedList.cpp

//
//  LinkedList.cpp
//  Linked Lists
//

#include <iostream>
#include <cstdlib>

#include "LinkedList.h"
#include "Node.h"

using namespace std;

LinkedList::LinkedList()
{
    head = NULL;
}

void LinkedList::addNode(int data)
{
    Node *newNode;
    newNode->data = data;
    newNode->next = NULL;

    Node *tmp = head;

    if(tmp != NULL)
    {
        while(tmp->next != NULL)
        {
            tmp = tmp->next;
        }

        tmp->next = newNode;
    }
    cout << "Node added" << endl;
    printList();
}

void LinkedList::removeNode(int data)
{
    Node *tmp = head;

    if(tmp == NULL)
    {
        cout << "No node removed" << endl;
        return;
    }

    if(tmp->next == NULL)
    {
        delete tmp;
        head = NULL;
    }
    else
    {
        Node *previous;

        do
        {
            if(tmp->data == data)
            {
                break;
            }
            previous = tmp;
            tmp = tmp->next;
        }
        while(tmp != NULL);

        previous->next = tmp->next;

        delete tmp;
    }
    cout << "Node removed" << endl;
    printList();
}

bool LinkedList::searchNode(int data)
{
    Node *tmp = head;

    while(tmp != NULL)
    {
        if(tmp->data == data)
        {
            cout << "Node found" << endl;
            printList();
            return true;
        }
        tmp = tmp->next;
    }
    cout << "Node not found" << endl;
    printList();
    return false;
}

void LinkedList::printList()
{
    Node *tmp = head;

    if(tmp == NULL)
    {
        cout << "List()" << endl;
        return;
    }

    if(tmp->next == NULL)
    {
        cout << "List(" << tmp->data << ")";
    }
    else
    {
        do 
        {
            cout << "List(" << tmp->data;
            cout << ", ";
            tmp = tmp->next;
        } 
        while (tmp != NULL);

        cout << ")" << endl;
    }
}

main.cpp

//
//  main.cpp
//  Linked Lists
//

#include <iostream>
#include <cstdlib>

#include "LinkedList.h"
#include "Node.h"
#include "LinkedList.cpp"

using namespace std;

int main ()
{
    LinkedList list;

    int data;
    int choice;

    while(1) 
    {
        cout << " Select:" << endl;
        cout << "1 to add a node" <<endl;
        cout << "2 to remove a node" << endl;
        cout << "3 to search for a node" << endl;
        cout << "4 to exit" << endl;
        cout << endl;

        cin >> choice;

        switch(choice)
        {
            case 1: //insertion
                cout << "Enter node: ";
                cin >> data;
                list.addNode(data); //add a node
                break;
            case 2: //deletion
                cout << "Enter node: ";
                cin >> data;
                list.removeNode(data); //remove a node
                break;
            case 3: //search
                cout << "Enter node: ";
                cin >> data;
                list.searchNode(data); //search for a node
                break;
            case 4:
                exit(0); //exit the program
                break;
            default: //default case
                cout << "Please enter a valid choice (1 - 4)!" << endl;
                break;
        }
    }
    return 0;
}

If you could help me figure out my problem I would greatly appreciate it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T10:43:38+00:00Added an answer on May 27, 2026 at 10:43 am

    You’re not adding any nodes. If head is NULL, your add Node becomes:

    void LinkedList::addNode(int data)
    {
        Node *newNode;
        newNode->data = data;
        newNode->next = NULL;
    
        Node *tmp = head;
    
        if(tmp != NULL)
        { 
            //this never gets executed
        }
        cout << "Node added" << endl;
        printList();
    }
    

    You need to treat this case (first insertion):

    void LinkedList::addNode(int data)
    {
        Node *newNode;
        newNode->data = data;
        newNode->next = NULL;
    
        Node *tmp = head;
    
        if(tmp != NULL)
        {
            while(tmp->next != NULL)
            {
                tmp = tmp->next;
            }
    
            tmp->next = newNode;
        }
        else
        {
            head = newNode;
        }
        cout << "Node added" << endl;
        printList();
    }
    

    This will create the head if it doesn’t exist already.

    Are you using a debugger? It would be much easier (for you) if you did.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am making a program for singly linked lists using multiple files and classes.
I am using the cool map making program DIY map and i want to
I'm making a program where you're firing a 'blaster', and I have 5 ammo.
I'm making a typing tutor program using javascript. Everything is going well except that
I'm making a simple steganography program to hide data in PNG files. Decoding/encoding single
I'm making a program that adds executable files to a listBox in C#. I'm
I have created a captcha image making program with PHP <?php function word($n) {
If something is making a single-thread program take, say, 10 times as long as
I'm making a program which the user build directories (not in windows, in my
I'm making a program that fits the wizard concept ideally; the user is walked

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.