Delete specific element linked list - c

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;
}
}

Related

Sorting a singly linked list with multiple elements in C [duplicate]

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;;
}

The list is not filled in

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

How do I swap the nodes of a linked list in a bubble-sort algorithm?

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;;
}

Delete a Node which is multiple of a given number with recursion [C programming]

I'm finding troubles trying to implement a function, this is what the program should do:
The user must first input an integer number (this number is not added to the list).
Then, I have to write a function which deletes recursively all the nodes in the list that are multiple of the input number.
This is my current code:
#include <stdio.h>
#include <stdlib.h>
#define true 1
#define false 0
#define bool int
typedef struct node {
int data;
struct node *next;
} Node;
void addToHead(Node **head, int value);
void printList(Node *head);
bool isMultipleOf(int value, int n);
void deleteMultipleOfNNodes(Node **head, int n);
int main() {
// Create head
Node *head = NULL;
int loop = true;
int input;
// The value whose multiples must be deleted from the list
int n;
scanf("%d", &n);
while (loop) {
scanf("%d", &input);
// End loop - print list
if (input < 0) {
deleteMultipleOfNNodes(&head, n);
printList(head);
loop = false;
} else {
// Add value to the head
addToHead(&head, input);
}
}
return 0;
}
void addToHead(Node **head, int value) {
Node *temp;
if (*head != NULL) {
// Create new node
Node *newNode = (Node*) malloc(sizeof(Node));
// Set new node data
newNode -> data = value;
// New node links to the head
newNode -> next = *head;
// New node is now the head of the list
*head = newNode;
} else {
// Create head
*head = (Node*) malloc(sizeof(Node));
// Set head data
(*head) -> data = value;
// Head links to NULL
(*head) -> next = NULL;
}
}
void printList(Node *head) {
Node *temp = head;
while (temp != NULL) {
if (temp -> next != NULL) {
printf("%d -> ", temp->data);
} else {
printf("%d -> NULL", temp -> data);
}
temp = temp->next;
}
}
bool isMultipleOf(int value, int n) {
// While the value is greater than zero, keep on subtracting the number
while (value > 0) {
value -= n;
}
return (value == 0);
}
void deleteMultipleOfNNodes(Node **head, int n) {
// ========= CODE ================
}
Thank you in advance for your help!
The function can look very simple
void deleteMultipleOfNNodes( Node **head, int n )
{
Node *tmp = *head;
if ( tmp != NULL )
{
tmp->data % n == 0 ? ( *head ) = ( *head )->next, free( tmp )
: ( void )( head = &( *head )->next );
deleteMultipleOfNNodes( head, n );
}
}
Pay attention to that this function
bool isMultipleOf(int value, int n) {
// While the value is greater than zero, keep on subtracting the number
while (value > 0) {
value -= n;
}
return (value == 0);
}
is invalid in general case because either value or n can be negative.
So define the function like
bool isMultipleOf( int value, int n )
{
return value % n == 0;
}
In this case the function above can be rewritten like
void deleteMultipleOfNNodes( Node **head, int n )
{
Node *tmp = *head;
if ( tmp != NULL )
{
isMultipleOf( tmp->data, n ) ? ( *head ) = ( *head )->next, free( tmp )
: ( void )( head = &( *head )->next );
deleteMultipleOfNNodes( head, n );
}
}
The function addToHead is too complicated. It can be written the following way
bool addToHead(Node **head, int value)
{
Node *newNode = malloc( sizeof( Node ) );
bool success = newNode != NULL;
if ( success )
{
newNode -> data = value;
newNode -> next = *head;
*head = newNode;
}
return success;
}
Here is a demonstrative program. It contains only those functions that are required to demonstrate the recursive function.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
typedef struct node {
int data;
struct node *next;
} Node;
bool addToHead(Node **head, int value)
{
Node *newNode = malloc( sizeof( Node ) );
bool success = newNode != NULL;
if ( success )
{
newNode -> data = value;
newNode -> next = *head;
*head = newNode;
}
return success;
}
bool isMultipleOf( int value, int n )
{
return value % n == 0;
}
void deleteMultipleOfNNodes( Node **head, int n )
{
Node *tmp = *head;
if ( tmp != NULL )
{
isMultipleOf( tmp->data, n ) ? ( *head ) = ( *head )->next, free( tmp )
: ( void )( head = &( *head )->next );
deleteMultipleOfNNodes( head, n );
}
}
void printList( const Node *head )
{
for ( ; head != NULL; head = head->next )
{
printf( "%d --> ", head->data );
}
puts( "NULL" );
}
int main(void)
{
Node *head = NULL;
const int N = 10;
for ( int i = N; i != 0; i-- )
{
addToHead( &head, i );
}
printList( head );
deleteMultipleOfNNodes( &head, 2 );
printList( head );
return 0;
}
Its output is
1 --> 2 --> 3 --> 4 --> 5 --> 6 --> 7 --> 8 --> 9 --> 10 --> NULL
1 --> 3 --> 5 --> 7 --> 9 --> NULL

how to connect a struct nodes(linked lists)?

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,&copyInt,&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,&copyNode,&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

Resources