Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 103 additions & 0 deletions insertion_in_a_linked_list.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#include<stdio.h>
#include<stdlib.h>

struct Node
{
int data;
struct Node * next;
};

void linkedListTraversal(struct Node *ptr)
{
while(ptr != NULL){
printf("Element: %d\n", ptr->data);
ptr = ptr->next;
}
}

struct Node * insertAtFirst(struct Node *head, int data){
struct Node * ptr = (struct Node *)malloc(sizeof(struct Node));
ptr->next = head;
ptr->data = data;
return ptr;
}

struct Node * insertAfterNode(struct Node *head, struct Node * prevNode, int data){
struct Node * ptr = (struct Node *)malloc(sizeof(struct Node));
ptr->data = data;
ptr->next = prevNode->next;
prevNode->next = ptr;
return head;
}

struct Node * insertAtIndex(struct Node *head, int data, int index){
struct Node * ptr = (struct Node *)malloc(sizeof(struct Node));
struct Node * p = head;
int i = 0;

while (i != index-1)
{
p = p->next;
i++;
}
ptr->data = data;
ptr->next = p->next;
p->next = ptr;
return head;
}

struct Node * insertAtEnd(struct Node * head, int data){
struct Node * ptr = (struct Node *)malloc(sizeof(struct Node));
ptr->data = data;
struct Node * p = head;

while (p->next != NULL)
{
p = p->next;
}
p->next = ptr;
ptr->next = NULL;
return head;
}



int main(){
struct Node *head;
struct Node *second;
struct Node *third;
struct Node *fourth;

// Allocate memory for nodes in the linkedlist.
head = (struct Node*)malloc(sizeof(struct Node));
second = (struct Node*)malloc(sizeof(struct Node));
third = (struct Node*)malloc(sizeof(struct Node));
fourth = (struct Node*)malloc(sizeof(struct Node));

// Link first and second nodes
head->data = 7;
head->next = second;

// Link second and third nodes
second->data = 11;
second->next = third;

// Link third and fourth nodes
third->data = 41;
third->next = fourth;

// Terminate the list at the third node
fourth->data = 66;
fourth->next = NULL;

printf("Linked List before insertion\n");
linkedListTraversal(head);
printf("\n");
printf("Linked List after insertion\n");
// head = insertAtFirst(head, 56);
// head = insertAtIndex(head, 56, 2);
head = insertAtEnd(head, 56);
// head = insertAfterNode(head, second, 45);
linkedListTraversal(head);
return 0;
}