Skip to content

Instantly share code, notes, and snippets.

@jerweb63
Created June 2, 2016 13:09
Show Gist options
  • Save jerweb63/83dd2a0840bb4a8926c1bd508623aed5 to your computer and use it in GitHub Desktop.
Save jerweb63/83dd2a0840bb4a8926c1bd508623aed5 to your computer and use it in GitHub Desktop.
// linked list: inserting at the n'th position
#include "stdio.h"
#include "stdlib.h"
typedef struct Node
{
int data;
struct Node* next;
} Node;
Node* head;
void Insert(int data, int n)
{
Node* temp1 = malloc(sizeof(Node));
temp1->data = data;
temp1->next = NULL;
if(n==1) { // list is empty, set next to head, initially NULL.
temp1->next = head;
head = temp1;
return;
}
Node* temp2 = head;
for(int i = 0; i < n-2; i+=1) {
temp2 = temp2->next;
}
temp1->next = temp2->next;
temp2->next = temp1;
}
void Print() {
Node* temp = head;
while(temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
int main (int argc, char *argv[])
{
head = NULL;
Insert(2,1);
Insert(3,2);
Insert(4,1);
Insert(5,2);
Print();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment