Deleting first occurrence in a doubly linked list - c

I have an assignment to do.. the task is to delete the first odd number only I wrote the code but honestly I have zero confidence about it.. if you guys can help me I would be pleased.
The function deletes the first node that has an odd value, so the function searches for the first node that has an odd value and deletes it.
typedef struct Node {
int data;
struct Node *next;
struct Node *previous;
}Node;
int DeleteFirstODD(Node **front) {
int oddnum;
Node *temp = *front;
if (*front == NULL) //Checking if the list is empty
return;
while (temp != NULL && temp->data % 2 == 0)
temp = temp->next;
if (temp == NULL)
return -1;
else if (temp == *front) { //if odd num founded # the begining of the doubly
linked list!
oddnum = (*front)->data;
*front = (*front)->next;
(*front)->previous = NULL;
free(temp);
}
else if (temp->next == NULL) { //if odd num founded # the end
oddnum = temp->data;
temp->previous->next = NULL;
free(temp);
}
else { // if the odd somewhere in the middle
temp->previous->next = NULL;
temp->next->previous = NULL;
free(temp);
}
return oddnum;
}

For starters this typedef declaration
typedef struct Node {
int data;
Node *next;
Node *previous;
}Node;
is incorrect. You have to write
typedef struct Node {
int data;
struct Node *next;
struct Node *previous;
} Node;
If a function has a return type that is not void then you may not use the return statement without an expression like in your function
return;
The function could return integer value 1 in the case when a node with an odd value was deleted or 0 otherwise.
The code within the function is incorrect. For example this loop
while (ptr != NULL && ptr % 2 != 0) {
oddnum = ptr->data;
ptr = ptr->next;
}
does not make a sense.
The function can be defined the following way
int DeleteFirstODD( Node **front )
{
while ( *front && ( *front )->data % 2 == 0 )
{
front = &( *front )->next;
}
int success = *front != NULL;
if ( success )
{
Node *current = *front;
if ( current->next )
{
current->next->previous = current->previous;
}
*front = current->next;
free( current );
}
return success;
}
Here is a demonstrative program.
#include <stdio.h>
#include <stdlib.h>
typedef struct Node
{
int data;
struct Node *next;
struct Node *previous;
} 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;
if ( *head ) ( *head )->previous = new_node;
new_node->previous = NULL;
*head = new_node;
}
return success;
}
void display( const Node *head )
{
for ( ; head; head= head->next )
{
printf( "%d -> ", head->data );
}
puts( "null" );
}
int DeleteFirstODD( Node **front )
{
while ( *front && ( *front )->data % 2 == 0 )
{
front = &( *front )->next;
}
int success = *front != NULL;
if ( success )
{
Node *current = *front;
if ( current->next )
{
current->next->previous = current->previous;
}
*front = current->next;
free( current );
}
return success;
}
int main(void)
{
Node *head = NULL;
const int N = 10;
for ( int i = N; i != 0; i-- )
{
push_front( &head, i );
}
display( head );
while ( DeleteFirstODD( &head ) )
{
display( head );
}
return 0;
}
The program output is
1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 10 -> null
2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 10 -> null
2 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 10 -> null
2 -> 4 -> 6 -> 7 -> 8 -> 9 -> 10 -> null
2 -> 4 -> 6 -> 8 -> 9 -> 10 -> null
2 -> 4 -> 6 -> 8 -> 10 -> null

Related

C rotate linked list by user choice

I wrote a program that get from a user numbers to linked list and number for how much to rotate each node in lust for the left. and I was only succeeded to do that but not in a circle. and my program need to be abale to move the node more left then the lenght of the list in circles.
someone know how can i fix my program??. (the function that need to be fixed is the "RotateALinkedList" function). i mean if the user want to move the list 4 times left the first node gonna start from the last node.
#include<stdio.h>
#include<stdlib.h>
typedef struct numbers_list
{
int data;
struct numbers_list* next;
}number;
void RotateALinkedList(number** head, int node); //the function that rotate the linked list
int CreateLinkedList(number** head, int iNumberofNode);
int attachToEnd(number** head, int k);
void PrintTheList(number* pNode);
void FreeAllocatedMemory(number** head);
int main(void)
{
int list_len = 0;
int data = 0;
number* head = NULL;
printf("How many nodes in list? ");
scanf("%d", &list_len);
getchar();
CreateLinkedList(&head, list_len);
printf("Choose a number k, and the list will be rotated k places to the left: ");
scanf("%d", &data);
getchar();
if (data <= list_len)
{
RotateALinkedList(&head, data);
PrintTheList(head);
}
else
{
printf("Please Enter Valid number of node\n");
}
FreeAllocatedMemory(&head);
getchar();
return 0;
}
void RotateALinkedList(number** head, int node)
{
int count = 0;
number* p = *head;
number* tempNode = NULL;
for (count = 1; ((count < node) && (p != NULL)); count++)
{
p = p->next;
}
if (p == NULL)
{
return;
}
else
{
tempNode = p;
}
while (p->next != NULL)
{
p = p->next;
}
p->next = *head;
*head = tempNode->next;
tempNode->next = NULL;
}
int CreateLinkedList(number** head, int iNumberofNode)
{
int data = 0;
int iRetValue = -1;
int count = 0;
number* pNewNode = NULL;
for (count = 0; count < iNumberofNode; count++)
{
printf("Enter number: ");
scanf("%d", &data);
getchar();
if ((*head) == NULL)
{
pNewNode = (number*)malloc(sizeof(number));
if (pNewNode != NULL)
{
pNewNode->data = data;
pNewNode->next = NULL;
*head = pNewNode;
iRetValue = 0;
}
}
else
{
iRetValue = attachToEnd(head, data);
}
}
return iRetValue;
}
int attachToEnd(number** head, int k)
{
int iRetValue = -1;
number* pLastNode = NULL;
number* pNewNode = NULL;
pLastNode = *head;
pNewNode = (number*)malloc(sizeof(number));
if (pNewNode != NULL)
{
pNewNode->data = k;
pNewNode->next = NULL;
iRetValue = 0;
}
if (pLastNode == NULL)
{
*head = pNewNode;
}
else
{
while (pLastNode->next != NULL)
{
pLastNode = pLastNode->next;
}
pLastNode->next = pNewNode;
}
return iRetValue;
}
void PrintTheList(number* pNode)
{
printf("the rotated list:\n");
while (pNode != NULL)
{
printf("%d ", pNode->data);
pNode = pNode->next;
}
}
void FreeAllocatedMemory(number** head)
{
number* ptempNode = NULL;
number* pFirstNode = NULL;
pFirstNode = *head;
while (pFirstNode != NULL)
{
ptempNode = pFirstNode;
pFirstNode = pFirstNode->next;
free(ptempNode);
}
*head = NULL;
}
For starters the name number of the alias in this typedef declaration
typedef struct numbers_list
{
int data;
struct numbers_list* next;
}number;
is confusing.
It will be better to define two structures instead
struct Node
{
int data;
struct Node *next;
};
struct List
{
size_t size;
struct Node *head;
};
and define in main a list like
struct List numbers = { .size = 0, .head = NULL };
You will simplify your task if the list contains a data member that specifies the number of nodes in the list.
The function CreateLinkedList should do one task: create a list from an array specified by the user. It should ask the user to enter numbers that will be stored in the list.
I can suggest the following solution shown in the demonstrative program below.
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node *next;
};
struct List
{
size_t size;
struct Node *head;
};
void clear( struct List *list )
{
while ( list->head )
{
struct Node *tmp = list->head;
list->head = list->head->next;
free( tmp );
}
list->size = 0;
}
size_t create( struct List *list, const int a[], size_t n )
{
clear( list );
for ( struct Node **current = &list->head;
n-- && ( *current = malloc( sizeof( struct Node ) ) ) != NULL; )
{
( *current )->data = *a++;
( *current )->next = NULL;
current = &( *current )->next;
++list->size;
}
return list->size;
}
FILE * display( const struct List *list, FILE *fp )
{
fprintf( fp, "There is/are %zu items: ", list->size );
for ( struct Node *current = list->head; current != NULL; current = current->next )
{
fprintf( fp, "%d -> ", current->data );
}
fputs( "null", fp );
return fp;
}
void rotate( struct List *list, size_t n )
{
if ( ( list->size != 0 ) && ( ( n %= list->size ) != 0 ) )
{
struct Node **current = &list->head;
while ( n-- ) current = &( *current )->next;
struct Node *tmp = list->head;
list->head = *current;
*current = NULL;
struct Node *last = list->head;
while ( last->next != NULL ) last = last->next;
last->next = tmp;
}
}
int main(void)
{
struct List numbers = { .size = 0, .head = NULL };
int a[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
create( &numbers, a, sizeof( a ) / sizeof( *a ) );
fputc( '\n', display( &numbers, stdout ) );
rotate( &numbers, 1 );
fputc( '\n', display( &numbers, stdout ) );
rotate( &numbers, 2 );
fputc( '\n', display( &numbers, stdout ) );
rotate( &numbers, 3 );
fputc( '\n', display( &numbers, stdout ) );
rotate( &numbers, 4 );
fputc( '\n', display( &numbers, stdout ) );
clear( &numbers );
return 0;
}
The program output is
There is/are 10 items: 0 -> 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> null
There is/are 10 items: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 0 -> null
There is/are 10 items: 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 0 -> 1 -> 2 -> null
There is/are 10 items: 6 -> 7 -> 8 -> 9 -> 0 -> 1 -> 2 -> 3 -> 4 -> 5 -> null
There is/are 10 items: 0 -> 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> null

Delete specific element linked list

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

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

Having a hard time creating a function for lists in C

I am trying to create a function delz() which deletes a given number from the end of the list, I tried recursively and with a while loop and I can't figure it out.
Example: [ 3 | next ] - [ 4 | next ] - [ 3 | next ] - [ 7 | next ] -----> [ 3 | next ] - [ 4 | next ] - [ 7 | next ]
list delz (int val, list l) {
if(l == NULL)
return NULL;
else {
list head = l;
list tmp = l;
list tail;
list temp;
while(l != NULL){
if(l->value == val) {
list tail = l->next;
head->next = tail;
}
temp = head;
head = tmp;
l = l->next;
}
return head;
}
}
typedef struct node {
int value;
struct node *next;
} node, *list;
The function will be simpler if to pass to the function the pointer to the head node by reference.
Here is a demonstrative program. I used my own singly-linked list definition because you did not show your own. Also it is a bad idea to introduce a typedef for a pointer type.
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int val;
struct Node *next;
};
void assign( struct Node **head, const int a[], size_t n )
{
if ( *head )
{
struct Node *tmp = *head;
*head = ( *head )->next;
free( tmp );
}
for ( size_t i = 0; i < n; i++ )
{
*head = malloc( sizeof( struct Node ) );
( *head )->val = a[i];
( *head )->next = NULL;
head = &( *head )->next;
}
}
void display( const struct Node *head )
{
for ( ; head != NULL; head = head->next )
{
printf( "%d -> ", head->val );
}
puts( "null" );
}
int remove_last( struct Node **head, int val )
{
struct Node **target = NULL;
for ( ; *head != NULL; head = &( *head )->next )
{
if ( ( *head )->val == val ) target = head;
}
int success = target != NULL;
if ( success )
{
struct Node *tmp = *target;
*target = ( *target )->next;
free( tmp );
}
return success;
}
int main(void)
{
int a[] = { 3, 4, 3, 7 };
const size_t N = sizeof( a ) / sizeof( *a );
struct Node *head = NULL;
assign( &head, a, N );
display( head );
int val = 3;
if ( remove_last( &head, val ) )
{
printf( "The last node with the value %d is removed.\n", val );
}
display( head );
return 0;
}
The program output is
3 -> 4 -> 3 -> 7 -> null
The last node with the value 3 is removed.
3 -> 4 -> 7 -> null

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

Resources