Linked List C

This page will tell you about the basics of a linked list and the basic operations that can be carried out on a Linked List

#include<stdio.h>
#include<stdlib.h>
 
struct Node{
    int data;
    struct Node *next;
};
 
typedef struct Node NODE;
 
void createNode(NODE **head, int data){
    NODE *newnode,*currentnode;
    newnode=(NODE *)malloc(sizeof(NODE));
    newnode->data = data;    
    if(*head == NULL){
        *head = newnode;
        }
    else{
        currentnode=*head;
        while(currentnode->next != NULL)
            currentnode = currentnode->next;
    currentnode->next = newnode;        
    }
}
 
void printList(NODE **head){
    NODE *currentnode=*head;
    while(currentnode!=NULL){
    printf("%d\n",currentnode->data);
    currentnode=currentnode->next;
    }
 
}
 
NODE *reverseList(NODE **head){
    NODE *currentnode = *head;
    NODE *previousnode = NULL;
 
    while(currentnode != NULL){
        NODE *nextnode;
        nextnode = currentnode->next;
        currentnode->next = previousnode;
 
        previousnode = currentnode;
        currentnode = nextnode;
    }
    return previousnode;
}    
 
int main(){
 
NODE *head=NULL,*reverse=NULL;
createNode(&head,10);
createNode(&head,20);
createNode(&head,30);
createNode(&head,40);
printList(&head);
reverse = reverseList(&head);
printList(&reverse);
return 0;
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License