I am trying to create a program that inserts numbers into a linked list in ascending order. This is my insert function. It works for inserting some numbers but not others. I think it has something to do with the last part, but i cant figure it out.
node* insert(node* head, int value) {
//check if head hasn't been created
if (head == NULL) {
head = malloc(sizeof(node));
if(head == NULL) {
printf("Failed to create head node");
return head;
}
head->value = value;
head->next = NULL;
return head;
}
//create a new node
node *newNode;
newNode = malloc(sizeof(node));
if(newNode == NULL) {
printf("Failed to create node");
return newNode;
}
newNode->value = value;
newNode->next = NULL;
//see if new node should be placed before head
if (value < head->value) {
newNode->next = head;
return newNode;
}
//search through to find correct spot and insert the node
node *temp = NULL;
temp = head;
while(temp->next != NULL && temp->value < value) {
temp = temp->next;
}
newNode->next = temp->next;
temp->next = newNode;
return head;
}
Part of the following bad
//search through to find correct spot and insert the node
node *temp = NULL;
temp = head;
while(temp->next != NULL && temp->value < value) {
temp = temp->next;
}
newNode->next = temp->next;
temp->next = newNode;
e.g. to fix like this:
node *temp ,*prev;
temp = head;
while(temp != NULL && temp->value <= value) {
prev = temp;
temp = temp->next;
}
newNode->next = temp;
prev->next = newNode;
or
node *temp ,*prev;
temp = head->next;
prev = head;
while(temp != NULL && temp->value < value) {
prev = temp;
temp = temp->next;
}
newNode->next = temp;
prev->next = newNode;
You need to check for temp->next->value inside the last while loop.
//This code of mine works perfectly.
void insertInAscOrder(int val)
{
node *new1;
node *temp;
node *previous;
//create new node
new1 = (node *)malloc(sizeof(node));
//check whether node is created or not
if(new1 == NULL)
{
printf("Insufficient memory.");
return;
}
//Updating different parts of the node
new1 -> info = val;
new1 -> next = NULL;
//checking whether the node created is only node or not
if (start == NULL)
{
start = new1;
}
//If value is less than the value of first node
else if(val < start -> info)
{
new1 -> next = start;
start = new1;
}
else
{
previous = start;
temp = start -> next;
//Go to the position where node is to be inserted
while(temp != NULL && val > temp -> info)
{
previous = temp;
temp = temp -> next;
}
//Insert the node at particular position
if(temp == NULL)
{
previous -> next = new1;
}
else
{
new1 -> next = temp;
previous -> next = new1;
}
}
}
It would be much better if you firstly implement (and test) functions like: push_front(), insert() (insert before), and push_back(), (probably advance (Node* curr, int steps);) and then simply take into consideration all the possibilities of insertion, i.e.:
empty list (current node is first, so just push_front / back())
iterate (advance()) over all elements from head and on, till:
element with value larger than the new is found, insert() before it.
last element reached, push_back().
in your new function insert_ordered().
Related
I want to reverse a linked list in pair such that if the list is 1->2->3->4->X
then it should output the following 2->1->4->3->X
I have tried to solve this problem but it doesn't seem to run. Guys, can you please help me with what is wrong in my code.
ListNode* reverseListInPairs(ListNode *head){
ListNode *current = head,*newHead = NULL,*temp = NULL,*prev = NULL;
while(current != NULL && current->next != NULL){
temp = current->next;
current->next = current->next->next;
temp->next = current;
prev->next = temp;
prev = current;
current = current->next;
if(newHead == NULL){
newHead = temp;
}
}
return newHead;
}
what is wrong in my code.
The main problem I see is here:
prev->next = temp;
On the first iteration of the loop, prev is still NULL at that point, so you're performing a null-pointer dereference.
You can resolve that issue and also remove the special case for the list head by introducing a synthetic head node in front of the real nodes:
ListNode* reverseListInPairs(ListNode *head) {
ListNode fake_head = { .next = head };
ListNode *prev = &fake_head;
ListNode *current = head;
while (current != NULL && current->next != NULL) {
ListNode *temp = current->next;
current->next = current->next->next;
temp->next = current;
prev->next = temp;
prev = current;
current = current->next;
}
return fake_head.next;
}
I've stuck as close as possible to your original code there, but personally, I'd tighten it up a little further. In particular, you don't need to maintain both current and prev across iterations; just the latter would be sufficient.
ListNode* reverseListInPairs(ListNode *head) {
ListNode fake_head = { .next = head };
ListNode *prev = &fake_head;
while (prev->next && prev->next->next) {
ListNode *first = prev->next;
ListNode *second = first->next;
prev->next = second;
first->next = second->next;
second->next = first;
prev = first;
}
return fake_head.next;
}
Node *reverse (Node *head, int k)
{
Node* current = head;
Node* next = NULL;
Node* prev = NULL;
int count = 0;
while (current != NULL && count < k)
{
next = current->next;
current->next = prev;
prev = current;
current = next;
count++;
}
if (next != NULL)
head->next = reverse(next, k);
return prev;
}
pass the value of k is 2
void pairWiseSwap(struct Node* head)
{
struct Node* temp = head;
/* Traverse further only if there are at-least two nodes left */
while (temp != NULL && temp->next != NULL) {
/* Swap data of node with its next node's data */
swap(&temp->data, &temp->next->data);
/* Move temp by 2 for the next pair */
temp = temp->next->next;
}
}
Sourced from GeeksForGeeks.
As for what is the error, it has been pointed out that prev is already NULL.
ListNode *current = head,*newHead = NULL,*temp = NULL,*prev = NULL;
.
.
prev->next = temp;
We can't have NULL->next as it will throw a segmentation fault.
I created a linked-list in C. I'm trying to create a function that looks at the lowest value in the linked list (which is the head) and the removes the "right-most" instance of that value in the list.
Suppose the linked-list looks like this:
2 -> 2 -> 2 -> 4 -> 5 -> 6
The head in this list is 2. But it's not the head I want to remove. I want to remove the 2 that comes before the 4 (it is the right-most instance of the head).
Here's the function I created to implement this:
double removeLowestValue() {
struct node *temp;
struct node *ptr = head;
double val = ptr->value;
if(head == tail)
{
free(head);
head = NULL;
tail = NULL;
}
else
{
while(ptr->value == ptr->next->value)
{
temp = ptr;
ptr = ptr->next;
val = ptr->value
}
temp->next = NULL;
temp->next = ptr->next;
free(ptr);
return val;
}
}
Then I tried to test if the function works:
int main() {
insertNode(18.0);
insertNode(13.0);
insertNode(11.0);
insertNode(11.0);
insertNode(22.0);
printf("%d", removeLowestValue());
return 0;
}
Unfortunately, the program doesn't print out "11" as expected. Matter of fact, it doesn't print anything at all. What's going on here?
EDIT:
Here's how I implemented the insertNode function:
void insertNode(double value) {
struct node *new_node = create_new_node(value);
struct node *temp = head;
struct node *prev;
if (head == NULL) {
head = new_node;
tail = new_node;
} else {
while (value > temp->value && temp->next != NULL) {
prev = temp;
temp = temp->next;
}
if(value < temp->value || value == temp->value)
{
/*If the value of the new node equals to the value of temp
OR if the value of the new node is less than the value of temp,
then insert the new node right before temp*/
new_node->next = temp;
prev->next = new_node;
}
else if(value > temp->value)
{
temp->next = new_node;
tail = new_node;
}
}
}
your function corrected, of course that supposes the list is sorted :
double removeLowestValue() {
if (head == NULL)
return 0; /* ???? */
else {
node * ptr = head;
node * previous = 0; /* the cell before the cell to remove */
while ((ptr->next != NULL) && (ptr->value == ptr->next->value)) {
previous = ptr;
ptr = ptr->next;
}
/* ptr is now the cell to remove */
double val = ptr->value;
if (ptr == head) {
/* remove the first cell */
ptr = head->next;
free(head);
head = ptr;
if (head == NULL)
/* the list is empty */
tail = NULL;
}
else if (ptr->next == NULL) {
/* all the values are the same in the list
ptr is the last cell */
free(ptr);
/* previous is now the last cell */
previous->next = NULL;
tail = previous;
}
else {
/* ptr is not the first nor the last cell */
previous->next = ptr->next;
free(ptr);
}
return val;
}
}
Concerning insertNode :
it is better to move the declaration of the variables temp and prev where they are useful, they are useless if head null so at the top of the definition
(value < temp->value || value == temp->value) can be just (value <= temp->value) and the else if (...) after can be just an else
when (value <= temp->value) prev can be still unset but used for prev->next = new_node, that appends when you insert 13 after 18 in your main. When (temp == head) you have to update head setting it with new_node
So the corrected version can be :
void insertNode(double value) {
struct node *new_node = create_new_node(value);
if (head == NULL) {
head = new_node;
tail = new_node;
} else {
struct node *temp = head;
struct node *prev = 0; /* = 0 to be sure to have a crash if something wrong */
while ((value > temp->value) && (temp->next != NULL)) {
prev = temp;
temp = temp->next;
}
if (value <= temp->value)
{
/* insert the new node right before temp*/
new_node->next = temp;
if (temp == head)
head = new_node;
else
/* prev was set */
prev->next = new_node;
} else {
/* insert the new node at end */
temp->next = new_node;
tail = new_node;
}
}
}
Using the additional definitions
typedef struct node {
double value;
struct node * next;
} node;
node * create_new_node(double value)
{
node * r = (node *) malloc(sizeof(node));
r->value = value;
r->next = 0;
return r;
}
int main() {
insertNode(18.0);
insertNode(13.0);
insertNode(11.0);
insertNode(11.0);
insertNode(22.0);
printf("%g\n", removeLowestValue());
printf("%g\n", removeLowestValue());
printf("%g\n", removeLowestValue());
printf("%g\n", removeLowestValue());
printf("%g\n", removeLowestValue());
printf("%g\n", removeLowestValue());
return 0;
}
the execution writes (the last 0 indicate the list is empty)
11
11
13
18
22
0
I am trying to build a head delete and tail delete function for Linked Lists.
I have succeeded in head delete, but my tail delete function does not actually delete the tail.
First, how my Linked List is created:
typedef struct node
{
int data;
struct node *next;
} node;
typedef struct LinkedList
{
node *head;
node *tail;
} LinkedList;
node *createNode(int data)
{
node *new_node;
if ((new_node = malloc(sizeof(node))) == NULL)
return NULL;
new_node->data = data;
new_node->next = NULL;
return new_node;
}
LinkedList *createLinkedList(void)
{
LinkedList *LL;
if ((LL = calloc(1, sizeof(LinkedList))) == NULL)
return NULL;
return LL;
}
void tailInsert(LinkedList *LL, int data)
{
if (LL == NULL)
return;
if (LL->tail == NULL)
{
LL->head = LL->tail = createNode(data);
return;
}
LL->tail->next = createNode(data);
LL->tail = LL->tail->next;
}
These are the two functions:
This one does not work:
int tail_Delete(LinkedList *list)
{
int retval;
node *temp;
if (list == NULL || list->head == NULL)
return EMPTY_LIST_ERR;
// if only one node
if (list->head->next == NULL)
{
retval = list->head->data;
free(list->head);
list->head = NULL;
list->tail = NULL;
return retval;
}
// make temp the node before the tail
for (temp = list->head; temp->next != list->tail; temp = temp->next)
;
retval = list->tail->data;
free(list->tail);
list->tail = temp;
return retval;
}
This one does work:
int head_delete(LinkedList *list)
{
int retval;
node *temp;
if (list == NULL || list->head == NULL)
return EMPTY_LIST_ERR;
retval = list->head->data;
temp = list->head->next;
free (list->head);
list->head = temp;
if (list->head == NULL)
list->tail = NULL;
return retval;
}
Print function and main:
void print_Linked_list(LinkedList *LL)
{
node *temp;
if (LL == NULL || LL->head == NULL)
return;
for (temp = LL->head; temp != NULL; temp = temp->next)
printf("%d%c", temp->data, (temp->next == NULL) ? '\n' : ' ');
}
int main(void)
{
LinkedList *LL;
int val;
LL = createLinkedList();
tailInsert(LL, 1);
tailInsert(LL, 2);
tailInsert(LL, 3);
print_Linked_list(LL);
val = tail_Delete(LL);
//val = head_delete(LL);
printf("%d\n", val);
print_Linked_list(LL);
return 0;
}
Output for head_delete:
1 2 3
Deleted node: 1
2 3
Output for tail_delete:
1 2 3
Deleted node: 3
1 2 3
It may be a simple mistake, but they seem to be practically equivalent functions.
Thank you for your help.
A tail delete needs to be managed from the node before the tail. If you don't have a back pointer in the list (doubly linked), then you'll need to walk forward to the tail.
Untested, tail delete (with no back pointer) should look like this.
LinkedList *node = list->head;
LinkedList *next = node->next;
while(next != list->tail) { // walk to the end
node = next;
next = node->next;
}
// now node is the node before the tail and next is the tail
int retval = next->data;
free(next);
// this is why we needed to walk here
node->next = NULL;
You should assign 0 to temp->next pointer when for loop was executed. By this you indicate that this element is last one.
for (temp = list->head; temp->next != list->tail; temp = temp->next)
;
temp->next = 0; // <---
retval = list->tail->data;
I am working on doubly linked list in c, I have a doubly linked tepm2 with 20 nodes and I want to delete the node whose word user insert.
struct node {
struct node *prev;
char word[100];
int repeatTime;
struct node *next;
} *h, *temp, *temp1, *temp2;
Each node has unique word.
printf("\n Enter word to delete : ");
scanf("%s", &word);
Delete(word);
int delete(char data[200]) { //unable to delete
if (h == NULL)
return;
temp2 = next = previous = h;
while (temp2->next != NULL) {
if ((strcmp(temp2->word, data) == 0)) {
if (temp2->prev == NULL) {
h = temp2->next;
free(temp2);
return;
} else if (temp2->prev == NULL) {
previous->next = temp2;
free(temp2);
previous->next = NULL;
return;
} else {
previous->next = temp2->next;
next->prev = temp2->next;
}
}
temp2 = temp->next;
}
}
I am been unable to delete the specific node that word user enter
Try this:
int delete(const char *data)
{
struct node *temp = h;
if (h == NULL) return;
while (temp->next != NULL)
{
if (strcmp(temp->word, data) == 0)
{
if (temp->prev != NULL)
{
temp->prev->next = temp->next;
}
if (temp->next != NULL)
{
temp->next->prev = temp->prev;
}
if (h == temp)
{
h = temp->next;
}
free(temp);
return;
}
temp = temp->next;
}
}
First of all I don't think this is right temp2 = next = previous = h;
Now all you have to do is find the node that you want to delete by traversing and than link it's prev node to it's next node i.e. (temp2->prev)->next = next and (temp2->next)->prev = prev and free it.
Now the real issue lies with
1. The first node that has other nodes after it
2. last node that has other nodes preceding it
3. only node
You can simplify all three by converting them into the former problem i.e. node in the middle problem which we've just solved.
For simplifying you can just make the head and tail both NULL.
Below is the code for inserting a node in a doubly linked list.
struct dllist
{
int data;
struct dllist *prev, *next;
};
void DLLInsert(struct dllist **head, int position, int data)
{
int k = 1;
struct dllist *temp, *newNode;
newNode = (struct dllist *)malloc(sizeof(struct dllist));
if (!newNode)
{
printf("Memory Error\n");
}
newNode->data = data;
if (position == 1)
{
newNode->next = *head;
newNode->prev = NULL;
*head = newNode;
return;
}
else
{
temp = *head;
while (temp->next != NULL && k < position - 1)
{
k++;
temp = temp->next;
}
if (temp->next == NULL)
{
temp->next = newNode;
newNode->prev = temp;
newNode->next = NULL;
}
else
{
newNode->prev = temp;
newNode->next = temp->next;
temp->next = newNode;
temp->next->prev = newNode;
}
}
}
I am getting somewhat confused in the underlying pointer operations being a newbie. A **head is passed onto the function to modify it. But in case when the position>1, a copy of *head(temp) is used to modify the list compared to the case when position==1. Can anybody explain me why is it so?
Thanks
When position > 1, temp is set to *head, and the code iterates temp through the linked list to the node at index position. Effectively, you are modifying the node at index position.
When position = 1, you are modifying the head node, so you don't need to iterate.
In the case of position==1, your new element will become the new head. You already know exactly where it is. Otherwise, you need to find the position.
temp = *head;
while (temp->next != NULL && k < position - 1)
This is used to iterate through the list until you find the element in the position you are going to insert at.
temp = temp->next;
The first element you assign to temp is head, but it is replaced with the next element in each iteration.