Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I define my Node as a struct and trying to get it. But when I run my code compiler says that "head", "tail" was not declared. Am I define it wrongly?
Tried to use template but it didn't work;
You are having head and tail declared inside class List as it's private member fields. But you are accessing them from methods outside the class. The compiler is throwing errors because of that.
In the insertFront function, you have coded
head = node
node->next = head;
This is wrong. head is the only access you have to the remaining linked list. By doing head = node, you lose access to the rest of the list. and doing node->next = head after that essentially points the node to itself. It should be done in reverse.
node->next = head;
head = node;
Another mistake in the insertBack function. prev is not declared in struct Node. The right way to insert at tail is
tail->next = node;
tail = node;
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I am trying to create a singly linked list by inserting nodes at end, and despite having no errors I am unable to print my linked list. Please help me debug my code.
I tried online compiler on codechef and it shows SIGSEGV Runtime error. What is this supposed to mean?
struct node
{
int data;
struct node *next;
};
void insert(struct node *root,int data)
{
struct node *temp=new(struct node);
if(root==NULL)
{
temp->data=data;
temp->next=NULL;
}
root->next=temp;
temp->data=data;
temp->next=NULL;
}
void print(struct node *root)
{
struct node *temp;
temp=root;
while(temp!=NULL)
{
cout<<temp->data;
temp=temp->next;
}
}
int main()
{
struct node *root=NULL;
insert(root,1);
insert(root,2);
insert(root,3);
insert(root,4);
print(root);
return 0;
}
Please help me debug my code.
OK lets try a dry run.
Imagine your list is empty and you are inserting your first item. So root equals NULL and we call insert.
1) first thing
struct node *temp=new(struct node);
You allocate a new node, and set temp equal to it, so far so good.
2) next thing
if(root==NULL)
this is true as explained in the preamble, so we enter the if statement
3) next thing
temp->data=data;
temp->next=NULL;
these statements in the if body get executed and initialise the newly allocated object. It's not clear why you only want to do this when root == NULL, I would think you would want to initialise the newly allocated node always. But anyway, so far no errors.
4) next thing
root->next=temp;
Now here's the error. Ask yourself, what is the value of root at this point? When we started it was NULL, has anything changed it since? The answer of course is no, so you are dereferencing a NULL pointer. That explains the error.
You need to be able to look at the code you've written and see it for what it really does. The ability to dry run your code like I did above is a very valuable skill to have.
Unfortunately your code really is not very close to being correct. So I think the best thing would be to look at some working code and see how it operates and then start again.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
Here is the snippet of code that confuses me :
T* pop(){
if(head == 0)
return 0;
T* result = head->data;
Link* oldHead = head;
head = head->next;
delete oldHead;
return result;
}
The result pointer is maked to point to the "data field" of head's object. After that, head is deleted. Does not result have to point to null or a blank space since the data field is deleted together with head ?
Does not result have to point to null or a blank space since the data field is deleted together with head?
When you execute
delete oldHead;
the memory for oldHead is deallocated. However, unless you also delete data in the destructor of Link, data continues to be a valid pointer. Hence, it's ok to return result and continue to use the return value in the calling function.
If you have
Link::~Link() {}
there is no problem.
However, if you have
Link::~Link() { delete data; }
there is a problem.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
Context: Create a circular, doubly linked list from a file (input.txt), containing a list of names. The number of names is unknown.
Prompt the user for the number of nodes to delete and then delete accordingly from the list.
Assumption: The number inputted by the user will not exceed the actual number of nodes in the list.
Question: My delete node function is not working, as when I go to display what is in the node that was supposedly deleted, the data is still there. Any Help?
My code: http://www.cplusplus.com/forum/general/210015/
(I am linking to this other website because it was easier to format there.)
My delete node function is not working, as when I go to display what is in the node that was supposedly deleted, the data is still there.
Accessing a destroyed (deleted) object has undefined behaviour.
You cannot expect how the program behaves when the behaviour is undefined. Therefore your expectation that "data shouldn't be there" was ill-founded. A correct expectation is that data might, or might not be there, and that the program might or might not crash and that daemons might or might not fly out of the users nose.
Your code for void deleteNode(struct node *&head) has a lot of bugs.
you don't check if head is nullptr
you don't check if current->next and/or current->prev are nullptr
accessing nullptr pointers is undefined behaviour, that may mean a crash or unexpected results or whatever.
Try using following delete function to delete the node.
Note: assuming delete starts from the head.
void deleteNode(struct node *&head,int deleteCount)
{
if(head != NULL){
struct node *current=head; //copy of head.
struct node *temp,*headPrev,*headNext;
headPrev = head->prev;
headNext = head->next;
int count = 0;
while(count++ < deleteCount)
{
//add code to free current node from me
temp = current; //get rid of this node
currrent = current->next; //skip all the nodes you want delete
}
current->prev = headPrev;
headPrev->next = current;
head = current;
}
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
so i want to Write code to implement the replaceNode(…) function that takes pointers to OldNode and
NewNode as inputs using the function header given below. Also include code for the special
cases where OldNode is the head or tail nodes of the list. Assume the list is never empty
before this function is called and that all nodes have been created dynamically
void List::replaceNode(Node *OldNode, Node *NewNode)
{
//write code here
}
i have no idea how to go about doing this can some one please help me with this
CASE 1: where OldNode is neither HEAD or TAIL (Singly LinkedList)
You need to know about the node pointing the OldNode, lets call it *PrevPntr & the node which oldNode is pointing to.
Then it will be something like:
`PrevPntr->next = NewNode;
NewNode->next = OldNode->next
else
just replace the DATA of OldNode by data of NewNode (some constraints will be there in this case).
CASE 2: where OldNode is HEAD (Singly LinkedList).
Then it will be something like:
`
NewNode->next = OldNode->next
CASE 3: where OldNode is TAIL (Singly LinkedList).
You need to know about the node pointing the OldNode, lets call it *PrevPntr.
Then it will be something like:
`PrevPntr->next = NewNode;
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am currently working on a hash table project for school and i have run into an issue that i cant figure out. My professor provided us with classes that have functions that we need to implement and these functions make use of templates.
Anyway, in my insert function, i am running into an issue with setting a value of a node in the singly-linked list structure i am using to implement the hash table.
My problem is this:
void insert(U item1, U item2){ //For my project U is a string
Node<U>* temp = headPtr;
cout << item1 << endl; //Will print out the string no problem
//Assignment attempt
temp->SSN = item1; // causes a seg fault
temp->name = item2;
temp->next = NULL;
if(headPtr->next == NULL){
headPtr->next = temp;
size++;
}
else{
Node<U>* temp2 = headPtr;
while(temp2->next != NULL){
temp2 = temp2->next;
}
temp2->next = temp;
size++;
}
}
And it is quite frustrating because in previous assignments i have been able to use this insert function properly, the only reason it does not work i have concluded is because i must be missing something with templates that i have overlooked.
Also here is my node.h file:
#include <iostream>
using namespace std;
template <class T>
struct Node{
T SSN;
T name;
Node<T>* next;
};
I am trying to assign a string value to what SHOULD be a string value and should work to as far as my understanding goes but every time i run the program it gets to this point and there is just segment fault 11.
You have to replace
Node<U>* temp = headPtr;
with
Node<U>* temp = new Node<U>;