You are on page 1of 2

#include <stdio.

h>
#include <stdlib.h>

typedef struct Node{


int data;
struct Node *next;
}node;

void printList(node*);
node* createNode(int);
void insertNode(node**, int, int);

int main(){
node* head = createNode(1);
printf("Making 1st insert call \n");
insertNode(&head, 2, 0);
printf("Making 2nd insert call \n");
insertNode(&head, 4, 0);
printf("Making 3rd insert call \n");
insertNode(&head, 6, 0);
printf("Making 4th insert call \n");
insertNode(&head, 8, 0);
printf("Making 5th insert call \n");
insertNode(&head, 10, 0);
return 0;
}

void insertNode(node** head, int data, int position){


node* current = *head;
node* newNode = createNode(data);
if(position == 0){
newNode->next = current;
if(current->next == NULL){
current->next = newNode;
}else{
while(current->next != *head){
current = current->next;
}
current->next = newNode;
}
*head = newNode;
}
printList(*head);
}

node* createNode(int data){


node* temp = (node *)(malloc(sizeof(node)));
temp->data = data;
temp->next = NULL;
return temp;
}

void printList(node* head){


node* temp = head;
printf("%d -> ", temp->data);
temp = temp->next;
while(temp != head){
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("\n");
}

You might also like