Linked List Program in C

typedef struct node{
    int value; //this is the value the node stores
    struct node *next; //this is the node the current node points to. this is how the nodes link
}node;

node *createNode(int val){
    node *newNode = malloc(sizeof(node));
    newNode->value = val;
    newNode->next = NULL;
    return newNode;
}

0
0
Awgiedawgie 440215 points

                                    // Node of the list
typedef struct node {
    int val;
    struct node * next;
} node_t;

0
0
4
6
Awgiedawgie 440215 points

                                    // https://github.com/davidemesso/LinkedListC for the full
// implementation of every basic function

typedef struct Node 
{
  // Void pointer content of this node (to provide multityping).
  void* data;

  // Points to the next Node in the list.   
  struct Node* next;  
} Node;

Node *llNewList(void* data, Node* next)
{
    Node* result = 
        (Node*)malloc(sizeof(Node));

    result->data          = data;
    result->next          = next;
    
    return result;
}

4 (6 Votes)
0
0
0
Phoenix Logan 186120 points

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

struct node {
   int data;
   int key;
   struct node *next;
};

struct node *head = NULL;
struct node *current = NULL;

//display the list
void printList() {
   struct node *ptr = head;
   printf("\n[ ");
	
   //start from the beginning
   while(ptr != NULL) {
      printf("(%d,%d) ",ptr->key,ptr->data);
      ptr = ptr->next;
   }
	
   printf(" ]");
}

//insert link at the first location
void insertFirst(int key, int data) {
   //create a link
   struct node *link = (struct node*) malloc(sizeof(struct node));
	
   link->key = key;
   link->data = data;
	
   //point it to old first node
   link->next = head;
	
   //point first to new first node
   head = link;
}

//delete first item
struct node* deleteFirst() {

   //save reference to first link
   struct node *tempLink = head;
	
   //mark next to first link as first 
   head = head->next;
	
   //return the deleted link
   return tempLink;
}

//is list empty
bool isEmpty() {
   return head == NULL;
}

int length() {
   int length = 0;
   struct node *current;
	
   for(current = head; current != NULL; current = current->next) {
      length++;
   }
	
   return length;
}

//find a link with given key
struct node* find(int key) {

   //start from the first link
   struct node* current = head;

   //if list is empty
   if(head == NULL) {
      return NULL;
   }

   //navigate through list
   while(current->key != key) {
	
      //if it is last node
      if(current->next == NULL) {
         return NULL;
      } else {
         //go to next link
         current = current->next;
      }
   }      
	
   //if data found, return the current Link
   return current;
}

//delete a link with given key
struct node* delete(int key) {

   //start from the first link
   struct node* current = head;
   struct node* previous = NULL;
	
   //if list is empty
   if(head == NULL) {
      return NULL;
   }

   //navigate through list
   while(current->key != key) {

      //if it is last node
      if(current->next == NULL) {
         return NULL;
      } else {
         //store reference to current link
         previous = current;
         //move to next link
         current = current->next;
      }
   }

   //found a match, update the link
   if(current == head) {
      //change first to point to next link
      head = head->next;
   } else {
      //bypass the current link
      previous->next = current->next;
   }    
	
   return current;
}

void sort() {

   int i, j, k, tempKey, tempData;
   struct node *current;
   struct node *next;
	
   int size = length();
   k = size ;
	
   for ( i = 0 ; i < size - 1 ; i++, k-- ) {
      current = head;
      next = head->next;
		
      for ( j = 1 ; j < k ; j++ ) {   

         if ( current->data > next->data ) {
            tempData = current->data;
            current->data = next->data;
            next->data = tempData;

            tempKey = current->key;
            current->key = next->key;
            next->key = tempKey;
         }
			
         current = current->next;
         next = next->next;
      }
   }   
}

void reverse(struct node** head_ref) {
   struct node* prev   = NULL;
   struct node* current = *head_ref;
   struct node* next;
	
   while (current != NULL) {
      next  = current->next;
      current->next = prev;   
      prev = current;
      current = next;
   }
	
   *head_ref = prev;
}

void main() {
   insertFirst(1,10);
   insertFirst(2,20);
   insertFirst(3,30);
   insertFirst(4,1);
   insertFirst(5,40);
   insertFirst(6,56); 

   printf("Original List: "); 
	
   //print list
   printList();

   while(!isEmpty()) {            
      struct node *temp = deleteFirst();
      printf("\nDeleted value:");
      printf("(%d,%d) ",temp->key,temp->data);
   }  
	
   printf("\nList after deleting all items: ");
   printList();
   insertFirst(1,10);
   insertFirst(2,20);
   insertFirst(3,30);
   insertFirst(4,1);
   insertFirst(5,40);
   insertFirst(6,56);
   
   printf("\nRestored List: ");
   printList();
   printf("\n");  

   struct node *foundLink = find(4);
	
   if(foundLink != NULL) {
      printf("Element found: ");
      printf("(%d,%d) ",foundLink->key,foundLink->data);
      printf("\n");  
   } else {
      printf("Element not found.");
   }

   delete(4);
   printf("List after deleting an item: ");
   printList();
   printf("\n");
   foundLink = find(4);
	
   if(foundLink != NULL) {
      printf("Element found: ");
      printf("(%d,%d) ",foundLink->key,foundLink->data);
      printf("\n");
   } else {
      printf("Element not found.");
   }
	
   printf("\n");
   sort();
	
   printf("List after sorting the data: ");
   printList();
	
   reverse(&head);
   printf("\nList after reversing the data: ");
   printList();
}

0
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
linkediliist in c how to implement a linked list in c create a singly linked list in c cheeg linked list in c implement a linked list using c creating linkedlist in C linked listr C creating a node in linked list c singly linked list example in c singly linked list program using c write a c program to implement singly linked list singly linked list program in data structure using c singly linked list in c example program linked lists c programming what does * do in linked list in c c code for linked list lista c program in linked list in c with all operations Singly linked lists in c linked list in c using function linked list create node in c basic singly linked list operations in c how to create a linked list with numbers in it c program structure linked list in c a simple program of linked list in c singly linked list program in c with explanation Linked List Operations implementation c use of a linked list in c basic operations in singly linked list in c basic operations in linked list in c Linked List c linked list of a linked list in c c programming list linked list display in c lifo linked list c Linked list in C program with all operations linked list programs in c singly linked list implementation in c display function in c to linked list program on linked list in c linked list in c example code linked lists of linked lists C displaying a linked list in c whats c list create a linked list in c algorithm for singly list syntax in c linked list used in c singly linked list application example in c c language basic linked list c linked list and examples what is linkedlist in c c Linked list class basic operations linked list c linked list basic operations in c create a node in linked list in c using lists in c program which uses linked list in c what is linked lists in c c program linked list address linked list on c list c library c liste structures and linked lists in c why are linked lists used in c c singly linked list example c singly linked list c program singly linked list c program to implement a singly linked list example of singly linked list in c linked list of linked lists in c linked list algorithm in c syntax of linked list in c all operations on linked list in c linked list creation in c creating singly linked list in c list c$ c progra how to do linked list c program how to create a link list linked list of linked lists c linked lists c vidual C linked list C C linked list working with linked list in c link list implementation in c programs on linkedlist in c linked list c programming c linked list how to use linked list iun c c program for a linked list implement linkedlist in c linked list types in c linklist in c implementing linkedlist in c how to create list in link list in c what are linked lists in c how to create a linked list in c with n nodes linked lists explained in c how to use list in c programming singly linked list in c program how to code a linked list in c A linked list in c where is linked list created in c Linking linked lists in c c program linked list with struct linked list in c programming linked ilist in c write a program to create a linked list in c program to create a linked list in c how linked list is implemented in c linkted list in c lists in c Program To Create And Display A Singly Linked List in c linked list class C implementation of linked list in c how to build a linked list c Singly linked list program in C linked list c library add linked list c linked list i n c C linked lists tutorial use linked lists c singly linked list in c c program linked list implementation linked list in c library create a linked list in c step by step C language lists List c[ how to create a linked list with struct in c language make and display a linked list in c creating a node in linked list in c why do we use linked list in c basic linked list program in c linked list tutorial in c how to use a linked list in c c programming in the linked list making linked list in c list of list in c c create and use linked list C$ list write a c program to create and display singly linked list linedlist c create new node in linked list in c understanding linked list tutorial in c for beginners c code linked lists c linked list explained linked list with embedded c c list -> linked list in c code display linked list in c create a linked list of 5 elements in c c code linked list c lista How to create a linked list in C using function create a linked list of n nodes in c creating linked list in c code to create linked list in c linkedlists in c creation of linked list in c linked list c example for linked list in c program for linked list operations in c create singly linked list in c how to add a node to a linked list in c c linked listy c stl linked list c.list c linked list operations does c have lists linked lists functions c c list structure C linked list all functions C linked list library C list library how to make a linked list of structures c how to create a node in linked list c linked list simple program in c linked list in c example list of c programming libraries linked list structures in c list c lista in c creating linked lists in c c creating a linked list create a linked list with all operations in c create a linked list in c linked lists implementation in c c list example how to learn linked list in c easily linked list creation program in c what is a linked list in c linked list declaration in c create linked list function in c c library list Learn linked lists in C define a linked list c how to do a list in C singly linked list operations in c linked list operations in c write a program in c to create and display singly linked list how to linked list in c linked lists c implementation list h in c to create linked list what are linked lists used for in C what does the LinkedList* list refer to C what is the use of linked list program in c creation of a linked list in c list en c easy linked list in c lists in c' linked list c language c£ list list c{ how to do a linked list in c linked list applications in c c list linked list in c[[ singly linked list easy example in c how to creat a globally linked list in c creat a generic list using a linked list in c uses of linked lists in c what is linked list in c c list. linked list in c program code how to create linked list in c c list implementation create a list in c c why use a linked list node list in c c start new linked list dynamic linked list in c code list c lsit c how to do list in c c linked lists c list of lists c a list of lists how to define list in c make a list C list chain in c linked list functions in c linked list source c struct linked list def in c how to define a list in c c program to create a linked list c node_t c list for define list in c linked list in c using structure doubly linked list in c full linked list program in c function to create a linked list in c create a list c create a linked list c IMPLEMENTATION OF LINK LIST list in c head c pionter list c code for creation of linked list list struct c define a list in c doubly linked list program in c use linked list in linked list c create a singly linked list in c how to define a linked list in c c nodes LINK LIST C how to make a linked list C inked list in c linked list c int c programming lists create linked list in c what is a list in c c stractures and lists dynamic lists in c linked list node c#$ linkend list c linked list example in c linear linke dlist in c create linked list in c loop create linked list next prev c linkedlist creation c#\ linkedlist creation c#] how to make a list c what is list in c how to program a dubbel linked list linked list using C simple linked list in c how to create head node in linked list link list c implementation linked list in c program make linked list exporting linked list in c how to make linkes lise c is struct is linked list singly linked list link list c inplementation stack adt using linked list in c list in c$ linked list program in c implementation of linkeed lists exemple linked list in c store data linked list c list in c programming linked list of nodes c lists tutorial node . c linked lists and how to use them c linked list program link list of struct c how to create linked list list up from list in c elements in a list c creating a linked list struct c how to use list in struct in c create node in c creat link list linked lists c examples how to create linked list c how to creat linked list c creating a linked list linked list c++ using structs linkedList explaination in c linked list using struct in c link list in c how to initialize a linked list how to make a linked list how to create a linked list c Declaring a Linked list : how to define node in c how to create a linked list for any data type what are linked lists in c and how to use them c lists how to read a llist in c create a linked list how to make a lsit in c user defined linked list in c implementation of singly linked list using array in c how to make linked list in c linked list creation linked list using array in c is linkedlist is Singly-linked List linear linked list in c link list titorial c using linked lists how to collect linked list pounter address link list program single link list node how do u make a linked list in C how to implement linked list in c node c programming how to form a linked list using struct create node c linked list implementation c linked list tutorial c linkedlist c listnode documentation c c linked list create node list structure in c how to create a linked list data structure linked list code linked list and structure inC create a doubly linked list in c store data from limked list in c how is a list node created in c c how to make a linked list c how to make a list implement list in c pointer node->next c make linked list in c linked list datastructure in c how to instantiate a linked list in C linked list c program singly linked list c with structs example implementing linked list in c how to use list in c how to build linked list c c linked list tutorial can you create a list in c linked list syntax in c how to implent linked lis in c c # list list funciont c list method in c simple linked list program in c implementung link list in c struct node in c liunked list in c syntax linked list implementation in c struct c linked list structure node in c struct node c how to make a list in c node* in c create list no node c listed list c how to use lists in c c list struct linked list i c implement a linked list in c c linked list notation list function C linked list struct c linekd list c linked structures in c creation of singly linked list in c what a chained list C linked list with pointers c linked list with struct list pointer create a node list c how to create a linked list in c linked list in c using pointers yout linked list in c using pointers creating a linked list in c with explanation linked list in data structure c C LANG LIST NEXTR how give list of data to a ink list in c declare a new struct node in c pointer and linked kist in c creating a linked list in c how to create a list in c making a linked list in c how to allocate a node in c how to make a list in c## linked list node c c struct for linked list linkedlist implementatino c node struct c how to make a list c~ syntax in node to create a node in the singly linked list c linkedlist methods Accessing linked list with [] in c linked list c implementation LINKED LIST C++' linked list c code implement linked list in c linked list implementation how to access linked list in c linkedlist with c create list in c nodes in c llinked lists c write a code for creation of linked list linked list using node struct and list struct in c linked list using node struct and list struct list c programming liked list in c linked list using node and list struct c C general purpose linked list how to create a node in c linked list code in C linkedi list strct c linked list syntax c c programmming list node Singly linked list c lists c linked list programs code for linked list in c linked lists c C node struct c programming linked list how to make linked lists in c ft_push_back_linked_list.c 42 struct first linked list tuto struct head linked list tuto how to parse a linked list in C how to create a list on c struct linked_list *next; lists in c c create a list make a list in c storing empty linked list in C linked lists code creating a list in c linked list in c contents list* list c c linked list implementation c linked list program linked lsit in c C node linkedlist in c llinked list in c create a linked list node in c how to use a list in c struct with a linked list c create linked list c create linked list linked list in c initialize linked list c function initliaze linkedlist c function npode in c linked lists in c using structure data type for linked list implementation struct List* next; c linked list example code create a node c c lists witch functions c linked list example struct list node c how to node in c programming how to create node in linked list in c create a linked list node using structures in c Linked list code linked list codes why pointer node variable is required in dynamic linked list linked list functions linked list create node c code for linked list singly linked list code in c linked list example c how to create a node in linked list go through linked list c what is a node in c create list of pointers of linked list c Geeksforgeeks linked list c node c how to make a list in c code how to make a linked list in c create linked list c c linkedlist c linked list linked list c node in c list in c c list list c
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source