I wrote a method that reverse the list, but as a result, the list remains empty. Help us understand what the problem is.
Method for reverse the list:
void reverseList(pLIST pL){
pNODE pN = pL->top;
pLIST pLReverse = createList();
while(pN){
pNODE pNew = malloc(sizeof(NODE));
pNew->value = pN->value;
if(!pLReverse->top){
pNew->next = NULL;
pLReverse->top = pNew;
}
else{
pNew->next = pLReverse->top;
pLReverse->top = pNew;
}
pN = pN->next;
}
showList(pLReverse);
}
The structure of the list:
typedef struct Node{
int value;
struct Node * next;
} NODE, *pNODE;
typedef struct List{
int len;
pNODE top;
} LIST, *pLIST;
Method for printing a list:
void showList(pLIST pL){
if(isEmpty(pL)) printf("Empty\n");
else{
pNODE temp = pL->top;
printf("Length: %d\n", pL->len);
while(temp){
printf("Pointer: %p\tValue: %d\tNext pointer: %p\n", temp, temp->value, temp->next);
temp = temp->next;
}
}
}
For starters it is a bad idea to introduce aliases for pointers like this
typedef struct List{
int len;
pNODE top;
} LIST, *pLIST;
Using such an alias you for example can not declare a pointer to constant list because this declaration
const pLIST list;
does not mean the same as
const struct List *list;
Instead it means
struct List * const list;
That is not what is required.
Taking into account this declaration
pLIST pLReverse = createList();
it seems that you are allocating lists dynamically. There is no need to do so. Lists can be declared as objects with the automatic storage duration.
The function reverseList should reverse the passed to it list itself not create a new list within the function. Moreover you have a memory leak because the created list pointed to by the pointer pLReverse is not freed.
Here is a demonstrative program that shows how the function reverseList can be defined.
#include <stdio.h>
#include <stdlib.h>
typedef struct Node
{
int value;
struct Node *next;
} Node;
typedef struct List
{
size_t len;
Node *head;
} List;
void init( List *list )
{
list->len = 0;
list->head = NULL;
}
int pushFront( List *list, int value )
{
Node *new_node = malloc( sizeof( Node ) );
int success = new_node != NULL;
if ( success )
{
new_node->value = value;
new_node->next = list->head;
list->head = new_node;
++list->len;
}
return success;
}
void showList( const List *list )
{
for ( Node *current = list->head; current != NULL; current = current->next )
{
printf( "%d -> ", current->value );
}
puts( "null" );
}
void reverseList( List *list )
{
Node *current = list->head;
list->head = NULL;
while ( current != NULL )
{
Node *new_node = current;
current = current->next;
new_node->next = list->head;
list->head = new_node;
}
}
void freeList( List *list )
{
while ( list->head != NULL )
{
Node *tmp = list->head;
list->head = list->head->next;
free( tmp );
}
}
int main(void)
{
List list;
init( &list );
const int N = 10;
for ( int i = 0; i < N; i++ )
{
pushFront( &list, i );
}
showList( &list );
reverseList( &list );
showList( &list );
freeList( &list );
return 0;
}
The program output is
9 -> 8 -> 7 -> 6 -> 5 -> 4 -> 3 -> 2 -> 1 -> 0 -> null
0 -> 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> null
Related
I would like to ask why this link list will not run the result. After running, it is TLE. I want the head to be an indicator, and the head list can be returned without modifying the head.
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2){
if(!list1 && !list2) return NULL;
struct ListNode *head;
struct ListNode *node = head;
while(list1 && list2){
if(list1->val < list2->val){
node->next = list1;
list1 = list1->next;
node = node->next;
}
else{
node->next = list2;
list2 = list2->next;
node = node->next;
}
}
if(list1){
while(list1){
node->next = list1;
list1 = list1->next;
node = node->next;
}
}
if(list2){
while(list2){
node->next = list2;
list2 = list2->next;
node = node->next;
}
}
return head->next;
}
The function has undefined behavior because the pointer head and correspondingly the pointer node are not initialized
struct ListNode *head;
struct ListNode *node = head;
while(list1 && list2){
if(list1->val < list2->val){
node->next = list1;
//...
To make this assignment correct
node->next = list1;
you need initially to define a dummy node and set the pointer node to the address of the node.
The while loops like this
while(list1){
node->next = list1;
list1 = list1->next;
node = node->next;
}
in fact are redundant. In fact it is enough to write
node->next = list1;
or
node->next = list2;
Also the function does not change the original pointers of the two lists passed to the function as arguments.
The function should be declared and defined the following way as shown in the demonstration program below.
#include <stdio.h>
#include <stdlib.h>
typedef struct ListNode
{
int val;
struct ListNode *next;
} ListNode;
void clear( ListNode **head )
{
while (*head)
{
ListNode *current = *head;
*head = ( *head )->next;
free( current );
}
}
size_t assign( ListNode **head, const int a[], size_t n )
{
clear( head );
size_t i = 0;
for (; i != n && ( *head = malloc( sizeof( ListNode ) ) ) != NULL; i++)
{
( *head )->val = a[i];
( *head )->next = NULL;
head = &( *head )->next;
}
return i;
}
FILE *display( const ListNode *const head, FILE *fp )
{
for (const ListNode *current = head; current != NULL; current = current->next)
{
fprintf( fp, "%d -> ", current->val );
}
fputs( "null", fp );
return fp;
}
struct ListNode *mergeTwoLists( struct ListNode **list1, struct ListNode **list2 )
{
struct ListNode *head = NULL;
struct ListNode **current = &head;
struct ListNode *head1 = *list1;
struct ListNode *head2 = *list2;
while ( head1 && head2 )
{
if ( head2->val < head1->val )
{
*current = head2;
head2 = head2->next;
}
else
{
*current = head1;
head1 = head1->next;
}
current = &( *current )->next;
}
if ( head1 )
{
*current = head1;
}
else if ( head2 )
{
*current = head2;
}
*list1 = NULL;
*list2 = NULL;
return head;
}
int main( void )
{
struct ListNode *head1 = NULL;
struct ListNode *head2 = NULL;
int a[] = { 0, 2, 4, 6, 8 };
assign( &head1, a, sizeof( a ) / sizeof( *a ) );
int b[] = { 1, 3, 5, 7, 9 };
assign( &head2, b, sizeof( b ) / sizeof( *b ) );
fputc( '\n', display( head1, stdout ) );
fputc( '\n', display( head2, stdout ) );
struct ListNode *head3 = mergeTwoLists( &head1, &head2 );
fputc( '\n', display( head3, stdout ) );
clear( &head3 );
}
The program output is
0 -> 2 -> 4 -> 6 -> 8 -> null
1 -> 3 -> 5 -> 7 -> 9 -> null
0 -> 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> null
#include <stdio.h>
#include <stdlib.h>
// A nested list node
struct ListNode
{
int val;
struct ListNode *next;
};
// Add a node
void add(struct ListNode ** head_ref, int new_val)
{
struct ListNode* new_node = (struct ListNode*) malloc(sizeof(struct ListNode));
new_node->val = new_val;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
// Merge nodes of linked list src into dest
void merge(struct ListNode *dest, struct ListNode **src)
{
struct ListNode *dest_curr = dest, *src_curr = *src;
struct ListNode *dest_next, *src_next;
// While there are available positions in dest
while (dest_curr != NULL && src_curr != NULL)
{
// Save next pointers
dest_next = dest_curr->next;
src_next = src_curr->next;
// Make src_curr as next of dest_curr
// Change next pointer of src_curr
src_curr->next = dest_next;
// Change next pointer of dest_curr
dest_curr->next = src_curr;
// Update current pointers for next iteration
dest_curr = dest_next;
src_curr = src_next;
}
// Update head pointer of second list
*src = src_curr;
}
// Print linked list
void print(struct ListNode *head)
{
struct ListNode *temp = head;
while (temp != NULL)
{
printf("%d ", temp->val);
temp = temp->next;
}
printf("\n");
}
int main()
{
struct ListNode *dest = NULL, *src = NULL;
add(&dest, 2);
add(&dest, 1);
printf("First List: ");
print(dest);
add(&src, 4);
add(&src, 3);
printf("Second List: ");
print(src);
merge(dest, &src);
printf("Merger Lists: ");
print(dest);
getchar();
return 0;
}
Output:
First List: 1 2
Second List: 3 4
Merger Lists: 1 3 2 4
I am trying to return the first node that contains the item which has a maximum value. Here is my code,
struct ListNode* CreateNode(int item) {
struct ListNode* Newnode = (struct
ListNode*)malloc(sizeof(struct ListNode)); //allocate new node
Newnode->item = item;
Newnode->next = NULL;
return Newnode;}
struct ListNode* searchmaxvalNode (struct ListNode *head) {
struct ListNode *cur = head;
int max = head->item;
while(cur != NULL){
if((cur->item) < max){
max = cur->item;
}
cur = cur->next;
return cur;
}
return NULL;
}
struct ListNode { //list.h file
int item;
struct ListNode *next;
};
what is wrong with my searchmaxvalnode function?
For starters in general the pointer head can be equal to NULL. So your function can invoke undefined behavior because it will try to access memory using a null pointer.
Also this if statement
if((cur->item) < max){
max = cur->item;
}
is incorrect and does not make a sense. In fact it selects a minimum value.
Also this return statement
return cur;
at least shall be placed outside the while loop though it also does not make a sense because it returns a pointer to a current node instead of the returning a pointer to the node with the maximum value.
The function can look the following way.
struct ListNode * searchmaxvalNode ( struct ListNode *head )
{
struct ListNode *max = head;
if ( head != NULL )
{
while ( ( head = head->next ) != NULL )
{
if ( max->item < head->item ) max = head;
}
}
return max;
}
Here is a demonstrative program.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
struct ListNode
{
int item;
struct ListNode *next;
};
struct ListNode * CreateNode( int item )
{
struct ListNode *Newnode = malloc( sizeof( struct ListNode ) );
if ( Newnode != NULL )
{
Newnode->item = item;
Newnode->next = NULL;
}
return Newnode;
}
int push_front( struct ListNode **head, int item )
{
struct ListNode *Newnode = CreateNode( item );
int success = Newnode != NULL;
if ( success )
{
Newnode->next = *head;
*head = Newnode;
}
return success;
}
void display( const struct ListNode *head )
{
for ( ; head != NULL; head = head->next )
{
printf( "%d -> ", head->item );
}
puts( "null" );
}
struct ListNode * searchmaxvalNode( struct ListNode *head )
{
struct ListNode *max = head;
if ( head != NULL )
{
while ( ( head = head->next ) != NULL )
{
if ( max->item < head->item ) max = head;
}
}
return max;
}
int main(void)
{
struct ListNode *head = NULL;
srand( ( unsigned int )time( NULL ) );
const int N = 10;
for ( int i = 0; i < N; i++ )
{
push_front( &head, rand() % N );
}
display( head );
struct ListNode *max = searchmaxvalNode( head );
printf( "The maximum value stored in the list is %d\n", max->item );
return 0;
}
The program output is
1 -> 8 -> 2 -> 1 -> 4 -> 6 -> 4 -> 5 -> 8 -> 4 -> null
The maximum value stored in the list is 8
In C I have to write a bubble sort function that swaps the nodes and not the values of a LinkedList, but I'm unable to accomplish it. Here is the code (As you can see the order is not correct):
#include <stdio.h>
#include <malloc.h> // malloc, free
#include <stddef.h> // NULL
// defining 'int' as data_t
typedef int data_t;
typedef struct node_s {
data_t data;
struct node_s* next;
} node_t;
node_t** list_new() {
node_t** list = malloc(sizeof **list);
if (!list) return NULL;
*list = NULL;
return list;
}
void list_delete(node_t** list) {
node_t *node, *next;
for (node = *list; node; node = next) {
next = node->next;
free(node);
}
}
node_t* list_push_front(node_t** list, data_t data) {
node_t* node = malloc(sizeof(node_t));
if (!node) return NULL;
node->data = data;
node->next = *list;
return *list = node;
}
// IS EASY TO SWAP THE VALUES
/*
void swap(data_t* a, data_t* b) {
data_t c = *a;
*a = *b;
*b = c;
}
void simple_bubble_sort(node_t** list) {
for(node_t* i = *list; i; i = i->next)
for(node_t* j = *list; j->next; j = j->next)
if(j->data > j->next->data)
swap(&(j->data), &(j->next->data));
}
*/
// MUCH LESS EASY TO SWAP THE NODES
void swap_node(node_t** prev_node_A, node_t** node_A, node_t** node_B) {
node_t *last_node = (*node_B)->next;
node_t *first_node = *node_A;
node_t *second_node = *node_B;
if (prev_node_A) {
(*prev_node_A)->next = second_node;
(*prev_node_A)->next->next = first_node;
(*prev_node_A)->next->next->next = last_node;
} else {
(*node_A) = second_node;
(*node_A)->next = first_node;
(*node_A)->next->next = last_node;
}
}
void simple_bubble_sort(node_t** list) {
for(node_t* i = *list; i->next; i = i->next)
for (node_t *j = *list; j->next->next; j = j->next) {
if (j == *list) {
if (j->data > j->next->data) {
*list = j->next;
swap_node(NULL, &j, &(j->next));
}
}
else {
if (j->next->data > j->next->next->data)
swap_node(&j, &(j->next), &(j->next->next));
}
//printf("%i,%i | %i, %i, %i, %i \n", i->data, j->data, (*list)->data, (*list)->next->data, (*list)->next->next->data, (*list)->next->next->next->data);
//system("pause");
}
}
int main() {
// Create List
node_t** list = list_new();
if (!list)
goto memory_allocation_failure;
// Add values to List
for(int i=0; i<10; i++)
if (!list_push_front(list, i))
goto memory_allocation_failure;
// Print List
for (node_t* node = *list; node != NULL; node = node->next)
printf("%i\n", node->data);
// Swap Test
//swap_node(NULL, &(*list), &((*list)->next));
//swap_node(&(*list), &((*list)->next), &((*list)->next->next));
// Sort List
printf("-- Bubble Sort --\n");
simple_bubble_sort(list);
// Print List
for (node_t* node = *list; node != NULL; node = node->next)
printf("%i\n", node->data);
// Delete List
list_delete(list);
return 0;
// Error Handler
memory_allocation_failure:
printf("Memory Allocation Failure");
return 1;
}
Here is the function swap.
void swap( node_t **current )
{
node_t *tmp = ( *current )->next->next;
( *current )->next->next = *current;
*current = ( *current )->next;
( *current )->next->next = tmp;
}
And here is the function simple_bubble_sort
void simple_bubble_sort( node_t **head )
{
if ( *head )
{
for ( node_t **first = head, *sorted = NULL, *last = sorted;
( *first )->next != last;
last = sorted )
{
sorted = ( *first )->next;
for ( node_t **current = first; ( *current )->next != last; current = &( *current )->next )
{
if ( ( *current )->next->data < ( *current )->data )
{
swap( current );
sorted = ( *current )->next;
}
}
}
}
}
Investigate them.
Pay attention to that the header <malloc.h> is not a standard C header. Instead use the header <stdlib.h>.
You need to revise your current code. For example this function
node_t** list_new() {
node_t** list = malloc(sizeof **list);
if (!list) return NULL;
*list = NULL;
return list;
}
does not make sense it should be removed.
What you need is just to define in main a pointer like
node_t *head = NULL;
And pass it to functions as for example to the function simple_bubble_sort like
simple_bubble_sort( &head );
Or the function list_push_front should be defined like
int list_push_front(node_t** list, data_t data)
{
node_t* node = malloc(sizeof(node_t));
int success = node != NULL;
if ( success )
{
node->data = data;
node->next = *list;
*list = node;
}
return success;;
}
Can you help me to understand why this function doesn't delete a specific element in the linked list? What am I doing wrong?
typedef struct str_node{
int data;
struct str_node *next;
}node;
...
node *head;
head = malloc(sizeof(node));
...
void delete_spec(node *a){
int num;
node *tmp;
tmp = a;
printf("Insert number to eliminate: ");
scanf("%d",&num);
while(tmp!=NULL){
if(tmp->data == num){
tmp = tmp->next->next;
}
tmp = tmp->next;
}
}
For starters it is unclear what the allocation is doing here
node *head;
head = malloc(sizeof(node));
The pointer head shall be initially set to NULL
node *head = NULL;
and new nodes shall be inserted in the list by the function that inserts new values in the list.
The function that deletes nodes from the list shall not issue any prompt. It is the caller of the function that will ask the user to specify the value that will be deleted from the list and then call the function passing the specified value. So the function should have two parameters: pointer to the pointer to the head node and the integer value that shall be deleted from the list.
The function can be defined the following way
void delete_spec( node **head, int data )
{
while ( *head != NULL )
{
if ( ( *head )->data == data )
{
node *tmp = *head;
*head = ( *head )->next;
free( tmp );
}
else
{
head = &( *head )->next;
}
}
}
Here is a demonstrative program.
#include <stdio.h>
#include <stdlib.h>
typedef struct str_node
{
int data;
struct str_node *next;
} node;
int push_front( node **head, int data )
{
node *new_node = malloc( sizeof( node ) );
int success = new_node != NULL;
if ( success )
{
new_node->data = data;
new_node->next = *head;
*head = new_node;
}
return success;
}
void delete_spec( node **head, int data )
{
while ( *head != NULL )
{
if ( ( *head )->data == data )
{
node *tmp = *head;
*head = ( *head )->next;
free( tmp );
}
else
{
head = &( *head )->next;
}
}
}
void display( node *head )
{
for ( ; head != NULL; head = head->next )
{
printf( "%d -> ", head->data );
}
puts( "null" );
}
int main(void)
{
node *head = NULL;
int a[] = { 1, 3, 5, 7, 1, 2, 3, 1 };
const size_t N = sizeof( a ) / sizeof( *a );
for ( size_t i = 0; i < N; i++ )
{
push_front( &head, a[i] );
}
display( head );
delete_spec( &head, 1 );
display( head );
return 0;
}
Its output is
1 -> 3 -> 2 -> 1 -> 7 -> 5 -> 3 -> 1 -> null
3 -> 2 -> 7 -> 5 -> 3 -> null
public class LL5 {
Node head;
int size;
LL5(){
this.size = 0;
}
class Node{
String data;
Node next;
Node(String data){
this.data = data;
this.next = null;
size++;
}
}
public void push(String data) {
Node newNode = new Node(data);
if(head == null) {
head = newNode;
return;
}
newNode.next = head;
head = newNode;
}
public void deleteelement(String data) {
Node cur = head;
Node x = cur.next;
Node y = x.next;
if(data == head.data) {
head = head.next;
}
else if(x.data == data) {
head.next = y;
}
else{
while(y.data != data) {
x = x.next;
y = y.next;
}
x.next = y.next;
}
}
public void printList() {
if(head == null) {
System.out.println("list is empty");
return;
}
Node cn = head;
while(cn != null) {
System.out.print(cn.data + " -> ");
cn = cn.next;
}
System.out.print("null");
}
public static void main(String[] args) {
LL5 obj = new LL5();
obj.push("1");
obj.push("2");
obj.push("3");
obj.push("4");
obj.push("5");
obj.push("6");
obj.push("7");
obj.push("8");
obj.push("9");
obj.push("10");
System.out.println("before delete");
obj.printList();
System.out.println();
System.out.println("After delete");
obj.deleteelement("10");
obj.printList();
}
}
delete_spec does not modify in anyway the input list. also: it does not free any memory.
in order to actually delete a node you must:
1. free its memory.
2. modify the list so the "next" pointers are updated. in order to update the list, you must provide the delete function the address of the head so it can modify also head.
something like this:
void delete_spec(node **a){
int num;
node *tmp;
if (a == NULL || *a == NULL) return;
tmp = *a;
printf("Insert number to eliminate: ");
scanf("%d",&num);
if (tmp->data == num)
{
*a = (*a)->next;
free(tmp);
return;
}
while(tmp->next!=NULL){
if(tmp->next->data == num){
node* tmp2 = tmp->next;
tmp->next = tmp->next->next;
free(tmp2);
}
tmp = tmp->next;
}
}
i want to make a new node that is a copy of Node_1 connected to Node_2
, the problem is that i need to choose elemenets in each node that
accept a specific condition thhat i insert in the connection function
. for example if i have two nodes that i want to connect to each other
(the second one at the end of the first one) , but i want to chose the
elements in each node that are for example odd ! (for example : first
linked list has the following elements(1 2 3 ) , and the second linked
list has the following elements (4 5 6) then i want to have a new linked list >that has the following elements : (1 3 5)
now my main problem is that i need to work with pointers to
functions because each time i want to give the function different
conditions .
i wrote this function with the assumption that i have a
ConditionFunction, but actually i an kinda stuck on how to make a
ConditionFunction in the main function that can actually do what i
want :/ (for example linke only the odd numbers)
i wrote this function to connect the two linked lists :
// the struct:
typedef struct node_t* Node;
struct node_t {
Element element;
Node next;
};
// ConditionNode a pointer to a function that has condition
// CopyNode a pointer to a function that copy's the node
Node concatLists(Node Node_1,Node Node_2,ConditionNode ConditionFunction,CopyNode copyFunction,void* condition){
Node currentNode=NULL;
Node* New_Node=NULL;
Node head=NULL;
while(Node_1!=NULL){
if(ConditionFunction(Node_1->element,condition)==0){
Node_Result=create_node(&New_Node);
if(head==NULL){
head=New_Node;
currentNode=New_Node;
}
currentNode->next=New_Node;
currentNode=New_Node;
Node_1=GetNextNode(Node_1);
}
else{
Node_1=GetNextNode(Node_1);
}
}
while(Node_2!=NULL){
if(CmpFunction(Node_2->element,condition)!=0){
if(head==NULL){
head=New_Node;
currentNode=New_Node;
}
currentNode->next=New_Node;
currentNode=New_Node;
Node_2=GetNextNode(Node_2);
} else {
Node_1=GetNextNode(Node_1);
}
}
return head;
}
Node_Result create_node(Node* CreatedNode) {
Node newNode = malloc(sizeof(*newNode));
if(!newNode) {
return MEM_PROBLEM;
}
newNode->element =0;
newNode->next = NULL;
*CreatedNode=newNode;
return NODE_SUCCESS;
}
Node GetNextNode(Node node){
if(node==NULL){
return NULL;
}
return node->next;
}
i wrote an example but i think it is wrong :\
int main(){
int array_1[3]={1,2,3};
int array_2[4]={4,5,6,7};
Node head_1=createAllNode(array_1,3);
Node head_2=createAllNode(array_2,4);
int num=2;
Node oddhead=concatLists(head_1,head_2,©Int,&checkIfOdd,&num);
printIntElements(oddhead);
return 0;
}
static Node createAllNode(int* array,int len){
Node head;
Node_Result result=create_node(&head);
if(result!=NODE_SUCCESS){
return NULL;
}
Node new_node=NULL;
int j=0;
while(len){
/*int *num=malloc(sizeof(*num));
if(num==NULL){
return NULL;
} */
int element=array[j];
head->element=*(int *)element;
if(j != len-1){
result=create_node(&new_node);
}
if(Node_Result!=NODE_SUCCESS){
return NULL;
}
head->next=new_node;
head=new_node;
new_node=GetNextNode(new_node);
j++;
len--;
}
return head;
}
static void* copyInt(void* num){
int* newInt=malloc(sizeof(*newInt));
*newInt=*(int*)num;
return newInt;
}
/*
static bool PrimaryIntNode(void*num1,void* num2){
}
*/
static void printIntElements(Node head){
while(head!=NULL){
printf("%d",(int*) head->element);
head=GetNextNode(head);
}
}
static bool checkIfOdd(Element num1,int num2){
int num=*(int *)num1;
if(num<0){
num *=-1;
}
return num % num2 != 0;
}
and i call the coonect list function like this in the main function :
Node oddhead=concatLists(head_1,head_2,©Node,&checkifIdd,&num);
can anyone just show me a correct example oh how actually use a
function like this in main !! because i get all kinda of errors in
eclipse ..
I will not use your definitions because they are confusing. I will just demonstrate how two lists can be concatenated by selecting nodes that satisfy some condition. You can use the presented demonstrative program as a base for your own list implementation.
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
void insert( struct node **head, const int a[], size_t n )
{
if ( *head != NULL ) head = &( *head )->next;
for ( size_t i = 0; i < n; i++ )
{
struct node *tmp = malloc( sizeof( struct node ) );
tmp->data = a[i];
tmp->next = *head;
*head = tmp;
head = &( *head )->next;
}
}
struct node * concatLists( struct node *head1, struct node *head2, int cmp( struct node * ) )
{
struct node *head = NULL;
struct node **current = &head;
for ( ; head1 != NULL; head1 = head1->next )
{
if ( cmp( head1 ) )
{
*current = malloc( sizeof( struct node ) );
( *current )->data = head1->data;
( *current )->next = NULL;
current = &( *current )->next;
}
}
for ( ; head2 != NULL; head2 = head2->next )
{
if ( cmp( head2 ) )
{
*current = malloc( sizeof( struct node ) );
( *current )->data = head2->data;
( *current )->next = NULL;
current = &( *current )->next;
}
}
return head;
}
void output( struct node *head )
{
for ( ; head != NULL; head= head->next )
{
printf( "%d ", head->data );
}
}
int odd( struct node *n )
{
return n->data % 2;
}
int main( void )
{
struct node *head1 = NULL;
struct node *head2 = NULL;
int a1[] = { 1, 2, 3 };
int a2[] = { 4, 5, 6 };
const size_t N1 = sizeof( a1 ) / sizeof( *a1 );
const size_t N2 = sizeof( a2 ) / sizeof( *a2 );
insert( &head1, a1, N1 );
insert( &head2, a2, N2 );
struct node *head3 = concatLists( head1, head2, odd );
output( head1 );
putchar( '\n' );
output( head2 );
putchar( '\n' );
output( head3 );
putchar( '\n' );
return 0;
}
The program output is
1 2 3
4 5 6
1 3 5