I don't know why the result of subit is "member access within null pointer of type 'struct ListNode'". I tested the sample and the capital was correct, I don't know what is wrong with my cognition of the empty indicators, can someone help me?
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* deleteDuplicates(struct ListNode* head){
struct ListNode* node = head;
while(node->next != NULL && node != NULL){
if(node->val == node->next->val){
struct ListNode* duplicate = node->next;
node->next = node->next->next;
free(duplicate);
}
else node = node->next;
}
return head;
}
If the list is empty head will be NULL. In the while loop expression node is dereferenced using node->next which is evaluated before the right hand side, which checks node for NULL. You should check to see if node is NULL before evaluating the while loop and NULL checks for node should always be performed before NULL checks for node->next.
This would be my suggestion:
struct ListNode* deleteDuplicates(struct ListNode* head){
struct ListNode* node = head;
while(node != NULL){
if(node->next == NULL)
{
node = node->next;
}
else if(node->val == node->next->val){
struct ListNode* duplicate = node->next;
node->next = node->next->next;
free(duplicate);
}
else node = node->next;
}
return head;
}
Related
Im trying to insert a node at the head of a linked list, and I'm not exactly sure why this function isn't working. I thought it was a fairly simple task, but I seem to be missing something here. I've also included my structs and a portion of main so you can get a clearer understanding of the code. Thanks
typedef struct node
{
struct node *next;
int data;
} node;
typedef struct LinkedList
{
node *head;
node *tail;
} LinkedList;
LinkedList *create_list(void)
{
return calloc(1, sizeof(LinkedList));
}
node *create_node(int data)
{
node *ptr = calloc(1, sizeof(node));
ptr->data = data;
return ptr;
}
void head_insert(LinkedList *list, int data) // problem
{
node *newHead = create_node(data);
newHead->next = list->head;
}
void print_list_helper(node *head)
{
if (head == NULL)
return;
printf("%d%c", head->data, (head->next == NULL) ? '\n' : ' ');
print_list_helper(head->next);
}
void print_list(LinkedList *list)
{
if (list == NULL || list->head == NULL)
return;
print_list_helper(list->head);
}
int main(void)
{
LinkedList *list = create_list();
head_insert(list, 8);
print_list(list); // print linked list function
return 0;
}
So I created a new Node, and set node->next to the head of the list. Im not sure what else im missing here. I have another function that prints the lists, thats why the function is void.
Add these lines at the end of your head_insert() function definition:
if (list->head == NULL)
{
list->tail = newHead;
}
list->head = newHead;
In your function, after adding new node at head the struct LinkedList still pointed to the previous head. You should change that head to the newly inserted head. And if there was no nodes in the list you should also set the newly created head
Here is the full function.
void head_insert(LinkedList *list, int data)
{
node *newHead = create_node(data);
newHead->next = list->head;
if (list->head == NULL)
{
list->tail = newHead;
}
list->head = newHead;
}
I'm fairly new to C and I am trying to create a function to reverse a linked list, passing only the List itself as a parameter. Is this possible to do without passing a node as a parameter?
Here is my code so far, I know it does not work correctly because I cannot figure out how to make the recursive call on the rest of the list.
void reverse(LL_t *L) {
if (L->head->next == NULL) {
return;
}
node_t *rest = L->head->next;
reverse(rest);
node_t *q = rest->next;
q->next = rest;
rest->next = NULL;
}
As well here are my type definitions.
typedef struct {
node_t *head;
node_t *tail;
} LL_t;
typedef struct _node {
int data;
struct _node *next;
} node_t;
You can reverse the list with a simple loop, recursion is not needed and given your API, not appropriate.
Here is a modified version of your function:
void reverse(LL_t *L) {
node_t *prev = NULL;
node_t *curr = L->head;
L->tail = curr;
while (curr != NULL) {
node_t *next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
L->head = prev;
}
If you are required to use recursion, you can test if the list is empty or limited to a singleton and do nothing, otherwise remove the head element, reverse the resulting list and append the element to the end:
void reverse(LL_t *L) {
if (L->head != L->tail) {
/* at least 2 elements */
node_t *node = L->head;
L->head = node->next;
node->next = NULL;
reverse(L);
L->tail = L->tail->next = node;
}
}
Note that this recursive approach may have undefined behavior if the list is too long as reverse will recurse too many times and cause a stack overflow.
struct node {
int value;
struct node* next;
};
Non-recursive definition operating in constant stack space:
void reverse(struct node** ptr) {
struct node* prev_ptr;
struct node* node_ptr;
if (prev_ptr = * ptr) {
node_ptr = prev_ptr -> next;
prev_ptr -> next = NULL;
while (node_ptr) {
struct node* temp = node_ptr -> next;
node_ptr -> next = prev_ptr;
prev_ptr = node_ptr;
node_ptr = temp;
}
* ptr = prev_ptr;
}
}
Extensionally equivalent recursive definition:
void reverse(struct node** ptr) {
struct node* node_ptr;
if (node_ptr = * ptr) {
node_ptr -> next = NULL;
* ptr = reverse_rec(node_ptr, node_ptr -> next);
}
}
struct node* reverse_rec(struct node* prev_ptr, struct node* node_ptr) {
if (! node_ptr) { return prev_ptr; }
struct node* temp = reverse_rec(node_ptr, node_ptr -> next);
node_ptr -> next = prev_ptr;
return temp;
}
This works, but using recursion to reverse a list requires O(n) stack space overhead. The concept here is to advance the static instance of L->head, while keeping a local copy of head for each level of recursion. Recursion continues until the end of the list is reached, then the list is reversed using the local instances of head as reverse() returns backup the call chain.
void reverse(LL_t *L)
{
node_t *head;
if(L->head == NULL || L->head->next == NULL)
return;
head = L->head;
L->head = head->next;
reverse(L);
head->next->next = head; // reverse the nodes
head->next = NULL;
L->tail = head; // ends up setting tail to what was 1st node
}
//A simple program to reverse a Linked List
void reverse(struct node* head_ref)
{
struct node* first;
struct node* rest;
if (head_ref == NULL)
return;
first = head_ref;
rest = first->next;
if (rest == NULL)
return;
reverse(rest);
first->next->next = first;
first->next = NULL;
head_ref = rest;
}
Suppose we have doubly linked list of nodes
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int value;
struct Node* next;
struct Node* prev;
} Node;
typedef struct LinkedList {
Node *first;
Node *last;
} LinkedList;
void initList(LinkedList* l) {
l->first = NULL;
l->last = NULL;
}
and I have to code method, which inserts a new node with given value to the end of the list and returns a pointer to the new node. My attempt follows:
Node *insert(LinkedList *list, int value) {
Node node;
node.value = value;
node.prev = list->last;
node.next = NULL;
if (list->last != NULL){
(list->last)->next = &node;
}else{
list->first = &node;
list->last = &node;
}
return &node;
}
It seems, that insertion in the empty list works, but it doesn't for a non-empty one.
(There are implementation tests, which tell me if an insertion was successful or not. I can post the codes of them, but don't think it's important).
So please, where are the mistakes?
There is a warning in the log (the 51st line is that with 'return &node')
C:\...\main.c|51|warning: function returns address of local variable [-Wreturn-local-addr]|
Is that serious problem? And how to remove it?
Thank you for the answers, but I think there is still a problem with non-empty lists, because according to the test, this fails:
void test_insert_nonempty(){
printf("Test 2: ");
LinkedList l;
initList(&l);
Node n;
n.value = 1;
n.next = NULL;
l.first = &n;
l.last = &n;
insert(&l, 2);
if (l.last == NULL) {
printf("FAIL\n");
return;
}
if ((l.last->value == 2) && (l.last->prev != NULL)) {
printf("OK\n");
free(l.last);
}else{
printf("FAIL\n");
}
}
Node node; is a local variable in your function insert. It is "destroyed" as soon as your function terminates and is not longer defined. Returning a pointer to local variable of a function is undefined behavior. You have to allocate dynamic memory. Dynamically allocate memory is reserved until you free it:
Node *insert(LinkedList *list, int value) {
Node *node = malloc( sizeof( Node ) ); // allocate dynamic memory for one node
if ( node == NULL )
return NULL; // faild to allocate dynamic memory
node->value = value;
node->prev = list->last;
node->next = NULL;
if ( list->first == NULL )
list->first = node; // new node is haed of list if list is empty
else // if ( list->last != NULL ) // if list->first != NULL then list->last != NULL
list->last->next = node; // successor of last node is new node
list->last = node; // tail of list is new node
return node;
}
Note to avoid memory leaks you have to free each node of the list, when you destroy the list.
You are returning address of non-static local variable which will vanish on returning from function, and dereferencing the address after returning from the function invokes undefined behavior.
You have to allocate some buffer and return its address.
Node *insert(LinkedList *list, int value) {
Node *node = malloc(sizeof(Node));
if (node == NULL) return NULL;
node->value = value;
node->prev = list->last;
node->next = NULL;
if (list->last != NULL){
(list->last)->next = node;
}else{
list->first = node;
list->last = node;
}
return node;
}
You have to allocate the new node dynamically.
Otherwise variable node in your function
Node *insert(LinkedList *list, int value) {
Node node;
//...
is a local variable of the function that will not be alive after exiting the function. As result any pointer to the variable used to access it will be invalid.
The function can look like
Node * insert( LinkedList *list, int value )
{
Node *node = malloc( sizeof( Node ) );
if ( node != NULL )
{
node->value = value;
node->prev = list->last;
node->next = NULL;
if ( list->last != NULL )
{
list->last->next = node;
}
else
{
list->first = node;
}
list->last = node;
}
return node;
}
For some reason, my code keeps crashing.
If anyone can help me find out why, I would be very thankful.
int destroy(struct node *p)
{
struct node * temp = p;
struct node * next;
while (temp->next != NULL)
{
next = temp->next;
free(temp);
temp = next;
}
p = NULL;
return 1;
}
You need to test temp for null-ness, not temp->next:
void destroy(struct node *p)
{
struct node *temp = p;
while (temp != NULL)
{
struct node *next = temp->next;
free(temp);
temp = next;
}
}
You also don't need to set p to null (it doesn't do anything useful). And returning a status is not a good idea. Your callers either have to test it (but will never see anything other than 1, so the test is pointless), or they have to ignore it (in which case, why bother to return it?). You could do without the variable temp, too:
void destroy(struct node *list)
{
while (list != NULL)
{
struct node *next = list->next;
free(list);
list = next;
}
}
If you really want to set the pointer to null, you have to change the notation:
void destroy(struct node **list)
{
struct node *node = *list;
while (node != NULL)
{
struct node *next = node->next;
free(node);
node = next;
}
*list = NULL;
}
and instead of:
struct node *root = ...;
...
destroy(root);
you would have to write:
struct node *root = ...;
...
destroy(&root);
This is my delete node function, can anyone help me in seeing what is wrong with it? I seem to be having a lot of trouble as I do not understand some parts of it.
How is the pointer to the head suppose to be used? struct lnode** head, I know this is a pointer to the head pointer. However I am not sure if I am using it correctly.
The second part of the code, I am not sure if I am correctly connecting the two nodes after deleting the one that the function calls.
void deleteNode (struct lnode** head, struct lnode* node) {
if((*head) == node){
struct lnode* temp = (*head) -> next;
free(node);
free(node->word);
*head = temp;
}
else{
struct lnode *nextNode = node->next;
struct lnode *prevNode;
while((*head) != NULL){
if((*head)->next = node){
prevNode = *head;
break;
}
else
*head = (*head) -> next;
}
prevNode->next = nextNode;
free(node);
free(node->word);
}
}
Please try with this modified program. In the original program, there were the following issues.
If the node to be deleted matched with head, then you were freeing the node twice. This will give an exception as you are trying to free a memory which already has been freed.
The comparison in if condition required to be if((*head)->next == node) instead of if((*head)->next = node)
The order of freeing memory is important. Hence node->word should be freed prior to freeing of node.
void deleteNode (struct lnode** head, struct lnode* node) {
struct lnode *prevNode = *head;
if((*head) == node){
struct lnode* temp = (*head) -> next;
*head = temp;
}
else{
struct lnode *nextNode = node->next;
while((*head) != NULL){
if((*head)->next == node){
prevNode = *head;
break;
}
else
*head = (*head) -> next;
}
prevNode->next = nextNode;
free(node->word);
free(node);
}
}