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

  • SEARCH
  • Home
  • 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 9173599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:36:51+00:00 2026-06-17T16:36:51+00:00

i made this linked list class in c++ and it works fine except after

  • 0

i made this linked list class in c++ and it works fine except after i run it the program goes unresponsive. i have located the line that’s causing the problem but i have no idea why. Even when i type it differently it still does the same thing.

Here’s my list class:

#include <string>

 template<class T>
 class List : public Object{
private: 
    Node<T>* first;
    Node<T>* last;
    int length;
public:
    List() : Object(new std::string("List")) {
        first = NULL;
        last = NULL;
        length = 0;
    }
    ~List() {
        delete first;
        delete last;
    }

    void Add(T value) {
        if(first==NULL)
            first = new Node<T>(NULL, value);
        else if(last==NULL)
            ---->last = new Node<T>(first, value);<-----
        else
            last = new Node<T>(last, value);
        length++;
    }

    T Remove(T value) {
        Node<T>* temp = first;
        while(temp!=NULL) {
            if(temp->GetValue()==value) {
                temp->GetPrev()->SetNext(temp->GetNext());
                temp->GetNext()->SetPrev(temp->GetPrev());
                delete temp;
                length--;
                return value;
            }
            temp = temp->GetNext();
        }
        return 0;
    }

    T Get(int index) {
        Node<T>* temp = first;
        int i = 0;
        while(temp!=NULL) {
            if(i==index)
                return temp->GetValue();
            i++;
            temp = temp->GetNext();
        }
        return 0;
    }
 };

when i remove the marked line above the program go unresponsive. This is my Node constructor:

#include <string>

template<class T>
class Node : public Object{
private:
    Node* next;
    Node* prev;
    T value;
public:
    Node(Node* prev, T value) : Object(new std::string("Node")){
        if(prev!=NULL) {
            prev->next = this;
            this->prev = next;
        } 
        next = NULL;
        this->value = value;
    }
    ~Node() {
        delete next;
    }

    T GetValue() {
        return value;
    }

    Node* GetNext() {
        return next;
    }

    Node* GetPrev() {
        return next;
    }
};

my object class:

#include <string>

class Object {
private:
    std::string* type;
public:
    Object() {
        type = new std::string("Object");
    }
    Object(std::string* type) {
        this->type = type;
    }
    ~Object() {
        delete type;
    }

    std::string* GetType() {
        return type;
    }
};

my Test.cpp

#include <iostream>
#include <string>

#include "Object.h"
#include "Node.h"
#include "List.h"

using namespace std;

int main () {

List<int> l;
l.Add(5);
l.Add(93);
l.Add(17);
l.Add(7789);
l.Add(60);

cout << "node 4 is:" << l.Get(3) << endl;

return 0;
}

error image http://i50.tinypic.com/2mw5phi.png
thanks for reading and please help as soon as you can, comment if you need me to supply more info.

  • 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-06-17T16:36:52+00:00Added an answer on June 17, 2026 at 4:36 pm

    Edit: There are many problems with your program, but what might be causing your crash: Your Add-function does not work correctly. It should be something like this:

    if(first==NULL) {
        first = new Node<T>(NULL, value);
        last = first;
    } else {
        last = new Node<T>(last, value);
    }
    length++;
    

    Otherwise, it will not correctly insert the second element. Why? With your original code, after the first add, your last is still NULL because of the else. So on the second add, you set last to new Node<T>(NULL, value). Therefore, it will not assign the first element’s next pointer. And your list will be inconsistent.

    Apart from that, there are double-frees, unnecessary heap-allocation of the string field in your Object class, ownership issues etc. To give you just one more example: Your List destructor will cause a heap corruption due to a double free. Calling delete first will delete all nodes due to the delete next in Node‘s destructor, as long as the list is consistent. Then you call delete last, but that object was already freed. This will corrupt your program’s memory management and can also cause a crash at program exit.

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

Sidebar

Related Questions

I have made a template class for a node in a linked list and
I had to make a Linked List program for my programming class. It works
I have a self made data structure (for example linked list) that works well,
i have to do a generic double linked list, and i made it in
I have made the following linked list which is not printing the list .The
I have made a singly linked list with node objects which contain a data
I have a problem with my code, I have made a singly linked list
Made this nice little loop for hiding and showing div's, works as a charm
I made this bash one-liner which I use to list Weblogic instances running along
I made this program for a school, and it involves a telnet server. When

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.