Why does my linked-list remove function not work? - c

My C/C++ linked-list remove function does not remove an element from the list. Here follows some of my code;
struct listIntElement {
struct listIntElement *next;
int data;
};
typedef struct listIntElement ListIntElement;
ListIntElement *head = NULL;
/*
Inserts a new element infront of the list.
*/
bool insert(ListIntElement **head, int data) {
// Allocate memory for new element. The cast is needed here as we are using a C++ compiler.
ListIntElement *newElement = (ListIntElement *) malloc(sizeof(ListIntElement));
// Check if memory allocation was succesfull.
if (newElement == NULL)
return false;
// Set the data for the new element of the list.
newElement->data = data;
// Keep track of the new head of the list.
newElement->next = *head;
*head = newElement;
return true;
}
/*
Deleting an element in the list.
*/
bool remove(ListIntElement **head, ListIntElement *elementToDelete) {
ListIntElement *element = *head;
// Check for NULL pointers.
if (head == NULL || *head == NULL || elementToDelete == NULL)
return false;
// Special case for the head.
if (elementToDelete == *head) {
*head = element->next;
free(elementToDelete);
return true;
}
// Traversal of the list to find the element to remove.
while (element != NULL) {
if (element->next == elementToDelete) {
// Relink the list so that it does not include the element to be deleted.
element->next = elementToDelete->next;
free(elementToDelete);
return true;
}
element = element->next;
}
// elementToDelete was not found.
return false;
}
/*
Finding an element in the list.
*/
ListIntElement find(ListIntElement **head, int data) {
// Take care of the head as we don't want to use the head
// in the traversal operation.
ListIntElement *element = *head;
while (element != NULL && element->data != data) {
element = element->next;
}
return *element;
}
/*
Displaying the list.
*/
void displayList(ListIntElement **head) {
ListIntElement *element = *head;
// Check if list is empty.
if (head == NULL | *head == NULL) {
printf("List is empty\n");
}
while (element != NULL) {
printf("%d --> ", element->data);
element = element->next;
}
printf("NULL");
printf("\n");
}
Here is my test code;
/*
* Testing a linked list.
*/
ListIntElement found;
printf("Linked list test\n");
insert(&head,0);
insert(&head, 1);
insert(&head, 2);
insert(&head, 3);
insert(&head, 4);
insert(&head, 5);
displayList(&head);
printf("size is: %d\n", size(&head));
found = find(&head, 5);
printf("This was found: %d\n", found.data);
remove(&head,&found);
displayList(&head);
I have found this section to be the section where things go wrong in the remove function;
// Special case for the head.
if (elementToDelete == *head) {
*head = element->next;
free(elementToDelete);
return true;
}
Take notice that I am using MS Visual Studio 2015 to write C code and using a C++ compiler.

From find function, you were returning a copy, and not the address itself, so your changes were not reflected in the calling function. Fixed it.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define true 1
#define false 0
struct listIntElement {
struct listIntElement *next;
int data;
};
typedef struct listIntElement ListIntElement;
ListIntElement *head = NULL;
/*
Inserts a new element infront of the list.
*/
bool insert(ListIntElement **head, int data) {
// Allocate memory for new element. The cast is needed here as we are using a C++ compiler.
ListIntElement *newElement = (ListIntElement *) malloc(sizeof(ListIntElement));
// Check if memory allocation was succesfull.
if (newElement == NULL)
return false;
// Set the data for the new element of the list.
newElement->data = data;
// Keep track of the new head of the list.
newElement->next = *head;
*head = newElement;
return true;
}
/*
Deleting an element in the list.
*/
bool removeElement (ListIntElement **head, ListIntElement *elementToDelete) {
ListIntElement *element = *head;
// Check for NULL pointers.
if (head == NULL || *head == NULL || elementToDelete == NULL)
return false;
// Special case for the head.
if (elementToDelete == *head) {
*head = element->next;
free(elementToDelete);
return true;
}
// Traversal of the list to find the element to remove.
while (element != NULL) {
if (element->next == elementToDelete) {
// Relink the list so that it does not include the element to be deleted.
element->next = elementToDelete->next;
free(elementToDelete);
return true;
}
element = element->next;
}
// elementToDelete was not found.
return false;
}
/*
Finding an element in the list.
*/
ListIntElement *find(ListIntElement **head, int data) {
// Take care of the head as we don't want to use the head
// in the traversal operation.
ListIntElement *element = *head;
while (element != NULL && element->data != data) {
element = element->next;
}
return element;
}
/*
Displaying the list.
*/
void displayList(ListIntElement **head) {
ListIntElement *element = *head;
// Check if list is empty.
if (head == NULL | *head == NULL) {
printf("List is empty\n");
}
while (element != NULL) {
printf("%d --> ", element->data);
element = element->next;
}
printf("NULL");
printf("\n");
}
int main () {
ListIntElement *found;
printf("Linked list test\n");
insert(&head,0);
insert(&head, 1);
insert(&head, 2);
insert(&head, 3);
insert(&head, 4);
insert(&head, 5);
displayList(&head);
printf("size is: %d\n", sizeof(&head));
found = find(&head, 5);
printf("This was found: %d\n", found->data);
removeElement(&head,found);
displayList(&head);
}
Output:
Linked list test
5 --> 4 --> 3 --> 2 --> 1 --> 0 --> NULL
size is: 4
This was found: 5
4 --> 3 --> 2 --> 1 --> 0 --> NULL

Related

C How to insert a new node after an existing node in a Linked List?

int add_after(ITEM *list, ITEM *c_item, int value)
{
//create a new node
ITEM *elem = malloc(sizeof(ITEM));
elem->value = value;
//if head is NULL, it is an empty list
if (elem == NULL || c_item == NULL || list == NULL) {
return -1;
} else {
while (list != NULL) {
if (list == c_item) {
elem->next = list->next;
elem->value = value;
list->next = elem;
list = list->next;
}
list = list->next;
}
}
return 0;
}
I am trying to make a function the puts a node after an existing node. my problem is inside the else statement. I am trying to find a node list equal to node c_item. After finding the equal, it should enter elem in between them.
I would rewrite your function as follow:
int add_after(ITEM *list, ITEM *c_item, int value)
{
//create a new node
ITEM *elem;
//if head is NULL, it is an empty list
if (!list || !c_item) {
return -1;
}
/* Create a new node. */
elem = malloc(sizeof(ITEM));
if (!elem) {
return -1;
}
/* You should rather set the value after having checked the allocated
* memory is not NULL. */
elem->value = value;
/* Insert elem ahead of the current item. */
elem->next = c_item->next;
c_item->next = elem;
return 0;
}
If you wish to insert a new node after a given node, you do not need to iterate on the list, just insert the new node as the next node of the c_item node:
int add_after(ITEM *list, ITEM *c_item, int value) {
//check function arguments
if (list == NULL || c_item == NULL)
return -1;
//create a new node
ITEM *elem = malloc(sizeof(ITEM));
if (elem == NULL)
return -1;
elem->value = value;
elem->next = c_item->next;
c_item->next = elem;
return 0;
}
In your approach, you try and locate the element in the list, which might be necessary if the caller cannot be trusted. In this case, you should have a different return value whether the element was inserted or not and free the allocated elem if c_item was not found:
int add_after(ITEM *list, ITEM *c_item, int value) {
//create a new node
ITEM *elem = malloc(sizeof(ITEM));
//if head is NULL, it is an empty list
if (elem == NULL || c_item == NULL || list == NULL) {
return -1;
} else {
while (list != NULL) {
if (list == c_item) {
elem->value = value;
elem->next = list->next;
list->next = elem;
return 1; // element was inserted
}
list = list->next;
}
free(elem);
return 0; // c_item was not found, elem not inserted
}
}

Infinite loop when trying to add to the beginning of a linked list in C

I'm trying to practice Linked Lists in C and I created a simple LL with an add function that takes a position index and inserts the data at that position.
I keep getting an infinite loop when trying to add to the beginning of my list using the add_beg, which just calls the add_at function with position 0 (add(0, data)) function. I can't seem to find the reason why this is happening. I need another set of eyes. Here is the code:
#include <stdio.h>
#include <stdlib.h>
typedef struct node_t {
int _data;
struct node_t *_next;
} node_t;
node_t *_head = NULL;
void add_at(int pos, int data) {
node_t *node = malloc(1 * sizeof(node_t));
node->_data = data;
node->_next = NULL;
// insert if empty
if (_head == NULL) {
_head = node;
}
else {
int index = 0;
node_t *prev = NULL;
node_t *curr = _head;
while (curr != NULL && index != pos) {
prev = curr;
curr = curr->_next;
index++;
}
// insert beginning
if (index == 0) {
_head = node;
node->_next = _head;
}
// insert end
else if (index != 0 && curr == NULL) {
prev->_next = node;
}
// insert middle
else {
prev->_next = node;
node->_next = curr;
}
}
}
void add_beg(int data) {
add_at(0, data);
}
void add_end(int data) {
add_at(-1, data);
}
void dump() {
if (_head != NULL) {
node_t *curr = _head;
while (curr != NULL) {
if (curr->_next != NULL) {
printf("%d -> ", curr->_data);
}
else {
printf("%d", curr->_data);
}
curr = curr->_next;
}
printf("\n");
}
else {
printf("The list is empty\n");
}
}
int main() {
add_beg(6);
add_at(1, 1);
add_at(2, 3);
add_at(3, 9);
add_at(4, 8);
add_at(5, 5);
add_at(7, 2);
add_at(8, 4);
add_beg(9); // commenting this out prevents the infinite loop
dump();
return 0;
}
The error is in this section:
if (index == 0) {
_head = node;
node->_next = _head;
}
That second command sets node->_next to head. However, the line before set _head to node. Therefore, you just set node->_next to node, creating the infinite loop.
You need to reverse the order of those two statements.
if (index == 0) {
_head = node;
node->_next = _head;
}
here you set _head as node but after next node at _head then when your doing while loop you had already broke your linked list.
Fix :
first set the _head as node->_next then set _head as node

From struct, pointer to NULL, and back

typedef struct Element
{
int number;
struct Element *right;
struct Element *left;
} Element;
Element *newElement;
Element *head;
Element *pos;
Element *current;
void insert_Element()
{
newElement = malloc(sizeof(Element));
if (head==NULL)
{
head = newElement;
current = newElement;
current->right = pos;
//pos->left = current;
}
}
The head of the double-linked-list should have a pointer to the next (right) element (which is NULL, but also pos). This works fine, but the pointer back(left) from pos/NULL doesn't work.
What did I do work; Is it even possible?
Thank you in advance :)
The present version of the insert_Element function does not work unless head is NULL and pos is not NULL, and it does not set the left member of the new element. The purpose of the pos variable has not been stated, but appears to be used to point to the element before which the new element is to be inserted. We could use pos == NULL to indicate that the new element is to be placed at the end of the list. Here is a version that does that:
void insert_Element(void)
{
newElement = malloc(sizeof(Element));
if (newElement == NULL)
{
return;
}
newElement->right = pos;
if (pos != NULL)
{
/* insert before pos */
newElement->left = pos->left;
pos->left = newElement;
}
else if (head != NULL)
{
/* add to end of list */
current = head;
while (current->right != NULL)
{
current = current->right;
}
newElement->left = current;
current->right = newElement;
}
else
{
/* add to empty list */
newElement->left = NULL;
}
if (pos == head)
{
/* make it the start of the list */
head = newElement;
}
/* make it the 'current' element */
current = newElement;
}
It is better not to use global variables so much and uses function parameters instead. Here is a version that uses no global variables and returns a pointer to the new element on the list (or NULL on allocation failure):
Element *insert_Element(Element **phead, Element *pos)
{
Element *newElement = malloc(sizeof(Element));
if (newElement == NULL)
{
return NULL;
}
newElement->right = pos;
if (pos != NULL)
{
/* insert before pos */
newElement->left = pos->left;
pos->left = newElement;
}
else if (*phead != NULL)
{
/* add to end of list */
Element *current = *phead;
while (current->right != NULL)
{
current = current->right;
}
newElement->left = current;
current->right = newElement;
}
else
{
/* add to empty list */
newElement->left = NULL;
}
if (pos == *phead)
{
/* make it the start of the list */
*phead = newElement;
}
return newElement;
}
It's possible you just need to allocate you Element *pos. Actually pos is equal to NULL and logically don't have pointer left or right, it's like looking for the toilet in a house that doesn't exist.

Not getting the changed linked list after changing

I made a linked-list and returned by reference, for some reason after sending it to another function it does not getting back with changes unless I send it again by reference, any explanation why?
if I just send "lst" to the function as lst without the "&" when it comes back from the function I only get an odd list,When I used the debugger inside the function I saw the list the with the even values(which should be also in the main),as I think it should be like that since lst is the base address like an array,or am I wrong?
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int data;
struct node *next;
}node;
node* get_lst(node **linked_lst);
node *createList2(node** L);
void main() {
node *odd_node = NULL, *lst = NULL;
get_lst(&lst);//get values for the original list
odd_node = createList2(&lst);//get an odd list
getch();
}
node *createList2(node **L)
{
node * tList = *L, odd_lst;
node *temp_odd = &odd_lst;
temp_odd->next = NULL;
//first element in the list is odd
while (tList != NULL && tList->data % 2)
{
temp_odd->next = tList;
tList = tList->next;
*L = tList;
temp_odd = temp_odd->next;
temp_odd->next = NULL;
}
//first element in the list is even
while (tList != NULL)
{
if (tList->next != NULL && tList->next->data % 2)
{
temp_odd->next = tList->next;
tList->next = tList->next->next;
temp_odd = temp_odd->next;
temp_odd->next = NULL;
}
else
tList = tList->next;
}
return odd_lst.next;
}
node* get_lst(node **linked_lst) {
int value;
node *curr = NULL;
node *new_node;
do
{
printf("enter value:");
scanf_s("%d", &value);
if (value <0) {
return NULL;
}
new_node = (node*)malloc(sizeof(node));
new_node->data = value;
new_node->next = NULL;
if (*linked_lst == NULL)
{
*linked_lst = new_node;
curr = *linked_lst;
}
else
{
curr->next = new_node;
curr = curr->next;
}
} while (value >= 0);
return *linked_lst;
}

Pointer in function to linked list not updating element in 1 case

I am using a struct like this
struct infoM {
char* direction;
int key;
};
typedef struct nodeM{
struct infoM nodeInfo;
struct nodeM *next;
struct nodeM *prev;
} node;
typedef node list;
I have one function that returns the wanted node by a specific field
node * search(list *l, char* direction) {}
And this is my function to remove elements from the list
int delete(list *l, char* direction) {
node *tmp = search(l, direction);
if (tmp != NULL) {
node *ant = tmp->prev;
node *seg = tmp->next;
if (seg != NULL) {
if (ant != NULL) {
ant->next = seg;
seg->prev = ant;
free(tmp);
return 1;
} else { //prev null
seg->prev = NULL;
*l = *seg;
tmp = NULL;
free(tmp);
return 1;
}
} else { //next null
if (ant == NULL) {
l->nodeInfo.key = somevalue;
l->next = NULL;
l->prev = NULL;
return 1;
} else {
printf("Here is the problem\n");
ant->next = NULL;
free(tmp);
return 1;
}
}
} else { //tmp nulo
perror("Error delete : node null\n");
return 0;
}
}
If I have 4 elements in the list, 1234 and I delete first first element everything is okay and returns 234. If I delete the last element it returns 23 seems to work great. But if I try to delete the last element now the function does nothing despite being the same case that when it is 234 and I don't understand why. The list is not being updated.
In the main I am using the list like this :
list a;
delete(&a, "whatever");
What am I doing wrong ?
This is the code for search
node * createnode(){
node *tmp = (node *) malloc (sizeof(node));
return tmp;
}
node * search(list *l, char* direction) {
node *tmp = createnode();
if (l->nodeInfo.key == 777) {
perror("Error search: empty list\n");
return NULL;
}
tmp=l;
while((strcmp(direction, tmp->nodeInfo.direction) !=0) && (tmp->next != NULL)) {
tmp = tmp->next;
}
if (strcmp(direction, tmp->nodeInfo.direction) == 0) {
return tmp;
} else {
perror("Error search: element not found\n");
return NULL;
}
}

Resources