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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T07:18:15+00:00 2026-05-18T07:18:15+00:00

Having trouble getting one portion of my code to work. Building a rudimentary linked

  • 0

Having trouble getting one portion of my code to work. Building a rudimentary linked list to learn pointers. I think I have most of it down, but any attempts to use a function I created (push_back) throws memory access errors on setting a value for a pointer.

Not exactly sure what’s wrong, because it works fine to use push_front, which work almost exactly the same.

Any ideas? =/

CODE:

driver.cpp

#include <string>
#include <iostream>
#include "linklist.h"
#include "node.h"

using namespace std;

// printList function
// Purpose: Prints each node in a list
// Returns: None.
// Pre-Conditions: List must have member nodes.
// Post-Conditions: None.
void printList(linklist);

int main()
{
     linklist grocery;

     grocery.push_front(new node("milk", "1 gallon"));
     grocery.push_front(new node("bread","2 loaves"));
     grocery.push_front(new node("eggs","1 dozen"));
     grocery.push_front(new node("bacon","1 package"));
     cout << "First iteration:" << endl;
     printList(grocery);
     cout << "----------------------" << endl << endl;

     grocery.push_front(new node("hamburger","2 pounds"));
     grocery.push_front(new node("hamburger buns", "1 dozen"));
     cout << "Second iteration:" << endl;
     printList(grocery);
     cout << "----------------------" << endl << endl;

     node* deleteMe = grocery.pop_front();
     delete deleteMe;
     cout << "Third iteration:" << endl;
     printList(grocery);
     cout << "----------------------" << endl << endl;

     grocery.push_back(new node("orange juice","2 cans"));
     grocery.push_back(new node("swiss cheeese","1 pound"));
     cout << "Fourth iteration:" << endl;
     printList(grocery);
     cout << "----------------------" << endl << endl;

     deleteMe = grocery.pop_back();
     delete deleteMe;
     cout << "Fifth iteration:" << endl;
     printList(grocery);
     cout << "----------------------" << endl << endl;

     while (grocery.getNodeCount() != 0)
     {
          deleteMe = grocery.pop_front();
          cout << "Cleaning: " << deleteMe->getDescription() << endl;
          delete deleteMe;
     }

     system("PAUSE");
     return 0;
}

void printList(linklist input)
{
   node* temp = input.getFirst();
   for (int i = 0; i < (input.getNodeCount()); i++)
   {
      cout << temp->getQuantity() << " " << temp->getDescription() << endl;

      temp = temp->getNextNode();
   }
}

node.h

#pragma once
#include <string>

using namespace std;

class node
{
public:
// Default Constructor
// Values, "none", "none", NULL.
node();

// Parameterized Constructor
// nextNode initialized NULL and must be explicitly set.
node(string descriptionInput, string quantityInput);

// getDescription function
// Purpose: Returns node description.
// Returns: string
// Pre-Conditions: None.
// Post-Conditions: None.
string getDescription();

// setDescription function
// Purpose: Sets node description
// Returns: Void
// Pre-Conditions: None
// Post-Conditions: None
void setDescription(string);

// getQuantity function
// Purpose: Returns node quantity.
// Returns: string
// Pre-Conditions: None.
// Post-Conditions: None.
string getQuantity();

// setQuantity function
// Purpose: Sets node quantity
// Returns: Void
// Pre-Conditions: None
// Post-Conditions: None
void setQuantity(string);

// getNextNode function
// Purpose: Returns pointer to next node in list sequence.
// Returns: node pointer
// Pre-Conditions: None.
// Post-Conditions: None.
// Note: Not set during initialization. Must be explicitly done.
node* getNextNode();

// setNextNode function
// Purpose: Sets pointer to next node in list sequence.
// Returns: None.
// Pre-Conditions: None.
// Post-Conditions: None.
// Note: Not set during initialization. Must be explicitly done.
void setNextNode(node*);
private:
string description;
string quantity;
node* nextNode;
};

node.cpp

#include "node.h"


node::node()
  :description("none"),
  quantity("none"),
  nextNode(NULL)
  {}

node::node(string descriptionInput, string quantityInput)
  :description(descriptionInput),
  quantity(quantityInput),
  nextNode(NULL)
  {}

string node::getDescription()
{
   return description;
}

void node::setDescription(string descriptionInput)
{
   description = descriptionInput;
}

string node::getQuantity()
{
   return quantity;
}

void node::setQuantity(string quantityInput)
{
   quantity = quantityInput;
}

node* node::getNextNode()
{
   return nextNode;
}

void node::setNextNode(node* input)
{
   nextNode = input;
}

linklist.h

#pragma once
#include "node.h"

class linklist
{
public:
// Constructor
// Builds an empty list
linklist();

// push_front function
// Purpose: Takes node pointer. Places that node at beginning of list.
// Returns: None
// Pre-Conditions: None
// Post-Conditions: None
void push_front(node*);

// pop_front function
// Purpose: Removes first node from list.
// Returns: Node pointer. NODE IS NOT DESTROYED.
// Pre-Conditions: List must have a node to remove.
// Post-Conditions: Node is not destroyed.
node* pop_front();

// getFirst function
// Purpose: Returns node pointer to first node in list
// Returns: node pointer
// Pre-Conditions: List must have a node added.
// Post-Conditions: None.
node* getFirst();

// push_back function
// Purpose: Takes node pointer. Places that node at end of list.
// Returns: None
// Pre-Conditions: None
// Post-Conditions: None
void push_back(node*);

// pop_back function
// Purpose: Removes last node from list.
// Returns: Node pointer. NODE IS NOT DESTROYED.
// Pre-Conditions: List must have a node to remove.
// Post-Conditions: Node is not destroyed.
node* pop_back();

// getNodeCount function
// Purpose: Returns nodeCount
// Returns: int
// Pre-Conditions: None.
// Post-Conditions: None.
int getNodeCount();
private:
node* firstNode;
node* lastNode;
int nodeCount;
};

linklist.cpp

#include "linklist.h"


linklist::linklist()
    :firstNode(NULL),
    lastNode(NULL),
    nodeCount(0)
{}

void linklist::push_front(node* input)
{
    node* temp = getFirst();

    input->setNextNode(temp);

    firstNode = input;
    nodeCount++;
}

node* linklist::pop_front()
{
    node* temp = getFirst();

    firstNode = temp->getNextNode();

    nodeCount--;
    return temp;
}

node* linklist::getFirst()
{
    return firstNode;
}

void linklist::push_back(node* input)
{
    node* temp = lastNode;

    temp->setNextNode(input);

    lastNode = temp;
    nodeCount++;
}

node* linklist::pop_back()
{
    node* oldLast = lastNode;
    node* temp = firstNode;

    // find second to last node, remove it's pointer
    for (int i = 0; i < (nodeCount - 1); i++)
    {
        temp = temp->getNextNode();
    }
    temp->setNextNode(NULL);

    lastNode = temp;

    nodeCount--;
    return oldLast;
}

int linklist::getNodeCount()
{
    return nodeCount;
}
  • 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-18T07:18:16+00:00Added an answer on May 18, 2026 at 7:18 am

    According to node‘s definition it’s a single-linked list. (otherwise you’d have to contain also prevNode).

    Hence – manipulating the “back” of your list is non-trivial. To pop the “back” of your list you’ll need to transverse the entire list and identify the new “last” element.

    Are you sure you do this right? Including handling all the “extremal” cases (like removing last element and etc.)?

    It’d be nice to post the code of push_back and pop_back.

    P.S. Perhaps you don’t set lastNode correctly in your push_front and pop_front. You may not notice this unless you’re trying to manipulate your “back”.

    Post the code of push_front and pop_front as well.

    EDIT:

    Alright, I see the code. And there’re plenty of errors.

    void linklist::push_front(node* input)
    {
        node* temp = getFirst();
    
        input->setNextNode(temp);
    
        firstNode = input;
        nodeCount++;
    
        // Missing:
        if (!temp)
            lastNode = firstNode;
    }
    
    
    node* linklist::pop_front()
    {
        node* temp = getFirst();
    
        firstNode = temp->getNextNode();
    
        // Missing:
        if (!firstNode)
            lastNode = 0;
    
        nodeCount--;
        return temp;
    }
    
    
    void linklist::push_back(node* input)
    {
        node* temp = lastNode;
    
        // instead of
        // temp->setNextNode(input);
        // lastNode = temp;
    
        // should be:
        if (temp)
            temp->setNextNode(input);
        else
            firstNode = input;
    
        lastNode = input;
    
        nodeCount++;
    }
    
    node* linklist::pop_back()
    {
        node* oldLast = lastNode;
    
        if (firstNode == lastNode)
        {
            firstNode = 0;
            lastNode = 0;
        } else
        {
            node* temp = firstNode;
    
            // find second to last node, remove it's pointer
            for (int i = 0; i < (nodeCount - 1); i++)
            {
                temp = temp->getNextNode();
            }
            temp->setNextNode(NULL);
    
            lastNode = temp;
        }
    
        nodeCount--;
        return oldLast;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.