Linked List CRUD Operations in C
How a linked list works: nodes linked through their data and nextKey values. Adding, deleting, updating and listing, step by step.
A linked list means data added as node variables, each with a data and a nextKey value, linked to each other to form a list. We can read this data directly in memory and run CRUD operations on it. In this post we'll do CRUD operations on linked lists in C++, and I'll share my sample app at the end.
Example layout:

The key holds the address of the next data, and this chain continues until it reaches a null value.
Before you start, include the following in your project.
#include #include #include #include To build the data & key pair in C++, we start by creating a struct-based node.
struct n{ int x; n * next;};typedef n node;We create a sample list and check for errors.
node * root; root = (node *)malloc(sizeof(node)); root->x = 500; root->next = NULL;We fill x as the data and next as the next key, and allocate space the size of our node with malloc.
We print the data by writing a reader method and running it in a console app. Note that the methods must be above the main method, otherwise it won't work and will throw an error.
void reader(node * r) { while (r!=NULL) { printf("%d ", r->x); r = r->next; }}Inside reader we loop until the list we passed returns null, printing x (the data) of the node r. Then, using next, we move r to the next item if there is one and print it; this repeats until r is null. For this to run, we need to call reader in main and pass it a list, so we'll use the root node created above. Write the following to see the output.
reader(root);Assuming you got the output, let's add 5 random values to the list and display them with the reader() method we wrote.
for (size_t i = 0; i < 5; i++) { int rnd = rand()%(100-50)+50; ekle(root, rnd); //sona ekleme } reader(root);We added 5 random numbers and printed them with reader.
Now, separately from the random numbers, let's write an add method that appends a node to the end of the list.
void add(node *r , int x) { while (r->next != NULL) { r = r->next; } r->next = (node *)malloc(sizeof(node)); r->next->x = x; r->next->next = NULL;}The add method takes two parameters, a node and an int. As you know, x is our data; when calling the method in main you can enter it by hand or write it in the code. I write it in the code:
ekle(rootYeni, 33);
We added 33 to the end of the list.
Now let's write the method that deletes an item from the list.
node* sil(node*r,int x) { node*temp; node* iter = r; if (r->x == x) { temp = r; r = r->next; } while (iter->next != NULL && iter->next->x != x) { iter = iter->next; } if (iter->next==NULL) { printf("sayı bulunamadı"); return r; } temp = iter->next; iter->next = iter->next->next; free(temp); return r;}The sil (delete) method takes the node list and the x value, the data of the item to delete. In the if block we check whether the first item in the list is the one the user wants to delete, because if we deleted the first item the normal way we would lose the start of the list. In the next if block we check whether the given value exists at all; if it doesn't, we print "number not found". At the bottom, we delete the found item with free().
To keep the post short, the last one I'll show is editing data by the index number the user picks. You can find examples of the other methods in the working project linked below.
void edit(node*r, int indexNo, int y) { node* temp = r; for (size_t i = 0; i < indexNo; i++) { temp = temp->next; } temp->x = y; }y is the value the user wants to set, r is our node list, and indexNo is the number of the index to change.
Thanks for reading, I hope it helps :) You can download the project I worked on below and go through the code.
The downloaded project also includes the following methods:
A method that deletes the first item
Printing the data recursively
Inserting data in the middle of the list
Copying a list
Deleting the last item
To download the project, click here
Comments 0
No comments yet. Be the first.
Leave a comment