0% found this document useful (0 votes)
11 views16 pages

Linked List

The document contains multiple C code snippets for operations on singly and doubly linked lists, including adding, inserting, and deleting nodes. It defines structures for nodes and provides functions to manipulate the lists, along with example main functions demonstrating their usage. There are also some errors in the print function calls that need to be corrected.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views16 pages

Linked List

The document contains multiple C code snippets for operations on singly and doubly linked lists, including adding, inserting, and deleting nodes. It defines structures for nodes and provides functions to manipulate the lists, along with example main functions demonstrating their usage. There are also some errors in the print function calls that need to be corrected.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

write a c code to add a node in begining single linked list

#include <stdio.h>

#include <stdlib.h>

// Define the structure of a node

struct Node {

int data;

struct Node* next;

};

// Func on to add a node at the beginning

struct Node* addNodeAtBeginning(struct Node* head, int newData) {

// Allocate memory for the new node

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

// Set the data of the new node

newNode->data = newData;

// Make the new node point to the current head

newNode->next = head;

// Update the head to the new node

head = newNode;

return head;

// Func on to print the linked list

void printList(struct Node* head) {

struct Node* temp = head;


while (temp != NULL) {

prin ("%d -> ", temp->data);

temp = temp->next;

prin ("NULL\n");

int main() {

struct Node* head = NULL; // Ini alize an empty linked list

// Add nodes to the linked list

head = addNodeAtBeginning(head, 10);

head = addNodeAtBeginning(head, 20);

head = addNodeAtBeginning(head, 30);

// Print the linked list

prin ("Linked list: ");

printList(head);

return 0;

}
C code snippet to add a node at the beginning of a doubly linked list:

Copy

#include <stdio.h>

#include <stdlib.h>

// Define the structure of a node

struct Node {

int data;

struct Node* prev;

struct Node* next;

};

// Func on to add a node at the beginning

struct Node* addNodeAtBeginning(struct Node* head, int newData) {

// Allocate memory for the new node

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

// Set the data of the new node

newNode->data = newData;

newNode->prev = NULL; // New node will have no previous node

newNode->next = head; // New node points to the current head

// If the list is not empty, update the previous pointer of the exis ng head

if (head != NULL) {

head->prev = newNode;

// Update the head to the new node

head = newNode;
return head;

// Func on to print the doubly linked list

void printList(struct Node* head) {

struct Node* temp = head;

while (temp != NULL) {

prin ("%d <-> ", temp->data);

temp = temp->next;

prin ("NULL\n");

int main() {

struct Node* head = NULL; // Ini alize an empty doubly linked list

// Add nodes to the linked list

head = addNodeAtBeginning(head, 10);

head = addNodeAtBeginning(head, 20);

head = addNodeAtBeginning(head, 30);

// Print the linked list

prin ("Doubly linked list: ");

printList(head);

return 0;

}
C code snippet to add a node at the end of a doubly linked list:

#include <stdio.h>

#include <stdlib.h>

// Define the structure of a node

struct Node {

int data;

struct Node* prev;

struct Node* next;

};

// Func on to add a node at the end

struct Node* addNodeAtEnd(struct Node* head, int newData) {

// Allocate memory for the new node

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

// Set the data of the new node

newNode->data = newData;

newNode->next = NULL; // As it's the last node, next is NULL

// If the list is empty, the new node becomes the head

if (head == NULL) {

newNode->prev = NULL;

return newNode;

// Traverse to the last node

struct Node* temp = head;

while (temp->next != NULL) {

temp = temp->next;

}
// Update the last node's next to point to the new node

temp->next = newNode;

newNode->prev = temp;

return head;

// Func on to print the doubly linked list

void printList(struct Node* head) {

struct Node* temp = head;

while (temp != NULL) {

prin ("%d <-> ", temp->data);

temp = temp->next;

prin ("NULL\n");

int main() {

struct Node* head = NULL; // Ini alize an empty doubly linked list

// Add nodes to the linked list

head = addNodeAtEnd(head, 10);

head = addNodeAtEnd(head, 20);

head = addNodeAtEnd(head, 30);

// Print the linked list

prin ("Doubly linked list: ");

printList(head);

return 0;
}

Here’s a C code snippet to insert a node at the Kth posi on in a doubly linked list:

#include <stdio.h>

#include <stdlib.h>

// Define the structure of a node

struct Node {

int data;

struct Node* prev;

struct Node* next;

};

// Func on to insert a node at the Kth posi on

struct Node* insertAtKthPosi on(struct Node* head, int newData, int k) {

// Allocate memory for the new node

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->data = newData;

// If inser ng at the head (posi on 1)

if (k == 1) {

newNode->prev = NULL;

newNode->next = head;

if (head != NULL) {

head->prev = newNode;

return newNode; // New node becomes the head

}
// Traverse the list to find the (k-1)th node

struct Node* temp = head;

for (int i = 1; i < k - 1 && temp != NULL; i++) {

temp = temp->next;

// If (k-1)th node is NULL, posi on is invalid

if (temp == NULL) {

prin ("Invalid posi on\n");

free(newNode);

return head;

// Update pointers to insert the new node

newNode->next = temp->next;

newNode->prev = temp;

if (temp->next != NULL) {

temp->next->prev = newNode;

temp->next = newNode;

return head;

// Func on to print the doubly linked list

void printList(struct Node* head) {

struct Node* temp = head;

while (temp != NULL) {

prin ("%d <-> ", temp->data);


temp = temp->next;

prin ("NULL\n");

int main() {

struct Node* head = NULL; // Ini alize an empty doubly linked list

// Insert nodes at different posi ons

head = insertAtKthPosi on(head, 10, 1); // Insert 10 at posi on 1

head = insertAtKthPosi on(head, 20, 2); // Insert 20 at posi on 2

head = insertAtKthPosi on(head, 30, 2); // Insert 30 at posi on 2

head = insertAtKthPosi on(head, 40, 5); // A empt invalid posi on

// Print the linked list

prin ("Doubly linked list: ");

printList(head);

return 0;

a C code snippet to delete a node from a doubly linked list:

#include <stdio.h>

#include <stdlib.h>

// Define the structure of a node

struct Node {
int data;

struct Node* prev;

struct Node* next;

};

// Func on to delete a node at a specific posi on

struct Node* deleteAtPosi on(struct Node* head, int posi on) {

if (head == NULL) {

prin ("The list is empty.\n");

return head;

struct Node* temp = head;

// If dele ng the head node

if (posi on == 1) {

head = temp->next;

if (head != NULL) {

head->prev = NULL;

free(temp);

return head;

// Traverse to the node at the specified posi on

for (int i = 1; i < posi on && temp != NULL; i++) {

temp = temp->next;

// If the posi on is invalid

if (temp == NULL) {
prin ("Invalid posi on.\n");

return head;

// Update the pointers to remove the node

if (temp->next != NULL) {

temp->next->prev = temp->prev;

if (temp->prev != NULL) {

temp->prev->next = temp->next;

free(temp);

return head;

// Func on to print the doubly linked list

void printList(struct Node* head) {

struct Node* temp = head;

while (temp != NULL) {

prin ("%d <-> ", temp->data);

temp = temp->next;

prin ("NULL\n");

int main() {

struct Node* head = NULL; // Ini alize an empty doubly linked list

// Adding some nodes for demonstra on


head = deleteAtPosi on(head, 1); // Handle empty list case

return 0;

Here’s a collec on of essen al opera ons for working with a doubly linked list in C:

1. Adding a Node at the Beginning

Copy

struct Node* addNodeAtBeginning(struct Node* head, int newData) {

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->data = newData;

newNode->prev = NULL;

newNode->next = head;

if (head != NULL) {

head->prev = newNode;

return newNode; // Update head to the new node

2. Adding a Node at the End

Copy

struct Node* addNodeAtEnd(struct Node* head, int newData) {

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->data = newData;

newNode->next = NULL;

if (head == NULL) {
newNode->prev = NULL;

return newNode; // New node becomes the head

struct Node* temp = head;

while (temp->next != NULL) {

temp = temp->next;

temp->next = newNode;

newNode->prev = temp;

return head;

3. Inser ng a Node at a Kth Posi on

Copy

struct Node* insertAtKthPosi on(struct Node* head, int newData, int k) {

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->data = newData;

if (k == 1) {

newNode->prev = NULL;

newNode->next = head;

if (head != NULL) {

head->prev = newNode;

return newNode;

struct Node* temp = head;


for (int i = 1; i < k - 1 && temp != NULL; i++) {

temp = temp->next;

if (temp == NULL) {

prin ("Invalid posi on\n");

free(newNode);

return head;

newNode->next = temp->next;

newNode->prev = temp;

if (temp->next != NULL) {

temp->next->prev = newNode;

temp->next = newNode;

return head;

4. Dele ng a Node at a Specific Posi on

Copy

struct Node* deleteAtPosi on(struct Node* head, int posi on) {

if (head == NULL) {

prin ("The list is empty.\n");

return head;

struct Node* temp = head;


if (posi on == 1) {

head = temp->next;

if (head != NULL) {

head->prev = NULL;

free(temp);

return head;

for (int i = 1; i < posi on && temp != NULL; i++) {

temp = temp->next;

if (temp == NULL) {

prin ("Invalid posi on.\n");

return head;

if (temp->next != NULL) {

temp->next->prev = temp->prev;

if (temp->prev != NULL) {

temp->prev->next = temp->next;

free(temp);

return head;

5. Prin ng the List


c

Copy

void printList(struct Node* head) {

struct Node* temp = head;

while (temp != NULL) {

prin ("%d <-> ", temp->data);

temp = temp->next;

prin ("NULL\n");

Example Main Func on

Here’s how these opera ons can be u lized in a main func on:

Copy

int main() {

struct Node* head = NULL;

// Add nodes at different posi ons

head = addNodeAtBeginning(head, 10);

head = addNodeAtEnd(head, 20);

head = insertAtKthPosi on(head, 15, 2);

head = deleteAtPosi on(head, 3);

// Print the final doubly linked list

prin ("Doubly linked list: ");

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