New to classes and Objects in c++

New to classes and Objects in c++



I'm new to classes and Objects in C++..I cannot understand why the linked list is not being created. It just prompts the first value then crashes. I cannot figure out where the problem is and already wasted too many hours on this. Finally decided to get some help. Thank You for your time.


#include <iostream>
using namespace std;

class Node
private:
int data;
Node* next;
Node* previous;

public:

Node(int value)
data = value;
next = NULL;
previous = NULL;


void setValue(int value)

data = value;


int getValue()

return data;


Node* getNext()

return next;


void setNext(Node* address)

next = address;


Node* getPrevious()
return previous;


void setPrevious(Node* address)
previous = address;


;


class LinkedList
private:
Node* head;
Node* tail;

public:

LinkedList()
Node* head = NULL;
Node* tail = NULL;




void createLinklist()

int n;
cout << "Enter the number of nodes = ";
cin >> n;



for(int i=0;i<n;i++)

int value;
cout << "Enter the value at " << i <<"=";
cin >> value;

Node* node = new Node(value);

if(head == NULL)

head = node;
tail = node;

else
insertAtEnd(node,tail);







void insertAtEnd(Node* newNode,Node* lastNode)

lastNode->setNext(newNode);
newNode->setPrevious(lastNode);
newNode->setNext(NULL);
tail = newNode;







void display()

Node* start = head;

while(start!=NULL)

cout << "Address=" << start << endl;
cout << "value = " << start->getValue() << endl;
cout << "Next = " << start->getNext() << endl;
start = start->getNext();






;


int main()

LinkedList newLink;
newLink.createLinklist();
newLink.display();





3 Answers
3



Problem



In


LinkedList()
Node* head = NULL;
Node* tail = NULL;



Node* head = NULL; tells the compiler to make a new Automatic variable named head that is a pointer to a Node and set this new variable to NULL. This new head shadows LinkedList::head, replacing it for the rest of the constructor. The result is head a variable that only exists within the body of the constructor, gets the initialization (assignment really) intended for LinkedList::head.


Node* head = NULL;


head


Node


NULL


head


LinkedList::head


head


LinkedList::head



That means when you get to


if(head == NULL)



in createLinklist, LinkedList::head probably isn't NULL and instead points off into the wild blue yonder, so the program executes


createLinklist


LinkedList::head


NULL


insertAtEnd(node,tail);



and LinkedList::tail suffered the same fate as LinkedList::head and likely points somewhere you cannot safely write. The program probably crashes at this point, but it could overwrite something else important and cause the program to crash later, hiding the true location of the bug.


LinkedList::tail


LinkedList::head



Solution


LinkedList()
head = NULL;
tail = NULL;



assigns NULL toheadandtail`. A more idiomatic approach is to use the Member Initializer List


NULL to


and


LinkedList(): head(NULL), tail(NULL)

// does nothing.



Sidenote



a good compiler with the warning level turned up will warn you that


Node* head = NULL;



doesn't do anything useful. Never ignore compiler warnings. A compiler warning means that while your program may be syntactically correct, it probably doesn't do what you want it to do. Warnings are your first line of defense against logic errors. Always try to understand and resolve what the compiler is telling you. It may save you hours of debugging later.





Thank you soo much sir. You are awesome!
– Zahin Zaman
Aug 31 at 3:44





Koodos you you as well, @ZahinZaman . I'd passed on the question until I realized this was the only bug in the program. Most of the time weeding through a Linked List question on stack overflow is a painful experience of un-or-incorrectly set pointers and incoherent logic. It's a pleasure to see someone get it right.
– user4581301
Aug 31 at 3:46



Issue is your constructor:


class LinkedList{
private:
Node* head;
Node* tail;

public:

LinkedList()
Node* head = NULL;
Node* tail = NULL;



In the constructor, you are declaring two LOCAL variables to NULL rather than the class ones. This means that the class ones point to wherever, but in all likelihood are not NULL.



Suggestions: Learn C++11 or later.



Basically C++11 allows you to do this:


class LinkedList{
private:
Node* head = nullptr;
Node* tail = nullptr;



and you won't need a constructor in your case, though it is good practice to add:


LinkedList() = default;



if you want to use the default one.





This is a good answer, I feel it could benefit from mentioning why if(head == NULL) isn't passing, which is ultimately causing OP's problems
– Tas
Aug 31 at 3:49


if(head == NULL)





Really good answer. I passed on bringing C++ 11 into the discussion to keep the amount of new information to a minimum, but it's great to see you managed to include it without a massive infodump.
– user4581301
Aug 31 at 3:50





A note on the new addition: While unique_ptrs are inappropriate for both next and prev, there is a case to be made for unique_pr for next and a raw pointer for prev or a combination of shared_ptr and weak_ptr. A good talk on the subject: youtube.com/watch?v=JfmTagWcqoE . Personally I don't like smart pointers in linked lists because they make, my opinion, the nodes too smart. I feel that managing the nodes should be the list's job. Also destroying a sufficiently large list can overflow the stack as the destruction propagates.
– user4581301
Aug 31 at 3:58


unique_ptr


next


prev


unique_pr


next


prev


shared_ptr


weak_ptr





@user4581301 -- Personally I don't like smart pointers in linked lists -- I really don't like them when implementing low-level data structures. For example, how many implementations of std::vector uses smart pointers as the pointer type to the internal buffer?
– PaulMcKenzie
Aug 31 at 5:29


std::vector



change your code to


LinkedList()
head = NULL;
tail = NULL;



because you have already defined head and tail.


head


tail





This doesn't add anything that the already great answer hasn't mentioned.
– Tas
Aug 31 at 3:44



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Some of your past answers have not been well-received, and you're in danger of being blocked from answering.



Please pay close attention to the following guidance:



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế