Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
3.67/5 (3 votes)
See more:
Please modify the linked list as follows:
- assume list is homogeneous. Do not pass size for push method.
- For memory allocation and deallocation of the node data, the client will be responsible to implement a function for that and supply as a callback.

So, during insertion/deletion you call those callback functions.e.g., delete operation will take another param of function pointer type. And inside your delete function you will invoke the callback.

what I have implemented so far is given below

What I have tried:

C
// C program for generic linked list 
#include<stdio.h> 
#include<stdlib.h> 

/* A linked list node */
struct Node 
{ 
	// Any data type can be stored in this node 
	void *data; 

	struct Node *next; 
}; 

/* Function to add a node at the beginning of Linked List. 
This function expects a pointer to the data to be added 
and size of the data type */
void push(struct Node** head_ref, void *new_data, size_t data_size) 
{ 
	// Allocate memory for node 
	struct Node* new_node = (struct Node*)malloc(sizeof(struct Node)); 

	new_node->data = malloc(data_size); 
	new_node->next = (*head_ref); 

	// Copy contents of new_data to newly allocated memory. 
	// Assumption: char takes 1 byte. 
	int i; 
	for (i=0; i<data_size; i++) 
		*(char *)(new_node->data + i) = *(char *)(new_data + i); 

	// Change head pointer as new node is added at the beginning 
	(*head_ref) = new_node; 
} 

/* Function to print nodes in a given linked list. fpitr is used 
to access the function to be used for printing current node data. 
Note that different data types need different specifier in printf() */
void printList(struct Node *node, void (*fptr)(void *)) 
{ 
	while (node != NULL) 
	{ 
		(*fptr)(node->data); 
		node = node->next; 
	} 
} 

// Function to print an integer 
void printInt(void *n) 
{ 
printf(" %d", *(int *)n); 
} 

// Function to print a float 
void printFloat(void *f) 
{ 
printf(" %f", *(float *)f); 
} 

/* Driver program to test above function */
int main() 
{ 
	struct Node *start = NULL; 

	// Create and print an int linked list 
	unsigned int_size = sizeof(int); 
	int arr[] = {10, 20, 30, 40, 50}, i; 
	for (i=4; i>=0; i--) 
	push(&start, &arr[i], int_size); 
	printf("Created integer linked list is \n"); 
	printList(start, printInt); 

	// Create and print a float linked list 
	unsigned float_size = sizeof(float); 
	start = NULL; 
	float arr2[] = {10.1, 20.2, 30.3, 40.4, 50.5}; 
	for (i=4; i>=0; i--) 
	push(&start, &arr2[i], float_size); 
	printf("\n\nCreated float linked list is \n"); 
	printList(start, printFloat); 

	return 0; 
} 
Posted
Updated 15-Jan-21 11:07am
v2
Comments
Richard Deeming 15-Jan-21 10:47am    
You seem to have forgotten to ask a question.
Member 15046944 15-Jan-21 11:43am    
I don't wanna pass size as the function parameter
I want to implement a separate function for allocation/deallocation of node data
OriginalGriff 15-Jan-21 11:08am    
And?
What does it do that you didn't expect, or not do that you did?
What have you tried to do to find out why?
Are there any error messages, and if so, where and when? What did you do to make them happen?

This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with.
Use the "Improve question" widget to edit your question and provide better information.
Member 15046944 15-Jan-21 11:41am    
I have implemented the generic linked list with the following code given
Code has no errors but now I want to implement the following changes
1)Not passing the size in push function
2)for memory allocation and deallocation of the node data I want to implement a separate function
OriginalGriff 15-Jan-21 12:02pm    
Well if you are waiting for permission, consider it given.

If you are waiting for something else, you need to tell us what it is ...

1 solution

try
C
#include<stdio.h>
#include<stdlib.h>

struct Node
{
  void *data;

  struct Node *next;
};


typedef void * (CallBackForAllocation) (void *);


void push( struct Node  ** head, void *data, CallBackForAllocation cballoc)
{
  struct Node* node = (struct Node*)malloc(sizeof(struct Node));

  node->data = cballoc(data);
  node->next = (*head);

  (*head) = node;
}


void * cballoc_int(void * data)
{
  void * p = malloc(sizeof(int));
  *(int*)p = *(int *)(data);
  return p;
}


int main()
{
  struct Node *start = NULL;

  int arr[] = {10, 20, 30, 40, 50}, i;
  for (i=4; i>=0; i--)
    push(&start, &arr[i], cballoc_int);

  return 0;
}
 
Share this answer
 
Comments
jeron1 15-Jan-21 15:37pm    
:thumbsup: I quite enjoy looking at your take on these problems.
CPallini 15-Jan-21 16:50pm    
Thank you :-)
Rick York 15-Jan-21 16:36pm    
I have no idea why someone would give this a two but it's five from me. ;)
CPallini 15-Jan-21 16:50pm    
Thank you, Rick!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900