Why isn't my reverse(); function working? - c

I'm writing a program in C for reversing a circular singly linked list. I keep getting segmentation fault for some reason. I'm sure the problem is with the reverse function as I tried commenting the function call, the program works fine.
For my reverse() function, I have used 3 pointers: prev, next and curr. The logic is that I'll run a loop till curr takes the address of head, which will be stored in the link part of the last node since it is a circular linked list. I'll keep updating curr->link using prev pointer which will change its link from the next to its previous node.
When the loop breaks, head->link = prev; and head = prev; will update the respective addresses such that they point to the first node of the reversed list.
//reversing CLL
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *link;
} *head;
void reverse() {
struct node *prev = NULL, *curr = head, *next;
while (curr != head) {
next = curr->link;
curr->link = prev;
prev = curr;
curr = next;
}
head->link = prev;
head = prev;
}
void createList(int n) {
int i, data;
head = (struct node *)malloc(sizeof(struct node));
struct node *ptr = head, *temp;
printf("Enter data of node 1\t");
scanf("%d", &data);
head->data = data;
head->link = NULL;
for (i = 2; i <= n; i++) {
temp = (struct node *)malloc(sizeof(struct node));
printf("Enter data of node %d\t", i);
scanf("%d", &data);
temp->data = data;
temp->link = NULL;
ptr->link = temp;
ptr = ptr->link;
}
ptr->link = head;
}
void disp() {
struct node *ptr = head;
do {
printf("%d\t", ptr->data); //gdb debugger shows problem is in this line
ptr = ptr->link;
} while (ptr != head);
}
int main() {
int n;
printf("Enter no of nodes to be created\t");
scanf("%d", &n);
createList(n);
printf("\n\nList is displayed below!\n");
disp();
printf("\n\nReversing list ...\n");
reverse(); // on commenting this call, disp() function
// works accurately showing node data non-reversed
disp();
printf("\n\nList successfully reversed!\n");
}

The loop in the reverse() function exits immediately because curr is initialized to the value of head so the test while (curr != head) is false at the first iteration.
reverse() then sets head->link to NULL and finally head is also set to NULL (the initial value of prev), which explains the segmentation fault in the subsequent disp() function where you use a do { } while (pre != head) that cannot handle an empty list.
Here is a modified version:
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *link;
};
struct node *reverse(struct node *head) {
struct node *prev = NULL, *curr = head;
if (head) {
do {
struct node *next = curr->link;
curr->link = prev;
prev = curr;
curr = next;
} while (curr != head);
curr->link = prev;
head = prev;
}
return head;
}
struct node *createList(int n) {
struct node *head = NULL, *tail = NULL, *temp;
int i;
for (i = 1; i <= n; i++) {
temp = (struct node *)malloc(sizeof(struct node));
temp->data = 0;
temp->link = NULL;
printf("Enter data of node %d\t", i);
scanf("%d", &temp->data);
if (head == NULL) {
head = temp;
} else {
tail->link = temp;
}
tail = temp;
temp->link = head;
}
return head;
}
void disp(struct node *head) {
if (head) {
struct node *ptr = head;
do {
printf("%d\t", ptr->data);
ptr = ptr->link;
} while (ptr != head);
}
}
int main() {
struct node *head;
int n = 0;
printf("Enter no of nodes to be created\t");
scanf("%d", &n);
head = createList(n);
printf("\n\nList is displayed below!\n");
disp(head);
printf("\n\nReversing list ...\n");
head = reverse(head);
disp(head);
printf("\n\nList successfully reversed!\n");
// should free the list
return 0;
}

For starters it is a bad idea to use the global variable head
struct node {
int data;
struct node *link;
} *head;
In this case the functions depend on the global variable and you can not use more than one list in a program.
Due to this initialization
struct node *prev = NULL, *curr = head, *next;
^^^^^^^^^^^^
the condition of the while loop
while (curr != head) {
is never evaluates to true because initially the pointer curr is equal to the pointer head.
Moreover if the list is empty then this statement
head->link = prev;
invokes undefined behavior.
Here is a demonstrative program that shows how the list can be declared in main and then reversed.
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *link;
};
size_t assign( struct node **head, const int a[], size_t n )
{
while ( *head )
{
struct node *tmp = *head;
*head = ( *head )->link;
free( tmp );
}
size_t total = 0;
struct node *first = NULL;
while ( total < n && ( *head = malloc( sizeof( struct node ) ) ) != NULL )
{
( *head )->data = a[total];
( *head )->link = NULL;
++total;
if ( first == NULL ) first = *head;
head = &( *head )->link;
}
if ( first != NULL ) *head = first;
return total;
}
void display( const struct node *head )
{
if ( head != NULL )
{
const struct node *current = head;
do
{
printf( "%d -> ", current->data );
} while ( ( current = current->link ) != head );
}
puts( "null" );
}
struct node * reverse( struct node **head )
{
if ( *head )
{
struct node *last = *head;
struct node *prev = NULL;
while ( ( *head )->link != last )
{
struct node *current = *head;
*head = ( *head )->link;
current->link = prev;
prev = current;
}
( *head )->link = prev;
last->link = *head;
}
return *head;
}
int main(void)
{
struct node *head = NULL;
int a[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
assign( &head, a, sizeof( a ) / sizeof( *a ) );
display( head );
display( reverse( &head ) );
display( reverse( &head ) );
return 0;
}
The program output is
0 -> 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> null
9 -> 8 -> 7 -> 6 -> 5 -> 4 -> 3 -> 2 -> 1 -> 0 -> null
0 -> 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> null

Related

can't get response from a function in c [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I'm writing a program that creates a doubly linked list and removes a element with negative value from it. Everything pretty much works, except for the part when I called the modify function and when I try to delete it, program crashes. Any suggestions?
/*
*Given a doubly linked lists with +ve and -ve key values.
*Write a function to delete all the nodes with negative key values.
*/
#include<stdio.h>
#include<stdlib.h>
struct list {
int data;
struct list *next;
struct list *prev;
};
struct list *head = NULL;
struct list* create(int);
void modify(struct list*);
int main(void) {
int n, i, value;
struct list *temp;
printf("Enter the count of node :");
scanf("%d",&n);
for (i = 0; i < n; i ++) {
printf("Enter the value of node: ");
scanf("%d",&value);
create(value);
}
temp = head;
printf("\nDoubly linked list is created and the list is as follows : \n");
while (temp != NULL) {
printf("%d ",temp -> data);
temp = temp -> next;
}
modify(head);
}
struct list* create(int value) {
struct list *new_node, *temp;
temp = head;
new_node = (struct list*)malloc(sizeof(struct list));
new_node -> data = value;
new_node -> next = NULL;
new_node -> prev = NULL;
if (head == NULL) {
head = new_node;
}
else {
while (temp -> next != NULL) {
temp = temp -> next;
}
temp -> next = new_node;
new_node -> prev = temp;
}
return head;
}
void modify(struct list *head) {
struct list *current_node, *prev_node, *next_node, *temp;
temp = head;
while (temp -> next != NULL) {
if (temp -> data < 0) {
current_node = temp;
prev_node = temp -> prev;
next_node = temp -> next;
prev_node -> next = next_node;
next_node -> prev = prev_node;
free(current_node);
}
}
printf("\nThe modified doubly linked list is : \n ");
temp = head;
while (temp -> next != NULL) {
printf("%d",temp -> data);
temp = temp -> next;
}
}
See the examples of Vlad from Moscow to have a better understanding of what you were doing.
I shall go trough your code and tell you what I would change.
/*
*Given a doubly linked lists with +ve and -ve key values.
*Write a function to delete all the nodes with negative key values.
*/
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
First of all: you're making a (doubly linked) list of nodes, not a list of lists. Call it a Node. Also, you can do a typedef to prevent you from writing struct Node all the time.
struct Node {
int data;
struct Node* next;
struct Node* prev;
};
void append(struct Node** head, int value); // variable names aren't needed here
struct Node* findLastNode(struct Node** head);
void removeNegativeNodes(struct Node** head);
void removeNode(struct Node** head, struct Node* currNode);
int main(void)
{
Try not to use global variables. There are many reasons to be found why not to use them, but in here it's possible to don't use them as well. Imagine having thousands of lines of code, you won't be able to have a decent view on the code.
struct Node* head = NULL;
struct Node* p; // temp-<p>ointer
int n, value;
printf("Enter the count of node :");
scanf("%d", &n);
You only need i in the for-loop, so keep it there.
for (int i = 0; i < n; ++i) {
printf("Enter the value of node: ");
scanf("%d", &value);
Make sure your function names are clear and tell you what they do. create() would tell me it creates a Node, but not that it also appends the node.
append(&head, value);
}
// this can be in a function! (A) printData
p = head; // temp-<p>ointer
printf("\nDoubly linked list is created and the list is as follows : \n");
while (p != NULL) {
printf("%d <=> ", p->data);
p = p->next;
}
printf("NULL\n");
Look at what you're doing: perhaps you want to make a general function to split the code? Here you're again going trough the list and printing out it's data members.
// this can be in a function! (B) printData
removeNegativeNodes(&head);
printf("\nThe modified doubly linked list is : \n");
p = head;
while (p != NULL) {
printf("%d <=> ", p->data);
p = p->next;
}
printf("NULL\n");
}
struct Node* findLastNode(struct Node** head)
{
struct Node* p = *head;
if (p != NULL)
while (p->next != NULL)
p = p->next;
return p;
}
Since your head has to be changed, you'll have to pass the address of the head as well. Also, split your code a bit, so it's easier for yourself to have an idea of your code's structure. If your function is 40 rules long, it will take longer to find out where the cause of the bug is located (exactly).
void append(struct Node** head, int value)
{
struct Node* lastNode = findLastNode(head);
struct Node* nextNode = (struct Node*)malloc(sizeof(struct Node));
if (lastNode != NULL) {
lastNode->next = nextNode;
nextNode->prev = lastNode;
}
else {
*head = nextNode;
nextNode->prev = NULL;
}
nextNode->next = NULL;
nextNode->data = value;
}
Here as well: the first number can be negative, so make sure you can access the head variable by it's address. Also, again keep it simple and split your code in functions removeNegativeNodes > removeNode.
void removeNegativeNodes(struct Node** head)
{
struct Node* p = *head;
struct Node* temp;
while (p != NULL) {
temp = p->next;
if (p->data < 0)
removeNode(head, p);
p = temp;
}
}
void removeNode(struct Node** head, struct Node* currNode)
{
if (currNode->next != NULL)
currNode->next->prev = currNode->prev;
if (currNode->prev != NULL)
currNode->prev->next = currNode->next;
else
*head = currNode->next;
free(currNode);
}
I've tested the code and it should work. Having it worked properly is not important though, it's understanding what happens. I recommend you having a closer look to it. Goodluck!
Your definition of a doubly-linked list does not make great sense.
The list should contain two pointers: to the head node and to the tail node of the list.
So you need to define two structures. The first one defines the node and the second one defines the list itself.
In this case you need not to traverse the whole list to append a new node to the tail of the list.
The function create with the confusing name is based on the global variable head while the function modify instead gets the variable through a parameter.
This is very confusing. As result for example you can not create two lists in a program.
So as the function modify gets the pointer to the head node by value then it means that it deals with a copy of the pointer to the head node. As a result any changes of the pointer to the head node in the function does not influence on the original pointer to the head node.
This loop in the function modify
temp = head;
while (temp -> next != NULL) {
in general can invoke undefined behavior because it is not excluded that the pointer to the head node can be equal to NULL.
And in any case the condition of the loop does not make sense because within the loop you are considering not the next node but the current
while (temp -> next != NULL) {
if (temp -> data < 0) {
So a question arises if temp->next is equal to NULL but the value of the current node pointed to by the pointer temp is negative does it mean that this node will not be removed?
Pay attention to that if you will write the condition of the loop correctly nevertheless either data member prev of the removed node or the data member next of the removed node or even the both can be equal to NULL. In this case these statements
prev_node = temp -> prev;
next_node = temp -> next;
prev_node -> next = next_node;
^^^^^^^^^^^^^^^^^
next_node -> prev = prev_node;
^^^^^^^^^^^^^^^^^
again can invoke undefined behavior.
here is a demonstrative program that shows how the list and its functions can be defined. Investigate it.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
struct Node
{
int data;
struct Node *next;
struct Node *prev;
};
struct List
{
struct Node *head;
struct Node *tail;
};
int push_back( struct List *list, int data )
{
struct Node *new_node = malloc( sizeof( struct Node ) );
int success = new_node != NULL;
if ( success )
{
new_node->data = data;
new_node->next = NULL;
if ( list->head == NULL )
{
new_node->prev = NULL;
list->head = list->tail = new_node;
}
else
{
new_node->prev = list->tail;
list->tail = list->tail->next = new_node;
}
}
return success;
}
void remove_if( struct List *list, int predicate( int ) )
{
struct Node *prev = NULL;
for ( struct Node **current = &list->head; *current != NULL; )
{
if ( predicate( ( *current )->data ) )
{
struct Node *tmp = *current;
if ( ( *current )->next != NULL )
{
( *current )->next->prev = ( *current )->prev;
}
*current = ( *current )->next;
free( tmp );
}
else
{
prev = *current;
current = &( *current )->next;
}
}
list->tail = prev;
}
void display( const struct List *list )
{
for ( const struct Node *current = list->head; current != NULL; current = current->next )
{
printf( "%d -> ", current->data );
}
puts( "null" );
}
void display_reverse( const struct List *list )
{
for ( const struct Node *current = list->tail; current != NULL; current = current->prev )
{
printf( "%d -> ", current->data );
}
puts( "null" );
}
int is_negative( int data )
{
return data < 0;
}
int main(void)
{
struct List list = { .head = NULL, .tail = NULL };
const size_t N = 10;
srand( ( unsigned int )time( NULL ) );
for ( size_t i = 0; i < N; i++ )
{
push_back( &list, rand() % N - N / 2 );
}
display( &list );
display_reverse( &list );
putchar( '\n' );
remove_if( &list, is_negative );
display( &list );
display_reverse( &list );
putchar( '\n' );
return 0;
}
The program output might look like
2 -> 4 -> 3 -> -5 -> 3 -> -3 -> -3 -> -2 -> 0 -> 2 -> null
2 -> 0 -> -2 -> -3 -> -3 -> 3 -> -5 -> 3 -> 4 -> 2 -> null
2 -> 4 -> 3 -> 3 -> 0 -> 2 -> null
2 -> 0 -> 3 -> 3 -> 4 -> 2 -> null
The create() function returns a linked list item. so you have to assign the return value to an item. Also the definition of pointers inside the struct is completely wrong.
struct list {
int data;
struct list *next;
struct list *prev;
};
struct list *head = NULL;
struct list* create(int); //function prototype
void modify(struct list*);//function prototype
int main(void) {
int n, i, value;
struct list *temp;
printf("Enter the number of nodes :");
scanf("%d",&n);
for (i = 0; i < n; i ++) {
printf("Enter the value of node: ");
scanf("%d",&value);
create(value);
}
temp = head;
printf("\nDoubly linked list is created and the list is as follows : \n");
while (temp != NULL) {
printf("%d ",temp -> data);
temp = temp -> next;
}
modify(head);
}
void create(int value) {
struct list* point = head;
while(point->next){
if(point->data != value)
point = point->next;
else{
printf("Data exists\n");
return NULL;
}
}
struct list* item = (struct list*)malloc(sizeof(struct list));
item->data = value;
item->next = NULL;
item->prev = point;
}
void modify(struct list *head) {
struct list *current_node, *prev_node, *next_node, *temp;
temp = head;
while (temp -> next != NULL) {
if (temp -> data < 0) {
temp->prev->next = temp->next;
temp->next->prev = temp->prev;
free(temp);
}
temp = temp->next;
}
printf("\nThe modified doubly linked list is : \n ");
temp = head;
while (temp -> next != NULL) {
printf("%d",temp -> data);
temp = temp -> next;
}
}
I hope this will work for you.

How to keep track of and return the head of a linked list in C while adding a node to the end

Currently I have a node struct that looks like the following:
struct Node {
int data;
Node* next;
};
and I want to implement a function that will make a linked list given a list of elements and will return the head of the list. I currently have the following structure:
struct Node *make_list(int values[10]) {
struct Node *curr;
head = (struct Node*) malloc(sizeof(struct Node));
struct Node *head;
head->value = values[0];
head->next = NULL;
curr = head;
for (int i = 1; i < 10 i++) {
struct Node *tmp;
tmp->value=values[i];
curr->next=tmp;
}
return head;
}
For some reason this code is always returning NULL as the head of the linked list, but after that null element, the 10 elements are in order. Does anybody know why this is happening?
For starters this declaration of node
struct Node {
int data;
Node* next;
};
is invalid in C. You have to declare the structure like
struct Node {
int data;
struct Node* next;
};
Your function definition will not compile and does not make great sense.
It can be defined for example the following way
struct Node * make_list( const int values[], size_t n )
{
struct Node *head = NULL;
struct Node **current = &head;
for ( size_t i = 0; i < n; i++ )
{
*current = malloc( sizeof( struct Node ) );
( *current )->data = values[i];
( *current )->next = NULL;
current = &( *current )->next;
}
return head;
}
Here is a demonstrative program.
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node* next;
};
struct Node * make_list( const int values[], size_t n )
{
struct Node *head = NULL;
struct Node **current = &head;
for ( size_t i = 0; i < n; i++ )
{
*current = malloc( sizeof( struct Node ) );
( *current )->data = values[i];
( *current )->next = NULL;
current = &( *current )->next;
}
return head;
}
void out( struct Node *head )
{
for ( ; head != NULL; head = head->next )
{
printf( "%d -> ", head->data );
}
puts( "null" );
}
int main(void)
{
int a[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
const size_t N = sizeof( a ) / sizeof( *a );
struct Node *head = make_list( a, N );
out( head );
return 0;
}
Its output is
0 -> 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> null
An alternative to Vlad's answer (which I think will be simpler to understand):
struct Node *make_list(int values[10]) {
struct Node* head = (struct Node*) malloc(sizeof(struct Node));
head->value = values[0];
head->next = NULL;
struct Node *curr;
curr = head;
for (int i = 1; i < 10; i++) {
struct Node* tmp = (struct Node*) malloc(sizeof(struct Node));
tmp->value=values[i];
tmp->next = NULL;
curr->next=tmp;
curr = tmp;
}
return head;
}
You must always allocate new memory to a new node
struct Node* tmp = (struct Node*) malloc(sizeof(struct Node));
otherwise it will be lost after the function finishes.
Then, you must ensure that you new node receives the correct values:
tmp->value=values[i];
tmp->next = NULL;
Finally, you need to update your curr node, so it points to the last item in the list (usually we call this as tail).
next=tmp;
curr = tmp;
You can try it out here.

inserting a node at the end of linked list recursivly

typedef struct node
{
int a;
struct node *next;
}node;
void generate(struct node **head)
{
int num = 10, i; //the num here is the length
struct node *temp;
for (i = 0; i < num; i++)
{
temp = (struct node*)malloc(sizeof(struct node));
temp->a = 10-i;
if (*head == NULL)
{
*head = temp; //each time add another node to the start
(*head)->next = NULL;
}
else
{
temp->next = *head;
*head = temp;
}
}
}
void addSpecific(node* head,int n)
{
node* temp = NULL;
if (head->next == NULL)
{
temp = (node*)malloc(sizeof(node*)); //allocating memory
(temp)->a = n; //adding the wanted value
(temp)->next = NULL; //making the new node to point to the end
head->next = temp; //and the previous one to point to temp
}
else
{
addSpecific(head->next, n); //if this is not the wanted node we need to move to the next node
}
}
void deleteNode(struct node **head)
{
struct node *temp;
while (*head != NULL)
{
temp = *head;
*head = (*head)->next; //going to the next node
free(temp); //free the allocated memory
}
}
int main()
{
struct node *head = NULL;
generate(&head);
addSpecific(head, 7);
display(head);
deleteNode(&head);
system("PAUSE");
return 0;
}
I was trying to insert new node at the end using recursion, but the free memory (delete) function make an expansion, and I couldn't find the problem. I tried the generating function and adding the node at the end and it worked but the complier alert me for "heap corruption".
The function can look the following way as it is shown in the following demonstrative program
#include <stdlib.h>
#include <stdio.h>
typedef struct node
{
int a;
struct node *next;
} node;
void addSpecific( node **head, int n )
{
if ( *head == NULL )
{
*head = malloc( sizeof( node ) ); //allocating memory
( *head )->a = n; //adding the wanted value
( *head )->next = NULL; //making the new node to point to the end
}
else
{
addSpecific( &( *head )->next, n ); //if this is not the wanted node we need to move to the next node
}
}
void display( node *head )
{
for ( ; head != NULL; head = head->next ) printf( "%d ", head->a );
printf( "\n" );
}
int main( void )
{
const int N = 10;
node *head = NULL;
for ( int i = 0; i < N; i++ )
{
addSpecific( &head, i );
display( head );
}
return 0;
}
The program output is
0
0 1
0 1 2
0 1 2 3
0 1 2 3 4
0 1 2 3 4 5
0 1 2 3 4 5 6
0 1 2 3 4 5 6 7
0 1 2 3 4 5 6 7 8
0 1 2 3 4 5 6 7 8 9
As for the function deleteNode then it can look for example the following way
void deleteNode( node **head )
{
for ( node *current = *head; current != NULL; )
{
node *temp = current;
current = current->next; //going to the next node
free( temp ); //free the allocated memory
}
*head = NULL;
}
As for this implementation of the function
void deleteNode( node **head )
{
while ( *head != NULL )
{
node *temp = *head;
head = &( *head )->next; //going to the next node
free( temp ); //free the allocated memory
}
}
then it has undefined behavior because it tries to access a data member of the structure object that was already deleted.
Or you can make the function recursive. For example
void deleteNode( node **head )
{
if ( *head != NULL )
{
deleteNode( &( *head )->next );
free( *head );
*head = NULL;
}
}
A bit off-topic, but it is quite convenient to maintain single-linked lists with two pointers, rather than one. This way you can prepend and append the list in O(1) without having to use recursion. E.g.:
struct Node
{
struct Node* next;
};
struct List
{
struct Node *head, **tail;
};
void List_init(struct List* list) {
list->head = 0;
list->tail = &list->head;
}
void List_append(struct List* list, struct Node* node) {
node->next = 0;
*list->tail = node;
list->tail = &node->next;
}
void List_prepend(struct List* list, struct Node* node) {
node->next = list->head;
list->head = node;
if(list->tail == &list->head)
list->tail = &node->next;
}
void List_remove(struct List* list, struct Node* node) {
struct Node *cur = list->head, **prev = &list->head;
while(cur && cur != node) {
prev = &cur->next;
cur = cur->next;
}
if(cur) {
if(!(*prev = node->next))
list->tail = prev;
}
}
# For reference:
#
# SinglyLinkedListNode:
# int data
# SinglyLinkedListNode next
def insertNodeAtTail(node, data):
if node is None:
last = SinglyLinkedListNode(node_data=data)
last.next = None
return last
if node.next is None:
last = SinglyLinkedListNode(node_data=data)
last.next = None
node.next = last
return node
node.next = insertNodeAtTail(node.next, data)
return node

Doubly Linked List Insertion With Recursion in C

I'm more or less just learning C, I was given an simple assignment that deals with doubly linked lists, dynamic data allocation, and recursion. I created an array of just 10 integers and I am trying to put these integers into a sorted doubly linked list using recursion. I am having some trouble with inserting nodes into the linked list; I think I have the first node down, but I'm not sure if the rest makes any sense. Right now I'm also getting a segmentation fault... Thank you for any help!
#include <stdio.h>
#include <stdlib.h>
#define N 10
typedef struct node_ {
int value;
struct node_ *next;
struct node_ *prev;
} node;
void insert(node **head, node *cur, node *p);
void print_list(node *cur);
void print_list(node *cur)
{
if (!cur) {
printf("\n");
return;
} else {
printf("%d ", cur->value);
print_list(cur->next);
}
}
int main(int argc, char *argv[])
{
int i;
int data[N] = {2, 7, 3, 9, 4, 4, 0, 8, 7, 100};
node *p, *head;
head = NULL;
for (i = 0; i < N; i++) {
p = (node *)malloc(sizeof(node));
p->value = data[i];
insert(&head, head, p);
}
print_list(head);
}
void insert(node **head, node *cur, node *p)
{
if(*head == NULL)
{
p->next = (*head);
//(*head)->prev = p;
(*head) = p;
}
if(p->value < cur->value)
{
cur->prev->next = p;
p->prev = cur->prev;
cur->prev = p;
p->next = cur;
}
insert(head, cur, p);
//p->next = *head;
//*head = p;
}
There are a few mistakes in your recursive insert function. It will be clear in the comments of my code:
void insert(node **head, node *cur, node *p)
{
if(*head == NULL) // the list will contain a single element
{
p->next = p->prev = NULL;
*head = p;
return; // we're done for this case!
}
if(p->value < cur->value)
{
p->prev = cur->prev;
p->next = cur;
cur->prev = p;
if(cur->prev != NULL) // what if cur is the head? there is no cur->prev!
cur->prev->next = p;
else
*head = p; // p becomes the new head
return; // we're done!
}
if(cur->next == NULL) // if cur is the last in the list, we just insert p after it
{
cur->next = p;
p->next = NULL;
p->prev = cur;
}
else // now we can proceed recursively and check the next element
insert(head, cur->next, p);
}
I think that it is the function insert itself that has to allocate new node.
It should have two parameters: pointer to head and a value that to be added.
Here is a demonstrative program that shows how the function can be writtem
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct node
{
int value;
struct node *next;
struct node *prev;
} node;
void insert( node **current, int value )
{
if ( *current == NULL || value < ( *current )->value )
{
node *tmp = malloc( sizeof( node ) );
tmp->value = value;
tmp->next = *current;
if ( *current != NULL )
{
tmp->prev = ( *current )->prev;
( *current )->prev = tmp;
}
else
{
tmp->prev = NULL;
}
*current = tmp;
}
else if ( ( *current )->next == NULL )
{
node *tmp = malloc( sizeof( node ) );
tmp->value = value;
tmp->next = ( *current )->next;
tmp->prev = *current;
( *current )->next = tmp;
}
else
{
insert( &( *current )->next, value );
}
}
void print_list( node *current )
{
if ( current == NULL )
{
printf( "\n" ) ;
}
else
{
printf( "%d ", current->value );
print_list( current->next );
}
}
#define N 10
int main( void )
{
node *head = NULL;
srand( ( unsigned int )time( NULL ) );
for ( int i = 0; i < N; i++ )
{
int value = rand() % N;
printf( "%d ", value );
insert( &head, value );
}
printf( "\n" );
print_list( head );
return 0;
}
The program output might look like
4 9 0 0 6 7 2 7 3 3
0 0 2 3 3 4 6 7 7 9
Of course you need to write also a recursive function that will free all allocated mempry for nodes.

union of linked lists

I have created a program to find union of 2 linked list. My logic is first of all take a new list insert list1 contents into this list and insert only those values from list2 which are not in result list. My code is:
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
/* Linked list node */
struct node
{
int data;
struct node* next;
};
struct node* result;
struct node *newNode(int data)
{
struct node *new_node = (struct node *) malloc(sizeof(struct node));
new_node->data = data;
new_node->next = NULL;
return new_node;
}
/* Function to insert a node at the beginning of the Doubly Linked List */
void push(struct node** head_ref, int new_data)
{
/* allocate node */
struct node* new_node = newNode(new_data);
new_node->next=*head_ref;
*head_ref=new_node;
}
struct node *union3(struct node *first, struct node *second, struct node *result)
{
int flag = 0;
struct node *temp = NULL;
temp = first;
struct node *temp2 = NULL;
temp2 = second;
int value;
while (temp != NULL)
{
push(&result, temp->data); // pushing first list values in result
temp = temp->next;
}
while (second)
{
present(second->data, result); // comparing second list each member with result
second = second->next;
}
return result;
}
void present(int data, struct node *result1)
{
printf("The value in the beginning of present function is:");
int flag = 0;
while (result1)
{
if (result1->data == data)
{
flag++;
}
result1 = result1->next;
}
if (flag > 0)
{
printf("");
}
else
{
push(&result, data);
}
}
void printList(struct node *node)
{
while(node != NULL)
{
printf("%d ", node->data);
node = node->next;
}
printf("\n");
}
/* Drier program to test above function */
int main(void)
{
struct node* first = NULL;
struct node* second=NULL;
// struct node* result=NULL;
struct node* union2=NULL;
// create first list 7->5->9->4->6
push(&first, 6);
push(&first, 4);
push(&first, 9);
push(&first, 5);
push(&first, 7);
printf("First list is:");
printList(first);
push(&second,6);
push(&second,4);
push(&second,9);
push(&second,11);
push(&second,12);
printf("second list is");
printList(second);
printf("their union is:");
union2=union3(first,second,result);
printf("Union of 2 lists is:");
printList(union2);
return 0;
}
Basically my logic is right but a problem is coming in result variable. Its list1 values pushed in it gets lost in it when it goes in present() function even though I have made result a global variable. Can anybody tell why output is displaying only list1 contents as:
output:6 4 9 5 7
With your algorithm if list1 has duplicates they will show in the final result, but if list2 has duplicates they will not show in the final result which is something you probably don't want.
Also I think you meant to use temp2 instead of second in:
while(second)
{
present(second->data,result); //comparing second list each member with result
second=second->next;
}
and finally this took me some time but I found your error:
push(&result,data);
should be result1
Hope this helps!
Catch! :) All you else need is to add a function that will free allocated memory for the lists.
#include <stdlib.h>
#include <stdio.h>
/* Linked list node */
struct node
{
int data;
struct node *next;
};
struct node *newNode( int data, struct node *next )
{
struct node *tmp = ( struct node *) malloc( sizeof( struct node ) );
if ( tmp )
{
tmp->data = data;
tmp->next = next;
}
return tmp;
}
/* Function to insert a node at the beginning of the Doubly Linked List */
void push( struct node **head_ref, int data )
{
/* allocate node */
struct node *tmp = newNode( data, *head_ref );
if ( tmp != NULL )
{
*head_ref = tmp;
}
}
int find( struct node *head, int data )
{
while ( head && head->data != data ) head = head->next;
return head != NULL;
}
struct node* list_union( struct node *first, struct node *second )
{
struct node *head = NULL;
struct node **head_ref = &head;
for ( struct node *current = first; current != NULL; current = current->next )
{
struct node *tmp = newNode( current->data, NULL );
if ( tmp != NULL )
{
*head_ref = tmp;
head_ref = &( *head_ref )->next;
}
}
for ( struct node *current = second; current != NULL; current = current->next )
{
if ( !find( first, current->data ) )
{
struct node *tmp = newNode( current->data, NULL );
if ( tmp != NULL )
{
*head_ref = tmp;
head_ref = &( *head_ref )->next;
}
}
}
return head;
}
void printList( struct node *node )
{
for ( ; node != NULL; node = node->next )
{
printf( "%d ", node->data );
}
printf("\n");
}
/* Drier program to test above function */
int main( void )
{
struct node *first = NULL;
struct node *second = NULL;
struct node *union2 = NULL;
// create first list 7->5->9->4->6
push( &first, 6 );
push( &first, 4 );
push( &first, 9 );
push( &first, 5 );
push( &first, 7 );
printf( "First list is: " );
printList( first );
push( &second, 6 );
push( &second, 4 );
push( &second, 9 );
push( &second, 11 );
push( &second, 12 );
printf( "second list is: " );
printList( second );
union2 = list_union( first, second );
printf( "Union of 2 lists is: " );
printList( union2 );
// Here you should call a function that frees all the memory allocated for the lists
}
The program output is
First list is: 7 5 9 4 6
second list is: 12 11 9 4 6
Union of 2 lists is: 7 5 9 4 6 12 11
Also you could write function push such a way that it would not add duplicated values to the list.

Resources