0% found this document useful (0 votes)
24 views11 pages

Insertion LL

Uploaded by

manishhadiabad70
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views11 pages

Insertion LL

Uploaded by

manishhadiabad70
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

// C Program to insert the node at the End of Linked List

#include <stdio.h>
#include <stdlib.h>

// A linked list node


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

// Function to create a new node


struct Node* createNode(int new_data) {
struct Node* new_node =
(struct Node*)malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = NULL;
return new_node;
}

// Given the head of a list and an int, appends


// a new node at the end and returns the head.
struct Node* append(struct Node* head, int new_data) {

// Create a new node


struct Node* new_node = createNode(new_data);
// If the Linked List is empty, make
// the new node as the head and return
if (head == NULL) {
return new_node;
}

// Store the head reference in a temporary variable


struct Node* last = head;

// Traverse till the last node


while (last->next != NULL) {
last = last->next;
}

// Change the next pointer of the last node


// to point to the new node
last->next = new_node;

// Return the head of the list


return head;
}

// This function prints the contents


// of the linked list starting from the head
void printList(struct Node* node) {
while (node != NULL) {
printf(" %d", node->data);
node = node->next;
}
}
// Driver code
int main()
{
// Create a hard-coded linked list:
// 2 -> 3 -> 4 -> 5 -> 6
struct Node* head = createNode(2);
head->next = createNode(3);
head->next->next = createNode(4);
head->next->next->next = createNode(5);
head->next->next->next->next = createNode(6);

printf("Created Linked list is:");


printList(head);

// Example of appending a node at the end


head = append(head, 1);

printf("\nAfter inserting 1 at the end:");


printList(head);
return 0;
}
// C Program to insert the node at the beginning of Linked
List
#include <stdio.h>
#include <stdlib.h>

// Define a node in the linked list


struct Node {
int data; // Data stored in the node
struct Node*
next; // Pointer to the next node in the list
};

// Function to create a new node with the given data


struct Node* createNode(int new_data)
{
// Allocate memory for a new node
struct Node* new_node
= (struct Node*)malloc(sizeof(struct Node));

// Initialize the node's data


new_node->data = new_data;

// Set the next pointer to NULL


new_node->next = NULL;
// Return the newly created node
return new_node;
}

// Function to insert a new node at the beginning of the


// list
struct Node* insertAtFront(struct Node* head, int new_data)
{
// Create a new node with the given data
struct Node* new_node = createNode(new_data);

// Make the next of the new node point to the current


// head
new_node->next = head;

// Return the new node as the new head of the list


return new_node;
}

// Function to print the contents of the linked list


void printList(struct Node* head)
{
// Start from the head of the list
struct Node* curr = head;

// Traverse the list


while (curr != NULL) {
// Print the current node's data
printf(" %d", curr->data);

// Move to the next node


curr = curr->next;
}

// Print a newline at the end


printf("\n");
}

// Driver code to test the functions


int main()
{
// Create the linked list 2->3->4->5
struct Node* head = createNode(2);
head->next = createNode(3);
head->next->next = createNode(4);
head->next->next->next = createNode(5);

// Print the original list


printf("Original Linked List:");
printList(head);

// Insert a new node at the front of the list


printf("After inserting Nodes at the front:");
int data = 1;
head = insertAtFront(head, data);

// Print the updated list


printList(head);

return 0;
}
// Write a program to insert a node after a given node

#include <stdio.h>
#include <stdlib.h>

// Definition of a linked list node


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

// Function to create and return a new node


struct Node* createNode(int data) {
struct Node* newNode
= (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}

// Function to insert a new node after a given node and


// return the head of modified list
struct Node* insertAfter(struct Node* head, int key,
int newData) {

// Initialize curr Pointer to head


struct Node* curr = head;

// Iterate over Linked List to find the key


while (curr != NULL) {
if (curr->data == key)
break;
curr = curr->next;
}

// if curr becomes NULL means, given key is not found in


// linked list
if (curr == NULL) {
printf("Node not found\n");
// Return the head of the original linked list
return head;
}

// Allocate new node and make the element to be inserted


// as a new node
struct Node* newNode
= (struct Node*)malloc(sizeof(struct Node));
newNode->data = newData;

// Set the next pointer of new node to the next pointer


// of given node
newNode->next = curr->next;
// Change the next pointer of given node to the new node
curr->next = newNode;

// Return the head of the modified linked list


return head;
}

// Function to print the linked list


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

// Driver code
int main() {

// Create the linked list 2 -> 3 -> 5 -> 6


struct Node* head = createNode(2);
head->next = createNode(3);
head->next->next = createNode(5);
head->next->next->next = createNode(6);
printf("Original Linked List: ");
printList(head);

// Key: Insert node after key


int key = 3, newData = 4;

// Insert a new node with data 4 after the node with data 3
head = insertAfter(head, key, newData);

printf("Linked List after insertion: ");


printList(head);

return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy