C examples for Data Structure:Linked List
Insert element after linked list head
#include <stdio.h> struct Node//from w w w . j ava2 s. co m { int value; struct Node *next; }; void insertNode(struct Node *newNode, struct Node *existingNode) { newNode->next = existingNode->next; existingNode->next = newNode; } int main (void) { struct Node head, n1, n2, nE; struct Node *listPointer; head.next = &n1; n1.value = 100; n1.next = &n2; n2.value = 200; n2.next = (struct Node *) 0; // mark list end with null pointer listPointer = head.next; printf("Values in the list before insert of new Node: "); while ( listPointer != (struct Node *) 0 ) { printf ("%i ", listPointer->value); listPointer = listPointer->next; } printf("\n"); nE.value = 50; insertNode(&nE, &head); listPointer = head.next; printf("Values in the list after insert of new Node: "); while ( listPointer != (struct Node *) 0 ) { printf ("%i ", listPointer->value); listPointer = listPointer->next; } printf("\n"); return 0; }