# Searching Node in List

#### Kittu20

Joined Oct 12, 2022
238
I have written code for linked list. I want to make a function that can search any specific node in list

C:
//Linked list code

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

//structure for node in list
struct node
{
int data;                              // first member of node in list
struct node *next;                     // second member of node in list
};

// Add node to front of List
void insert ( struct node **current, int value)
{
struct node *temp = malloc( sizeof( struct node));
{
if ( temp != NULL )               // Memory allocated successfully
{
temp -> data = value;         // assign value to first member of node
temp -> next = *current;      // this member point to previous node in list
*current = temp;              // temporary node becomes current node in list
}
}
}

int main()
{

struct node *current = NULL;            // empty list
insert(&current, 1);                    // node 1 hold value 1 in list
printf("%d \n", current-> data);
insert(&current, 2);                    // node 2 hold value 2 in list
printf("%d \n", current-> data);
insert(&current, 3);                    // node 3 hold value 3 in list
printf("%d \n", current-> data);

return 0;
}
Code:
1
2
3
N3 -> N2 ->N1-> Null;

@WBahn

How to make a function to find node 1 in list ?

#### WBahn

Joined Mar 31, 2012
28,492
What do you mean by "node 1"? That usually refers to the first node in the list (or sometimes the second, if counting starts with zero). Do you mean search for the node whose data is equal to 1?

By they way, do you see the use of double pointers here? Does that use make sense?

Are you interested in seeing a cleaner way to implement your linked list (it's the same basic structure, just wrapped differently to aid abstraction).

#### Kittu20

Joined Oct 12, 2022
238
What do you mean by "node 1"? That usually refers to the first node in the list (or sometimes the second, if counting starts with zero). Do you mean search for the node whose data is equal to 1?
"node 1" Means first node in list. Yes I want to search for node whose data is equal to 1

By they way, do you see the use of double pointers here? Does that use make sense?
Double pointer store address of first pointer.

I wanted to update the value of the variable "current" through the function so I passed the address of the variable "current" to the function

Are you interested in seeing a cleaner way to implement your linked list (it's the same basic structure, just wrapped differently to aid abstraction).
Yes of course. I think my one drawback is that I didn't use typedef to define structure

#### WBahn

Joined Mar 31, 2012
28,492
"node 1" Means first node in list. Yes I want to search for node whose data is equal to 1
I think you confusing a couple of things.

The notion of which node is first in a list has no connection to which node what defined first, which is what you appear to be assuming.

Your function inserts a new node at the front of the list. THAT node BECOMES the first node in the list.

It's the same as if I have a list of items with each item written on an index card. When I give you the cards, you only know the order that they are in at that moment, you have no idea what order I wrote them in.

So your "current" pointer in main is always (or better always) point to the first node in the list. In essence, whatever node that current points to is, by definition, the first node in the list.

The node whose data is equal to 1 is actually the LAST node in your list, because you inserted the other two nodes in front of it.

Yes of course. I think my one drawback is that I didn't use typedef to define structure

I'm working on some stuff right now that is going to take up most of my time for the next day or two, so I may not get back to fleshing this out for a while.

#### Kittu20

Joined Oct 12, 2022
238
Thanks for clarifying my mistakes. I am stuck with following function. I don't know how to find last node in function

C:
void search ( struct node **current, int value)
{
struct node *Temp = *current;    // Temp hold memory location of first node in list
// here need code to find last node
}

Last edited:

#### WBahn

Joined Mar 31, 2012
28,492
Thanks for clarifying my mistakes. I am stuck with following function. I don't know how to find last node in function

C:
void search ( struct node **current, int value)
{
struct node *Temp = *current;    // Temp hold memory location of first node in list
// here need code to find last node
}
What is this function going to do? It's a void function, so it can't return anything. If it changes the value of current, then whoever called is at risk of losing touch with the rest of the list.

But, leaving that aside, what do you know about the last node in a list that makes it different from all of the other nodes in that list?

#### Kittu20

Joined Oct 12, 2022
238
What is this function going to do? It's a void function, so it can't return anything. If it changes the value of current, then whoever called is at risk of losing touch with the rest of the list.
I learned how to add a node to a list. now i want to learn how to remove node from list. so before i need to learn how to find the any node in the list

But, leaving that aside, what do you know about the last node in a list and makes it different from all of the other nodes in that list?
The second member of the first node points to the second node in list.

The second member of the second node points to the third node in list.

The second member of the third node points to the NULL value.

#### djsfantasi

Joined Apr 11, 2010
8,903
The second member of the first node points to the second node in list.

The second member of the second node points to the third node in list.

The second member of the third node points to the NULL value.
If you read what YOU wrote, I think you have answered your own question. The pointer in the last node in your list is NULL. Unless your definition of the last node is different…?

#### Kittu20

Joined Oct 12, 2022
238
The pointer in the last node in your list is NULL. Unless your definition of the last node is different…?
Are you referring to the definition of the last node given in post number #7? please correct it as i don't see anything wrong with it

Temp in search function hold the memory address of the first node in a list then how to set it to hold the memory address of the second node in the list.

#### WBahn

Joined Mar 31, 2012
28,492
I learned how to add a node to a list. now i want to learn how to remove node from list. so before i need to learn how to find the any node in the list

The second member of the first node points to the second node in list.

The second member of the second node points to the third node in list.

The second member of the third node points to the NULL value.
And so what is different about the second member of the last node in the list, relative to every other node in the list, that makes it real easy to see if the current node happens to be the last node in the list?

#### WBahn

Joined Mar 31, 2012
28,492
Are you referring to the definition of the last node given in post number #7? please correct it as i don't see anything wrong with it

Temp in search function hold the memory address of the first node in a list then how to set it to hold the memory address of the second node in the list.
It Temp points to the some node in the list, then isn't Temp->next the address of the next node in the list?

#### Kittu20

Joined Oct 12, 2022
238
It Temp points to the some node in the list, then isn't Temp->next the address of the next node in the list?
Temp = * current;
Temp stores the address of the first node

Temp = Temp->next;

Temp should stores the address of the second node.

how to set Temp so it point to the last node in list?

#### djsfantasi

Joined Apr 11, 2010
8,903
Temp = * current;
Temp stores the address of the first node

Temp = Temp->next;

Temp should stores the address of the second node.

how to set Temp so it point to the last node in list?
You have to go through the list node by node until you find the last node (when next=NUL). Then the last address is the address of the last node.

#### WBahn

Joined Mar 31, 2012
28,492
Temp = * current;
Temp stores the address of the first node

Temp = Temp->next;

Temp should stores the address of the second node.

how to set Temp so it point to the last node in list?
Let's say that the list has 414 nodes. You could walk down the list doing the following:

Temp = * current; // Node #1
Temp = Temp->next; // Node #2
Temp = Temp->next; // Node #3
Temp = Temp->next; // Node #4
...
Temp = Temp->next; // Node #412
Temp = Temp->next; // Node #413
Temp = Temp->next; // Node #414
Temp = Temp->next; // Node #415 (which doesn't exist)
Temp = Temp->next; // Node #416 (which doesn't exist)
...

What check could you perform before each of these assignments to determine if you want to do the assignment or not?

Could you wrap all of this up into a simple (a very simple) loop?

#### Kittu20

Joined Oct 12, 2022
238
Could you wrap all of this up into a simple (a very simple) loop?
Yes, now I can do
C:
//Linked list code

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

//structure for node in list
struct node
{
int data;                              // first member of node in list
struct node *next;                     // second member of node in list
};

// Add node to front of List
void insert ( struct node **current, int value)
{
struct node *temp = malloc( sizeof( struct node));
{
if ( temp != NULL )               // Memory allocated successfully
{
temp -> data = value;         // assign value to first member of node
temp -> next = *current;      // this member point to previous node in list
*current = temp;              // temporary node becomes current node in list
}
}
}

void List_Data ( struct node **current)
{
struct node *Temp = *current;         // Temp hold memory location of first node in list

while ( Temp != NULL)                // check List is not empty
{
printf("%d ", Temp-> data);      // print data value of node
Temp = Temp-> next;              // Temp point to next node in list
}

}

int main()
{

struct node *current = NULL;            // empty list

insert(&current, 1);

insert(&current, 2);

insert(&current, 3);

// Display List data
List_Data(&current);

return 0;
}
Program Output
Code:
3 2 1

Last edited:

#### Kittu20

Joined Oct 12, 2022
238
I am struggling to delete specific node in my code. I want to remove second node from list.

I am looking help in delete function.

C:
//Linked list code

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

//structure for node in list
struct node
{
int data;                              // first member of node in list
struct node *next;                     // second member of node in list
};

// Add node to front of List
void insert ( struct node **current, int value)
{
struct node *temp = malloc( sizeof( struct node));
{
if ( temp != NULL )               // Memory allocated successfully
{
temp -> data = value;         // assign value to first member of node
temp -> next = *current;      // this member point to previous node in list
*current = temp;              // temporary node becomes current node in list
}
}
}

void delete ( struct node **current, int value)
{
struct node *Temp = *current;         // Temp hold memory location of first node in list

while ( Temp != NULL)                // check List is not empty
{

}

}
void List_Data ( struct node **current)
{
struct node *Temp = *current;         // Temp hold memory location of first node in list

while ( Temp != NULL)                // check List is not empty
{
printf("%d ", Temp-> data);      // print data value of node
Temp = Temp-> next;              // Temp point to next node in list
}

}

int main()
{

struct node *current = NULL;            // empty list

insert(&current, 1);

insert(&current, 2);

insert(&current, 3);

insert(&current, 4);

// Display List data
List_Data(&current);

delete(&current, 3);                     // Remove second node from list

return 0;
}

#### WBahn

Joined Mar 31, 2012
28,492
I find that drawing a picture really helps. Use a circle to represent a node, an arrow pointing down to represent the data, and an arrow leaving the right side to represent the next pointer. Draw a picture of your at least three nodes in a list and then draw what you want it to look like after you delete the center one. Now make that happen. Then go back and do the same for the special cases of wanting to delete the first item in the list and then the last item in the list. Sometimes your approach for working with an interior node works fine on one end or the other, but frequently you have to treat them a bit differently.

#### Kittu20

Joined Oct 12, 2022
238
I find that drawing a picture really helps. Use a circle to represent a node, an arrow pointing down to represent the data, and an arrow leaving the right side to represent the next pointer. Draw a picture of your at least three nodes in a list and then draw what you want it to look like after you delete the center one.
I don't know the rules how to make diagram for linked list but I tried to make one as you said. If there is anything to be changed in this, I will gladly take your advice.

In the right hand diagram I have made three nodes and in the left hand diagram I am removing the middle node

#### Attachments

• 186.9 KB Views: 9

#### dl324

Joined Mar 30, 2015
15,536
In the right hand diagram I have made three nodes and in the left hand diagram I am removing the middle node
All you need to do is update the pointer to next in node 1 to point to node 3 and reclaim the memory used by node 2.