0% found this document useful (0 votes)
14 views22 pages

Linked List Program

The document contains multiple C programs for operations on singly and doubly linked lists, including creation, traversal, insertion, deletion, and reverse traversal. Key functionalities include inserting nodes at various positions, deleting nodes from the beginning, end, or specific positions, and displaying the list. Additionally, it provides a method to find the length of a singly linked list and demonstrates the use of recursion for reverse traversal.

Uploaded by

hanvithcharan
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)
14 views22 pages

Linked List Program

The document contains multiple C programs for operations on singly and doubly linked lists, including creation, traversal, insertion, deletion, and reverse traversal. Key functionalities include inserting nodes at various positions, deleting nodes from the beginning, end, or specific positions, and displaying the list. Additionally, it provides a method to find the length of a singly linked list and demonstrates the use of recursion for reverse traversal.

Uploaded by

hanvithcharan
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

1.

Singly Linked List – Create, Traversal, Insertion Operations

#include <stdio.h>

#include <stdlib.h>

/* Structure definition */

struct node {

int data;

struct node *next;

};

struct node *head = NULL;

/* Function to create initial linked list */

void create(int n) {

struct node *newnode, *temp;

int i, value;

for (i = 1; i <= n; i++) {

newnode = (struct node *)malloc(sizeof(struct node));

printf("Enter data for node %d: ", i);

scanf("%d", &value);

newnode->data = value;

newnode->next = NULL;

if (head == NULL) {
head = newnode;

temp = head;

} else {

temp->next = newnode;

temp = newnode;

/* Function to display linked list */

void display() {

struct node *temp = head;

if (head == NULL) {

printf("List is empty\n");

return;

printf("Linked List: ");

while (temp != NULL) {

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

temp = temp->next;

printf("NULL\n");

/* Insert at beginning */
void insert_begin(int value) {

struct node *newnode;

newnode = (struct node *)malloc(sizeof(struct node));

newnode->data = value;

newnode->next = head;

head = newnode;

/* Insert at end */

void insert_end(int value) {

struct node *newnode, *temp;

newnode = (struct node *)malloc(sizeof(struct node));

newnode->data = value;

newnode->next = NULL;

if (head == NULL) {

head = newnode;

return;

temp = head;

while (temp->next != NULL)

temp = temp->next;

temp->next = newnode;
}

/* Insert at middle (after given position) */

void insert_middle(int value, int pos) {

struct node *newnode, *temp;

int i;

newnode = (struct node *)malloc(sizeof(struct node));

newnode->data = value;

temp = head;

for (i = 1; i < pos && temp != NULL; i++)

temp = temp->next;

if (temp == NULL) {

printf("Position not valid\n");

return;

newnode->next = temp->next;

temp->next = newnode;

/* Main function */

int main() {

int n;

printf("Enter number of nodes: ");

scanf("%d", &n);

create(n);

display();

insert_begin(10);

printf("\nAfter inserting 10 at beginning:\n");


display();

insert_middle(25, 2);

printf("\nAfter inserting 25 at middle (after position 2):\n");

display();

insert_end(50);

printf("\nAfter inserting 50 at end:\n");

display();

return 0;

Input: Enter number of nodes: 3

Enter data for node 1: 5

Enter data for node 2: 15

Enter data for node 3: 20

Output 1: Linked List: 5 -> 15 -> 20 -> NULL

Output 2: After inserting 10 at beginning:

Linked List: 10 -> 5 -> 15 -> 20 -> NULL

Output 3: After inserting 25 at middle (after position 2):

Linked List: 10 -> 5 -> 25 -> 15 -> 20 -> NULL

Output 4: After inserting 50 at end:

Linked List: 10 -> 5 -> 25 -> 15 -> 20 -> 50 -> NULL

2. Singly Linked List – Delete Operations


#include <stdio.h>

#include <stdlib.h>

// Definition of node

struct Node {

int data;
struct Node *next;

};

struct Node *head = NULL;

// Function to create list

void createList(int n) {

struct Node *temp, *newNode;

int i, data;

for (i = 0; i < n; i++) {

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

printf("Enter data for node %d: ", i + 1);

scanf("%d", &data);

newNode->data = data;

newNode->next = NULL;

if (head == NULL) {

head = newNode;

} else {

temp = head;

while (temp->next != NULL)

temp = temp->next;

temp->next = newNode;

}
}

// Display list

void display() {

struct Node *temp = head;

if (head == NULL) {

printf("List is empty\n");

return;

printf("Linked List: ");

while (temp != NULL) {

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

temp = temp->next;

printf("NULL\n");

// Delete from beginning

void deleteBeginning() {

struct Node *temp;

if (head == NULL) {

printf("List is empty\n");

return;

}
temp = head;

head = head->next;

free(temp);

printf("Node deleted from beginning\n");

// Delete from end

void deleteEnd() {

struct Node *temp, *prev;

if (head == NULL) {

printf("List is empty\n");

return;

if (head->next == NULL) {

free(head);

head = NULL;

printf("Node deleted from end\n");

return;

temp = head;

while (temp->next != NULL) {

prev = temp;
temp = temp->next;

prev->next = NULL;

free(temp);

printf("Node deleted from end\n");

// Delete from specific position

void deletePosition(int pos) {

struct Node *temp, *prev;

int i;

if (head == NULL) {

printf("List is empty\n");

return;

if (pos == 1) {

deleteBeginning();

return;

temp = head;

for (i = 1; i < pos && temp != NULL; i++) {

prev = temp;

temp = temp->next;
}

if (temp == NULL) {

printf("Invalid position\n");

return;

prev->next = temp->next;

free(temp);

printf("Node deleted from position %d\n", pos);

// Main function

int main() {

int n, pos;

printf("Enter number of nodes: ");

scanf("%d", &n);

createList(n);

display();

deleteBeginning();

display();

deleteEnd();

display();
printf("Enter position to delete: ");

scanf("%d", &pos);

deletePosition(pos);

display();

return 0;

Test Cases:

Input: Enter number of nodes: 5

Enter data for node 1: 10

Enter data for node 2: 20

Enter data for node 3: 30

Enter data for node 4: 40

Enter data for node 5: 50

Output:

Linked List: 10 -> 20 -> 30 -> 40 -> 50 -> NULL

Node deleted from beginning

Linked List: 20 -> 30 -> 40 -> 50 -> NULL

Node deleted from end

Linked List: 20 -> 30 -> 40 -> NULL

Enter position to delete: 2

Node deleted from position 2

Linked List: 20 -> 40 -> NULL


3. Program for reverse traversal of a singly linked list
#include <stdio.h>

#include <stdlib.h>

struct node {

int data;

struct node *next;

};

// Function to create linked list

struct node* createList(int n) {

struct node *head = NULL, *temp = NULL, *newNode;

int value;

for (int i = 0; i < n; i++) {

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

printf("Enter data for node %d: ", i + 1);

scanf("%d", &value);

newNode->data = value;

newNode->next = NULL;

if (head == NULL) {

head = newNode;

temp = head;

} else {

temp->next = newNode;
temp = newNode;

return head;

// Forward traversal

void display(struct node *head) {

printf("Forward Traversal: ");

while (head != NULL) {

printf("%d ", head->data);

head = head->next;

printf("\n");

// Reverse traversal using recursion

void reverseDisplay(struct node *head) {

if (head == NULL)

return;

reverseDisplay(head->next);

printf("%d ", head->data);

int main() {

struct node *head;


int n;

printf("Enter number of nodes: ");

scanf("%d", &n);

head = createList(n);

display(head);

printf("Reverse Traversal: ");

reverseDisplay(head);

return 0;

Output:

Input:

Enter number of nodes: 4

Enter data for node 1: 10

Enter data for node 2: 20

Enter data for node 3: 30

Enter data for node 4: 40

Output:

Forward Traversal: 10 20 30 40

Reverse Traversal: 40 30 20 10
4. C program to find the length (number of nodes) of a singly linked list
#include <stdio.h>

#include <stdlib.h>

struct node {

int data;

struct node *next;

};

int main() {

struct node *head = NULL, *temp, *newnode;

int count = 0, choice = 1;

/* Creating the linked list */

while (choice) {

newnode = (struct node *)malloc(sizeof(struct node));

printf("Enter data: ");

scanf("%d", &newnode->data);

newnode->next = NULL;

if (head == NULL) {

head = newnode;

} else {

temp = head;

while (temp->next != NULL) {


temp = temp->next;

temp->next = newnode;

printf("Do you want to continue (0/1): ");

scanf("%d", &choice);

/* Finding the length of the linked list */

temp = head;

while (temp != NULL) {

count++;

temp = temp->next;

printf("Length of the linked list = %d\n", count);

return 0;

Output:

Enter data: 10

Do you want to continue (0/1): 1

Enter data: 20

Do you want to continue (0/1): 1

Enter data: 30

Do you want to continue (0/1): 0


5. Doubly Linked List insertion
#include <stdio.h>

#include <stdlib.h>

struct node {

int data;

struct node *prev;

struct node *next;

};

struct node *head = NULL;

// Insert at beginning

void insert_beginning(int value) {

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

newNode->data = value;

newNode->prev = NULL;

newNode->next = head;

if (head != NULL)

head->prev = newNode;

head = newNode;

}
// Insert at end

void insert_end(int value) {

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

struct node *temp = head;

newNode->data = value;

newNode->next = NULL;

if (head == NULL) {

newNode->prev = NULL;

head = newNode;

return;

while (temp->next != NULL)

temp = temp->next;

temp->next = newNode;

newNode->prev = temp;

// Insert at specific position

void insert_position(int value, int pos) {

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

struct node *temp = head;

newNode->data = value;
for (int i = 1; i < pos - 1; i++) {

temp = temp->next;

if (temp == NULL) {

printf("Invalid position\n");

return;

newNode->next = temp->next;

newNode->prev = temp;

if (temp->next != NULL)

temp->next->prev = newNode;

temp->next = newNode;

// Display list

void display() {

struct node *temp = head;

printf("Doubly Linked List: ");

while (temp != NULL) {

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

temp = temp->next;

printf("\n");
}

int main() {

int choice, value, pos;

while (1) {

printf("\[Link] at Beginning");

printf("\[Link] at End");

printf("\[Link] at Position");

printf("\[Link]");

printf("\[Link]");

printf("\nEnter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter value: ");

scanf("%d", &value);

insert_beginning(value);

break;

case 2:

printf("Enter value: ");

scanf("%d", &value);

insert_end(value);

break;
case 3:

printf("Enter value and position: ");

scanf("%d %d", &value, &pos);

insert_position(value, pos);

break;

case 4:

display();

break;

case 5:

exit(0);

default:

printf("Invalid choice");

return 0;

Output:

[Link] at Beginning

[Link] at End

[Link] at Position

[Link]

[Link]

Enter your choice: 1

Enter value: 10
Enter your choice: 2

Enter value: 20

Enter your choice: 3

Enter value and position: 15 2

Enter your choice: 4

Doubly Linked List: 10 15 20

You might also like