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

Insertelement

The document contains C programs for various array and linked list operations, including insertion, deletion, merging, and traversal. It demonstrates how to manipulate both singly and doubly linked lists, providing functions for creating nodes, counting nodes, and deleting all nodes. The main function showcases examples of these operations in action.

Uploaded by

foheke6586
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

Insertelement

The document contains C programs for various array and linked list operations, including insertion, deletion, merging, and traversal. It demonstrates how to manipulate both singly and doubly linked lists, providing functions for creating nodes, counting nodes, and deleting all nodes. The main function showcases examples of these operations in action.

Uploaded by

foheke6586
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.

Program to insert an element at any position of an array

#include <stdio.h>

void insertElement(int arr[], int *size, int element, int position) {


// Check if position is valid
if (position < 0 || position > *size) {
printf("Invalid position!\n");
return;
}

// Shift elements to make space for new element


for (int i = *size - 1; i >= position; i--) {
arr[i + 1] = arr[i];
}

// Insert the element at the specified position


arr[position] = element;

// Increase size
(*size)++;
}

// 2. Program to delete an element from any position of an array

void deleteElement(int arr[], int *size, int position) {


// Check if position is valid
if (position < 0 || position >= *size) {
printf("Invalid position!\n");
return;
}

// Shift elements to remove the gap


for (int i = position; i < *size - 1; i++) {
arr[i] = arr[i + 1];
}

// Decrease size
(*size)--;
}

// 3. Program to merge two arrays

void mergeArrays(int arr1[], int size1, int arr2[], int size2, int
result[]) {
// Copy elements from first array
for (int i = 0; i < size1; i++) {
result[i] = arr1[i];
}

// Copy elements from second array


for (int i = 0; i < size2; i++) {
result[size1 + i] = arr2[i];
}
}

// Example of using all the array operations


int main() {
int arr[100], size = 0;
int choice, element, position;

printf("Array Operations:\n");

// Insert some elements


printf("Adding initial elements (10, 20, 30, 40, 50)...\n");
for (int i = 0; i < 5; i++) {
arr[i] = (i + 1) * 10;
size++;
}

// Display the array


printf("Array: ");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");

// Insert an element
element = 25;
position = 2;
printf("\nInserting %d at position %d...\n", element, position);
insertElement(arr, &size, element, position);

// Display the array after insertion


printf("Array after insertion: ");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");

// Delete an element
position = 3;
printf("\nDeleting element at position %d...\n", position);
deleteElement(arr, &size, position);

// Display the array after deletion


printf("Array after deletion: ");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");

// Merge arrays
printf("\nMerging two arrays...\n");
int arr2[] = {60, 70, 80};
int size2 = 3;
int merged[100];

mergeArrays(arr, size, arr2, size2, merged);

// Display the merged array


printf("Merged array: ");
for (int i = 0; i < size + size2; i++) {
printf("%d ", merged[i]);
}
printf("\n");

return 0;
}

// 4-14. Programs for Singly Linked List operations

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

// Define the structure for a node


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

// 4. Function to create a linked list and traverse it


void createAndTraverse() {
struct Node* head = NULL;
struct Node* second = NULL;
struct Node* third = NULL;

// Allocate memory for nodes


head = (struct Node*)malloc(sizeof(struct Node));
second = (struct Node*)malloc(sizeof(struct Node));
third = (struct Node*)malloc(sizeof(struct Node));

// Assign data values


head->data = 10;
second->data = 20;
third->data = 30;

// Connect nodes
head->next = second;
second->next = third;
third->next = NULL;

// Traverse the linked list


printf("\nTraversing the linked list:\n");
struct Node* current = head;
while (current != NULL) {
printf("%d -> ", current->data);
current = current->next;
}
printf("NULL\n");

// Free allocated memory


free(head);
free(second);
free(third);
}

// 5. Function to insert a node at the beginning


struct Node* insertAtBeginning(struct Node* head, int new_data) {
// Allocate memory for new node
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));

// Set the data


new_node->data = new_data;

// Make next of new node as head


new_node->next = head;

// Move the head to point to the new node


head = new_node;

return head;
}

// 6. Function to insert a node at the end


struct Node* insertAtEnd(struct Node* head, int new_data) {
// Allocate memory for new node
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));

// Set the data


new_node->data = new_data;
// This new node is going to be the last node, so make next of it
as NULL
new_node->next = NULL;

// If the Linked List is empty, then make the new node as head
if (head == NULL) {
return new_node;
}

// Else traverse till the last node


struct Node* last = head;
while (last->next != NULL) {
last = last->next;
}

// Change the next of last node


last->next = new_node;

return head;
}

// 7. Function to insert a node at a given position


struct Node* insertAtPosition(struct Node* head, int new_data, int
position) {
// If position is 0, then call insertAtBeginning
if (position == 0) {
return insertAtBeginning(head, new_data);
}

// Allocate memory for new node


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

// Set the data


new_node->data = new_data;

// Find the previous node of position


struct Node* temp = head;
for (int i = 0; i < position - 1 && temp != NULL; i++) {
temp = temp->next;
}

// If position is more than number of nodes


if (temp == NULL || temp->next == NULL) {
printf("Position out of range\n");
free(new_node);
return head;
}
// Make next of new node as next of temp
new_node->next = temp->next;

// Move the next of temp as new node


temp->next = new_node;

return head;
}

// 8. Function to count the number of nodes


int countNodes(struct Node* head) {
int count = 0;
struct Node* current = head;

while (current != NULL) {


count++;
current = current->next;
}

return count;
}

// 9. Function to reverse a linked list


struct Node* reverseList(struct Node* head) {
struct Node *prev = NULL, *current = head, *next = NULL;

while (current != NULL) {


// Store next
next = current->next;

// Reverse current node's pointer


current->next = prev;

// Move pointers one position ahead


prev = current;
current = next;
}

head = prev;
return head;
}

// 10. Function to merge two linked lists


struct Node* mergeLists(struct Node* list1, struct Node* list2) {
// If first list is empty, return second list
if (list1 == NULL)
return list2;
// If second list is empty, return first list
if (list2 == NULL)
return list1;

// Create a copy of the first list


struct Node* result = NULL;
struct Node* current = list1;

while (current != NULL) {


result = insertAtEnd(result, current->data);
current = current->next;
}

// Append the second list


current = list2;
while (current != NULL) {
result = insertAtEnd(result, current->data);
current = current->next;
}

return result;
}

// 11. Function to delete a node from the beginning


struct Node* deleteFromBeginning(struct Node* head) {
if (head == NULL) {
printf("List is empty\n");
return NULL;
}

struct Node* temp = head;


head = head->next;
free(temp);

return head;
}

// 12. Function to delete a node from the end


struct Node* deleteFromEnd(struct Node* head) {
if (head == NULL) {
printf("List is empty\n");
return NULL;
}

// If only one node


if (head->next == NULL) {
free(head);
return NULL;
}

// Find the second last node


struct Node* current = head;
while (current->next->next != NULL) {
current = current->next;
}

// Delete the last node


free(current->next);
current->next = NULL;

return head;
}

// 13. Function to delete a node from a given position


struct Node* deleteFromPosition(struct Node* head, int position) {
// If empty list
if (head == NULL) {
printf("List is empty\n");
return NULL;
}

// If position is 0, delete from beginning


if (position == 0) {
return deleteFromBeginning(head);
}

// Find the previous node of the node to be deleted


struct Node* current = head;
for (int i = 0; i < position - 1 && current != NULL; i++) {
current = current->next;
}

// If position is more than number of nodes


if (current == NULL || current->next == NULL) {
printf("Position out of range\n");
return head;
}

// Node to be deleted
struct Node* temp = current->next;

// Change next of current


current->next = temp->next;

// Free memory
free(temp);
return head;
}

// 14. Function to delete all nodes


struct Node* deleteAllNodes(struct Node* head) {
struct Node* current = head;
struct Node* next;

while (current != NULL) {


next = current->next;
free(current);
current = next;
}

return NULL;
}

// Function to print the linked list


void printList(struct Node* head) {
struct Node* current = head;

while (current != NULL) {


printf("%d -> ", current->data);
current = current->next;
}
printf("NULL\n");
}

// Example of using all the linked list operations


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

printf("Singly Linked List Operations:\n");

// Create and traverse a linked list


createAndTraverse();

// Insert at beginning
printf("\nInserting nodes at the beginning (15, 5)...\n");
head = insertAtBeginning(NULL, 15);
head = insertAtBeginning(head, 5);
printf("List after insertion at beginning: ");
printList(head);

// Insert at end
printf("\nInserting nodes at the end (25, 35)...\n");
head = insertAtEnd(head, 25);
head = insertAtEnd(head, 35);
printf("List after insertion at end: ");
printList(head);

// Insert at position
printf("\nInserting 20 at position 2...\n");
head = insertAtPosition(head, 20, 2);
printf("List after insertion at position: ");
printList(head);

// Count nodes
printf("\nNumber of nodes: %d\n", countNodes(head));

// Reverse list
printf("\nReversing the list...\n");
head = reverseList(head);
printf("Reversed list: ");
printList(head);

// Create a second list for merging


printf("\nCreating a second list...\n");
struct Node* head2 = NULL;
head2 = insertAtEnd(head2, 40);
head2 = insertAtEnd(head2, 50);
head2 = insertAtEnd(head2, 60);
printf("Second list: ");
printList(head2);

// Merge lists
printf("\nMerging the two lists...\n");
struct Node* mergedList = mergeLists(head, head2);
printf("Merged list: ");
printList(mergedList);

// Delete from beginning


printf("\nDeleting node from beginning...\n");
mergedList = deleteFromBeginning(mergedList);
printf("List after deletion from beginning: ");
printList(mergedList);

// Delete from end


printf("\nDeleting node from end...\n");
mergedList = deleteFromEnd(mergedList);
printf("List after deletion from end: ");
printList(mergedList);

// Delete from position


printf("\nDeleting node at position 2...\n");
mergedList = deleteFromPosition(mergedList, 2);
printf("List after deletion from position: ");
printList(mergedList);

// Delete all nodes


printf("\nDeleting all nodes...\n");
mergedList = deleteAllNodes(mergedList);
printf("List after deleting all nodes: ");
printList(mergedList);

// Free any remaining memory


head = deleteAllNodes(head);
head2 = deleteAllNodes(head2);

return 0;
}

// 15. Program to implement Doubly Linked List

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

// Define the structure for a node in doubly linked list


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

// 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;
newNode->prev = NULL;
return newNode;
}

// Function to insert at the beginning


struct Node* insertAtBeginning(struct Node* head, int data) {
struct Node* newNode = createNode(data);

if (head == NULL) {
return newNode;
}

newNode->next = head;
head->prev = newNode;
return newNode;
}

// Function to insert at the end


struct Node* insertAtEnd(struct Node* head, int data) {
struct Node* newNode = createNode(data);

if (head == NULL) {
return newNode;
}

struct Node* current = head;


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

current->next = newNode;
newNode->prev = current;

return head;
}

// Function to insert at a specific position


struct Node* insertAtPosition(struct Node* head, int data, int
position) {
if (position < 0) {
printf("Invalid position\n");
return head;
}

if (position == 0) {
return insertAtBeginning(head, data);
}

struct Node* newNode = createNode(data);


struct Node* current = head;
int i = 0;

while (i < position - 1 && current != NULL) {


current = current->next;
i++;
}

if (current == NULL) {
printf("Position out of range\n");
free(newNode);
return head;
}

newNode->next = current->next;

if (current->next != NULL) {
current->next->prev = newNode;
}

current->next = newNode;
newNode->prev = current;

return head;
}

// Function to delete from the beginning


struct Node* deleteFromBeginning(struct Node* head) {
if (head == NULL) {
printf("List is empty\n");
return NULL;
}

struct Node* temp = head;

if (head->next != NULL) {
head->next->prev = NULL;
}

head = head->next;
free(temp);

return head;
}

// Function to delete from the end


struct Node* deleteFromEnd(struct Node* head) {
if (head == NULL) {
printf("List is empty\n");
return NULL;
}

if (head->next == NULL) {
free(head);
return NULL;
}

struct Node* current = head;


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

current->prev->next = NULL;
free(current);

return head;
}

// Function to delete from a specific position


struct Node* deleteFromPosition(struct Node* head, int position) {
if (head == NULL) {
printf("List is empty\n");
return NULL;
}

if (position == 0) {
return deleteFromBeginning(head);
}

struct Node* current = head;


int i = 0;

while (i < position && current != NULL) {


current = current->next;
i++;
}

if (current == NULL) {
printf("Position out of range\n");
return head;
}

current->prev->next = current->next;

if (current->next != NULL) {
current->next->prev = current->prev;
}

free(current);

return head;
}

// Function to display the list forward


void displayForward(struct Node* head) {
struct Node* current = head;

printf("Forward: NULL <-- ");


while (current != NULL) {
printf("%d <--> ", current->data);
current = current->next;
}
printf("NULL\n");
}

// Function to display the list backward


void displayBackward(struct Node* head) {
if (head == NULL) {
printf("Backward: NULL\n");
return;
}

struct Node* current = head;


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

printf("Backward: NULL <-- ");


while (current != NULL) {
printf("%d <--> ", current->data);
current = current->prev;
}
printf("NULL\n");
}

// Function to free all nodes


struct Node* deleteAllNodes(struct Node* head) {
struct Node* current = head;
struct Node* next;

while (current != NULL) {


next = current->next;
free(current);
current = next;
}

return NULL;
}

// Example of using doubly linked list operations


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

printf("Doubly Linked List Operations:\n");

// Insert at beginning
printf("\nInserting nodes at the beginning (15, 10, 5)...\n");
head = insertAtBeginning(head, 15);
head = insertAtBeginning(head, 10);
head = insertAtBeginning(head, 5);
displayForward(head);
displayBackward(head);

// Insert at end
printf("\nInserting nodes at the end (20, 25)...\n");
head = insertAtEnd(head, 20);
head = insertAtEnd(head, 25);
displayForward(head);
displayBackward(head);

// Insert at position
printf("\nInserting 12 at position 2...\n");
head = insertAtPosition(head, 12, 2);
displayForward(head);

// Delete from beginning


printf("\nDeleting node from the beginning...\n");
head = deleteFromBeginning(head);
displayForward(head);

// Delete from end


printf("\nDeleting node from the end...\n");
head = deleteFromEnd(head);
displayForward(head);

// Delete from position


printf("\nDeleting node at position 2...\n");
head = deleteFromPosition(head, 2);
displayForward(head);
displayBackward(head);

// Delete all nodes


printf("\nDeleting all nodes...\n");
head = deleteAllNodes(head);
displayForward(head);

return 0;
}

// 16. Program to implement Circular Linked List

#include <stdio.h>
#include <stdlib.h>
// Define the structure for a node
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 at the beginning


struct Node* insertAtBeginning(struct Node* head, int data) {
struct Node* newNode = createNode(data);

if (head == NULL) {
newNode->next = newNode; // Point to itself for first node
return newNode;
}

struct Node* current = head;


while (current->next != head) {
current = current->next;
}

newNode->next = head;
current->next = newNode;

return newNode;
}

// Function to insert at the end


struct Node* insertAtEnd(struct Node* head, int data) {
struct Node* newNode = createNode(data);

if (head == NULL) {
newNode->next = newNode; // Point to itself for first node
return newNode;
}

struct Node* current = head;


while (current->next != head) {
current = current->next;
}
current->next = newNode;
newNode->next = head;

return head;
}

// Function to insert at a specific position


struct Node* insertAtPosition(struct Node* head, int data, int
position) {
if (head == NULL || position == 0) {
return insertAtBeginning(head, data);
}

struct Node* newNode = createNode(data);


struct Node* current = head;
int i = 0;

while (i < position - 1 && current->next != head) {


current = current->next;
i++;
}

newNode->next = current->next;
current->next = newNode;

return head;
}

// Function to delete from the beginning


struct Node* deleteFromBeginning(struct Node* head) {
if (head == NULL) {
printf("List is empty\n");
return NULL;
}

// If there is only one node


if (head->next == head) {
free(head);
return NULL;
}

struct Node* current = head;


while (current->next != head) {
current = current->next;
}

struct Node* temp = head;


head = head->next;
current->next = head;
free(temp);

return head;
}

// Function to delete from the end


struct Node* deleteFromEnd(struct Node* head) {
if (head == NULL) {
printf("List is empty\n");
return NULL;
}

// If there is only one node


if (head->next == head) {
free(head);
return NULL;
}

struct Node* current = head;


struct Node* prev = NULL;

while (current->next != head) {


prev = current;
current = current->next;
}

prev->next = head;
free(current);

return head;
}

// Function to delete from a specific position


struct Node* deleteFromPosition(struct Node* head, int position) {
if (head == NULL) {
printf("List is empty\n");
return NULL;
}

if (position == 0) {
return deleteFromBeginning(head);
}

struct Node* current = head;


struct Node* prev = NULL;
int i = 0;
do {
if (i == position) {
prev->next = current->next;
free(current);
return head;
}
prev = current;
current = current->next;
i++;
} while (current != head);

printf("Position out of range\n");


return head;
}

// Function to display the list


void displayList(struct Node* head) {
if (head == NULL) {
printf("List is empty\n");
return;
}

struct Node* current = head;

printf("List: ");
do {
printf("%d -> ", current->data);
current = current->next;
} while (current != head);
printf("(back to %d)\n", head->data);
}

// Function to count nodes


int countNodes(struct Node* head) {
if (head == NULL) {
return 0;
}

int count = 0;
struct Node* current = head;

do {
count++;
current = current->next;
} while (current != head);

return count;
}
// Function to free all nodes
struct Node* deleteAllNodes(struct Node* head) {
if (head == NULL) {
return NULL;
}

struct Node* current = head;


struct Node* next;

do {
next = current->next;
free(current);
current = next;
} while (current != head);

return NULL;
}

// Example of using circular linked list operations


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

printf("Circular Linked List Operations:\n");

// Insert at beginning
printf("\nInserting nodes at the beginning (15, 10, 5)...\n");
head = insertAtBeginning(head, 15);
head = insertAtBeginning(head, 10);
head = insertAtBeginning(head, 5);
displayList(head);

// Insert at end
printf("\nInserting nodes at the end (20, 25)...\n");
head = insertAtEnd(head, 20);
head = insertAtEnd(head, 25);
displayList(head);

// Insert at position
printf("\nInserting 12 at position 2...\n");
head = insertAtPosition(head, 12, 2);
displayList(head);

// Count nodes
printf("\nNumber of nodes: %d\n", countNodes(head));

// Delete from beginning


printf("\nDeleting node from the beginning...\n");
head = deleteFromBeginning(head);
displayList(head);

// Delete from end


printf("\nDeleting node from the end...\n");
head = deleteFromEnd(head);
displayList(head);

// Delete from position


printf("\nDeleting node at position 2...\n");
head = deleteFromPosition(head, 2);
displayList(head);

// Delete all nodes


printf("\nDeleting all nodes...\n");
head = deleteAllNodes(head);
displayList(head);

return 0;
}

You might also like