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 6660109
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:06:50+00:00 2026-05-26T02:06:50+00:00

I am trying to implement a linked list for a data structures class and

  • 0

I am trying to implement a linked list for a data structures class and I am having some difficulty with the searching portion of the algorithm.

Below is the offending code, which I have tried to implement following the pseudo-code in the MIT introduction to algorithms text:

//
// Method searches and retrieves a specified node from the list
//
Node* List::getNode(unsigned position)
{
    Node* current = m_listHead;

    for(unsigned i = m_listSize-1; (current != 0) && (i != position); --i)
            current = current->next;

    return current;
}

The head at this point in the program is the 4th node, which contains the value of int 5. the problem appears to be in the body of the for-loop, where the pointer to the node object is assigned to the next node. But this is going beyond the head of the node, so it is essentially pointing at some random location in memory (this makes sense).

Shouldn’t the algorithm be moving to the previous Node instead of the next Node in this case? Below is the pseudo-code:

LIST-SEARCH(L, k)
    x <- head
    while x != NIL and key != k
        do x <- next[x]
    return x

Also, here is the header file for my Linked list implementation. I haven’t tried to implement it in Template form yet just to keep things simple:

#ifndef linkList_H
#define linkList_h


//
// Create an object to represent a Node in the linked list object
// (For now, the objects to be put in the list will be integers)
//
struct Node
{
    // nodes of list will be integers
    int number;
    // pointer to the next node in the linked list
    Node* next;
};


//
// Create an object to keep track of all parts in the list
//
class List
{
public:

    // Contstructor intializes all member data
    List() : m_listSize(0), m_listHead(0) {}

    // methods to return size of list and list head
    Node* getListHead() const { return m_listHead; }
    unsigned getListSize() const { return m_listSize; }

    // method for adding a new node to the linked list, 
    // retrieving and deleting a specified node in the list
    void addNode(Node* newNode);
    Node* getNode(unsigned position);

private:

    // member data consists of an unsigned integer representing
    // the list size and a pointer to a Node object representing head
    Node* m_listHead;
    unsigned m_listSize;
};

#endif

Implementation of addNode method:

//
// Method adds a new node to the linked list
//
void List::addNode(Node* newNode)
{
    Node* theNode = new Node;

    theNode = newNode;
    theNode->next;
    m_listHead = theNode;

    ++m_listSize;
}
  • 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-26T02:06:51+00:00Added an answer on May 26, 2026 at 2:06 am

    Try this to construct the list:

    void List::addNode(int number) 
    { 
        newNode = new Node;
        newNode -> number = number;
        newNode -> next = m_listHead ;
        m_listHead = newNode;
        ++m_listSize; 
    } 
    

    It will add nodes to the head. Perhaps you may wish to store the pointer to the tail and insert the nodes there.

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

Sidebar

Related Questions

I have been trying to implement my own linked list class for didactic purposes.
I am trying to implement the linked list data structure using java, it works
I'm trying to implement a linked list but get an error when compiling: intSLLst.cpp:38:
I am trying to implement a singly linked list in C. A common implementation
I have been trying to implement XOR linked list and its operations but I
I'm trying to implement a linked list in C, and I want to store
I am trying to implement a simple linked list using c++. I am probably
I'm trying to write a some container classes for implementing primary data structures in
I'm trying to implement doubly-linked list in c++ and right now I'm adding iterator
As a pet project I'm trying to implement an immutable list data structure in

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.