0% found this document useful (0 votes)
5 views

LinkedListprograms.docx

The document contains three C programs: the first implements a singly linked list with functions for insertion, deletion, and printing; the second counts the number of nodes in a singly linked list; and the third creates and displays a doubly linked list. Each program includes functions for creating nodes, managing the linked list, and displaying the results. The output examples demonstrate the functionality of each program.

Uploaded by

shraddhavinod1
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

LinkedListprograms.docx

The document contains three C programs: the first implements a singly linked list with functions for insertion, deletion, and printing; the second counts the number of nodes in a singly linked list; and the third creates and displays a doubly linked list. Each program includes functions for creating nodes, managing the linked list, and displaying the results. The output examples demonstrate the functionality of each program.

Uploaded by

shraddhavinod1
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

1)​ C Program to Implement Singly Linked List

// // C Program for Implementation of Singly Linked List


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

// Define the Node structure


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

// Function to create 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 element at the beginning of the singly linked list
void insertAtFirst(struct Node** head, int data) {
struct Node* newNode = createNode(data);
newNode->next = *head;
*head = newNode;
}

// Function to insert a new element at the end of the singly linked list
void insertAtEnd(struct Node** head, int data) {
struct Node* newNode = createNode(data);
if (*head == NULL) {
*head = newNode;
return;
}
struct Node* temp = *head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}

// Function to insert a new element at a specific position in the singly linked list
void insertAtPosition(struct Node** head, int data, int position) {
struct Node* newNode = createNode(data);
if (position == 0) {
insertAtFirst(head,data);
return;
}
struct Node* temp = *head;
for (int i = 0; temp != NULL && i < position - 1; i++) {
temp = temp->next;
}
if (temp == NULL) {
printf("Position out of range\n");
free(newNode);
return;
}
newNode->next = temp->next;
temp->next = newNode;
}

// Function to delete the first node of the singly linked list


void deleteFromFirst(struct Node** head) {
if (*head == NULL) {
printf("List is empty\n");
return;
}
struct Node* temp = *head;
*head = temp->next;
free(temp);
}

// Function to delete the last node of the singly linked list


void deleteFromEnd(struct Node** head) {
if (*head == NULL) {
printf("List is empty\n");
return;
}
struct Node* temp = *head;
if (temp->next == NULL) {
free(temp);
*head = NULL;
return;
}
while (temp->next->next != NULL) {
temp = temp->next;
}
free(temp->next);
temp->next = NULL;
}

// Function to delete a node at a specific position in the singly linked list


void deleteAtPosition(struct Node** head, int position) {
if (*head == NULL) {
printf("List is empty\n");
return;
}
struct Node* temp = *head;
if (position == 0) {
deleteFromFirst(head);
return;
}
for (int i = 0; temp != NULL && i < position - 1; i++) {
temp = temp->next;
}
if (temp == NULL || temp->next == NULL) {
printf("Position out of range\n");
return;
}
struct Node* next = temp->next->next;
free(temp->next);
temp->next = next;
}

// Function to print the LinkedList


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

// Driver Code
int main() {
struct Node* head = NULL;

insertAtFirst(&head, 10);
printf("Linked list after inserting the node:10 at the beginning \n");
print(head);

printf("Linked list after inserting the node:20 at the end \n");


insertAtEnd(&head, 20);
print(head);

printf("Linked list after inserting the node:5 at the end \n");


insertAtEnd(&head, 5);
print(head);

printf("Linked list after inserting the node:30 at the end \n");


insertAtEnd(&head, 30);
print(head);

printf("Linked list after inserting the node:15 at position 2 \n");


insertAtPosition(&head, 15, 2);
print(head);

printf("Linked list after deleting the first node: \n");


deleteFromFirst(&head);
print(head);

printf("Linked list after deleting the last node: \n");


deleteFromEnd(&head);
print(head);

printf("Linked list after deleting the node at position 1: \n");


deleteAtPosition(&head, 1);
print(head);

return 0;
}

Output:-
Output
Linked list after inserting the node:10 at the beginning ​
10 -> NULL​
Linked list after inserting the node:20 at the end ​
10 -> 20 -> NULL​
Linked list after inserting the node:5 at the end ​
10 -> 20 -> 5 -> NULL​
Linked list after inserting the node:30 at the end ​
10 -> 20 -> 5 -> 30 -> NULL​
Linked list after inserting the node:15 at position 2 ​
10 -> 20 -> 15 -> 5 -> 30 -> NULL​
Linked list after deleting the first node: ​
20 -> 15 -> 5 -> 30 -> NULL​
Linked list after deleting the last node: ​
20 -> 15 -> 5 -> NULL​
Linked list after deleting the node at position 1: ​
20 -> 5 -> NULL
2) C program to count number of nodes of Singly Linked List

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

/* Structure of a node */
struct node {
int data; // Data
struct node *next; // Address
}*head;

void createList(int n);


int countNodes();
void displayList();

int main()
{
int n, total;

/*
* Create a singly linked list of n nodes
*/
printf("Enter the total number of nodes: ");
scanf("%d", &n);
createList(n);

printf("\nData in the list \n");


displayList();

/* Count number of nodes in list */


total = countNodes();

printf("\nTotal number of nodes = %d\n", total);

return 0;
}

/*
* Create a list of n nodes
*/
void createList(int n)
{
struct node *newNode, *temp;
int data, i;

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

/*
* If unable to allocate memory for head node
*/
if(head == NULL)
{
printf("Unable to allocate memory.");
}
else
{
/*
* Read data of node from the user
*/
printf("Enter the data of node 1: ");
scanf("%d", &data);

head->data = data; // Link data field with data


head->next = NULL; // Link address field to NULL

temp = head;

/*
* Create n nodes and adds to linked list
*/
for(i=2; i<=n; i++)
{
newNode = (struct node *)malloc(sizeof(struct node));

/* If memory is not allocated for newNode */


if(newNode == NULL)
{
printf("Unable to allocate memory.");
break;
}
else
{
printf("Enter the data of node %d: ", i);
scanf("%d", &data);

newNode->data = data; // Link the data field of newNode with data


newNode->next = NULL; // Link the address field of newNode with NULL

temp->next = newNode; // Link previous node i.e. temp to the newNode


temp = temp->next;
}
}

printf("SINGLY LINKED LIST CREATED SUCCESSFULLY\n");


}
}

/*
* Counts total number of nodes in the list
*/
int countNodes()
{
int count = 0;
struct node *temp;

temp = head;

while(temp != NULL)
{
count++;
temp = temp->next;
}

return count;
}

/*
* Displays the entire list
*/
void displayList()
{
struct node *temp;

/*
* If the list is empty i.e. head = NULL
*/
if(head == NULL)
{
printf("List is empty.");
}
else
{
temp = head;
while(temp != NULL)
{
printf("Data = %d\n", temp->data); // Print data of current node
temp = temp->next; // Move to next node
}
}
}

Output:-
Enter the total number of nodes: 5
Enter the data of node 1: 10
Enter the data of node 2: 20
Enter the data of node 3: 30
Enter the data of node 4: 40
Enter the data of node 5: 50
SINGLY LINKED LIST CREATED SUCCESSFULLY

Data in the list


Data = 10
Data = 20
Data = 30
Data = 40
Data = 50

Total number of nodes = 5

3) Write a function to create and display doubly link list.

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

// Structure for a doubly linked list node


struct node {
int num;
struct node *preptr;
struct node *nextptr;
} *stnode, *ennode;

// Function prototypes
void DlListcreation(int n);
void displayDlList();

int main() {
int n;
stnode = NULL;
ennode = NULL;
printf("\n\n Doubly Linked List: Create and display a doubly linked list:\n");
printf("-------------------------------------------------------------------\n");
printf(" Input the number of nodes : ");
scanf("%d", &n);

DlListcreation(n); // Create a doubly linked list


displayDlList(); // Display the created doubly linked list
return 0;
}

// Function to create a doubly linked list


void DlListcreation(int n) {
int i, num;
struct node *fnNode;

if (n >= 1) {
stnode = (struct node *)malloc(sizeof(struct node)); // Allocate memory for the first node

if (stnode != NULL) {
printf(" Input data for node 1 : ");
scanf("%d", &num);

stnode->num = num;
stnode->preptr = NULL;
stnode->nextptr = NULL;
ennode = stnode; // Assign stnode as the last node

// Create nodes and link them to form the doubly linked list
for (i = 2; i <= n; i++) {
fnNode = (struct node *)malloc(sizeof(struct node));
if (fnNode != NULL) {
printf(" Input data for node %d : ", i);
scanf("%d", &num);
fnNode->num = num;
fnNode->preptr = ennode;
fnNode->nextptr = NULL;

ennode->nextptr = fnNode;
ennode = fnNode;
} else {
printf(" Memory can not be allocated.");
break;
}
}
} else {
printf(" Memory can not be allocated.");
}
}
}
// Function to display the doubly linked list
void displayDlList() {
struct node *tmp;
int n = 1;
if (stnode == NULL) {
printf(" No data found in the List yet.");
} else {
tmp = stnode;
printf("\n\n Data entered on the list are :\n");

// Traverse and display each node's data in the list


while (tmp != NULL) {
printf(" node %d : %d\n", n, tmp->num);
n++;
tmp = tmp->nextptr; // Move to the next node
}
}
}

Output:-
Doubly Linked List : Create and display a doubly linked list :
-------------------------------------------------------------------
Input the number of nodes : 3
Input data for node 1 : 2
Input data for node 2 : 5
Input data for node 3 : 8

Data entered on the list are :


node 1 : 2
node 2 : 5
node 3 : 8

You might also like