0% found this document useful (0 votes)
4 views18 pages

Linledlist

The document contains multiple C programs that demonstrate various operations on linked lists, including insertion at the front, end, and after a given node, as well as deletion from the beginning, middle, and end. It also includes a stack implementation using a linked list. Each program initializes a linked list or stack, performs operations, and displays the results.
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)
4 views18 pages

Linledlist

The document contains multiple C programs that demonstrate various operations on linked lists, including insertion at the front, end, and after a given node, as well as deletion from the beginning, middle, and end. It also includes a stack implementation using a linked list. Each program initializes a linked list or stack, performs operations, and displays the results.
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/ 18

#include <stdio.

h>
#include <stdlib.h>

// Define the structure for a node


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

// Function to insert a node at the front of the linked list


void insert_at_front(struct Node** head_ref, int data) {
// Create a new node
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
if (new_node == NULL) {
printf("Memory allocation failed.\n");
return;
}

// Set the data and next pointer of the new node


new_node->data = data;
new_node->next = *head_ref;

// Update the head to point to the new node


*head_ref = new_node;
}

// Function to display the contents of the linked list


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

int main() {
// Initialize an empty linked list
struct Node* head = NULL;

// Insert nodes at the front of the linked list


insert_at_front(&head, 3);
insert_at_front(&head, 5);
insert_at_front(&head, 7);

// Display the contents of the linked list


display(head);

return 0;
}

%%writefile linkedlist_insertion_end.c
#include <stdio.h>
#include <stdlib.h>

// Define the structure for a node


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

// Function to insert a node at the end of the linked list


void insert_at_end(struct Node** head_ref, int data) {
// Create a new node
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
if (new_node == NULL) {
printf("Memory allocation failed.\n");
return;
}

// Set the data and next pointer of the new node


new_node->data = data;
new_node->next = NULL;

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


if (*head_ref == NULL) {
*head_ref = new_node;
return;
}

// Traverse the list to find the last node


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

// Set the next pointer of the last node to point to the new node
last->next = new_node;
}

// Function to display the contents of the linked list


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

int main() {
// Initialize an empty linked list
struct Node* head = NULL;

// Insert nodes at the end of the linked list


insert_at_end(&head, 3);
insert_at_end(&head, 5);
insert_at_end(&head, 7);

// Display the contents of the linked list


display(head);

return 0;
}

%%writefile linkedlist_insertion_after_given_node.c
#include <stdio.h>
#include <stdlib.h>

// Define the structure for a node


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

// Function to insert a node after a given node in the linked list


void insert_after(struct Node* prev_node, int data) {
// Check if the given previous node is NULL
if (prev_node == NULL) {
printf("Previous node cannot be NULL.\n");
return;
}

// Create a new node


struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
if (new_node == NULL) {
printf("Memory allocation failed.\n");
return;
}

// Set the data of the new node


new_node->data = data;

// Set the next pointer of the new node to point to the next node of the
previous node
new_node->next = prev_node->next;

// Set the next pointer of the previous node to point to the new node
prev_node->next = new_node;
}

// Function to display the contents of the linked list


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

int main() {
// Initialize an empty linked list
struct Node* head = NULL;

// Insert nodes at the end of the linked list


insert_after(head, 3); // This should print "Previous node cannot be NULL."

// Create a node with data 2


head = (struct Node*)malloc(sizeof(struct Node));
head->data = 2;
head->next = NULL;

// Insert nodes after the given node


insert_after(head, 5); // Insert 5 after 2
insert_after(head, 7); // Insert 7 after 2

// Display the contents of the linked list


display(head);

return 0;
}
%%writefile linkedlist_delete_at_begining.c
#include <stdio.h>
#include <stdlib.h>

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


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

// Function to delete the head node of the linked list


void deleteHead(struct Node** head_ref) {
// If the list is empty, return
if (*head_ref == NULL)
return;

// Store the current head node


struct Node* temp = *head_ref;

// Move the head pointer to the next node


*head_ref = (*head_ref)->next;

// Free the memory occupied by the original head node


free(temp);
}

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


void push(struct Node** head_ref, int new_data) {
// Allocate memory for new node
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
if (new_node == NULL) {
printf("Memory allocation failed\n");
return;
}

// Assign data to the new node


new_node->data = new_data;

// Make next of new node as head


new_node->next = *head_ref;

// Move the head to point to the new node


*head_ref = new_node;
}

// Function to print the linked list


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

// Main function
int main() {
// Initialize an empty linked list
struct Node* head = NULL;

// Insert some elements into the linked list


push(&head, 3);
push(&head, 2);
push(&head, 1);

printf("Linked list before deleting the head node: ");


printList(head);

// Delete the head node


deleteHead(&head);
printf("Linked list after deleting the head node: ");
printList(head);

return 0;
}

%%writefile linkedlist_delete_at_middle.c
#include <stdio.h>
#include <stdlib.h>

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


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

// Function to delete a node from the middle of the linked list


void deleteMiddleNode(struct Node** head_ref, int key) {
// If the list is empty, return
if (*head_ref == NULL)
return;

// Store the head node


struct Node* temp = *head_ref;
struct Node* prev = NULL;

// If the head node itself holds the key to be deleted


if (temp != NULL && temp->data == key) {
*head_ref = temp->next; // Change head
free(temp); // Free old head
return;
}

// Find the node with the given key and keep track of the previous node
while (temp != NULL && temp->data != key) {
prev = temp;
temp = temp->next;
}

// If key was not present in linked list


if (temp == NULL)
return;

// Unlink the node from the linked list


prev->next = temp->next;

// Free the memory allocated for the node


free(temp);
}

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


void push(struct Node** head_ref, int new_data) {
// Allocate memory for new node
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
if (new_node == NULL) {
printf("Memory allocation failed\n");
return;
}

// Assign data to the new node


new_node->data = new_data;

// Make next of new node as head


new_node->next = *head_ref;

// Move the head to point to the new node


*head_ref = new_node;
}

// Function to print the linked list


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

int main() {
// Initialize an empty linked list
struct Node* head = NULL;

// Insert some elements into the linked list


push(&head, 3);
push(&head, 2);
push(&head, 1);

printf("Linked list before deletion: ");


printList(head);

// Delete node with value 2


deleteMiddleNode(&head, 2);
printf("Linked list after deletion of node with value 2: ");
printList(head);

return 0;
}

%%writefile linkedlist_delete_at_end.c
#include <stdio.h>
#include <stdlib.h>

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


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

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


void deleteLastNode(struct Node** head_ref) {
// If the list is empty, return
if (*head_ref == NULL)
return;

// If there is only one node, delete it and set head to NULL


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

// Otherwise, traverse the list to find the second-to-last node


struct Node* temp = *head_ref;
while (temp->next->next != NULL) {
temp = temp->next;
}

// Free the memory allocated for the last node and set next of second-to-last to
NULL
free(temp->next);
temp->next = NULL;
}

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


void push(struct Node** head_ref, int new_data) {
// Allocate memory for new node
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
if (new_node == NULL) {
printf("Memory allocation failed\n");
return;
}

// Assign data to the new node


new_node->data = new_data;
// Make next of new node as head
new_node->next = *head_ref;

// Move the head to point to the new node


*head_ref = new_node;
}

// Function to print the linked list


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

// Main function
int main() {
// Initialize an empty linked list
struct Node* head = NULL;

// Insert some elements into the linked list


push(&head, 3);
push(&head, 2);
push(&head, 1);

printf("Linked list before deleting the last node: ");


printList(head);

// Delete the last node


deleteLastNode(&head);
printf("Linked list after deleting the last node: ");
printList(head);

return 0;
}

%%writefile stacks_using_linkedlist.c
#include <stdio.h>
#include <stdlib.h>

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


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

// Define a structure for the stack


struct Stack {
struct Node* top;
};

// Function to create a new node


struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) {
printf("Memory allocation failed\n");
exit(1);
}
newNode->data = data;
newNode->next = NULL;
return newNode;
}

// Function to initialize the stack


struct Stack* createStack() {
struct Stack* stack = (struct Stack*)malloc(sizeof(struct Stack));
if (stack == NULL) {
printf("Memory allocation failed\n");
exit(1);
}
stack->top = NULL;
return stack;
}

// Function to check if the stack is empty


int isEmpty(struct Stack* stack) {
return (stack->top == NULL);
}

// Function to push an element onto the stack


void push(struct Stack* stack, int data) {
struct Node* newNode = createNode(data);
newNode->next = stack->top;
stack->top = newNode;
}

// Function to pop an element from the stack


int pop(struct Stack* stack) {
if (isEmpty(stack)) {
printf("Stack underflow\n");
exit(1);
}
struct Node* temp = stack->top;
int data = temp->data;
stack->top = temp->next;
free(temp);
return data;
}

// Function to peek the top element of the stack


int peek(struct Stack* stack) {
if (isEmpty(stack)) {
printf("Stack is empty\n");
exit(1);
}
return stack->top->data;
}

// Main function
int main() {
struct Stack* stack = createStack();

// Push elements onto the stack


push(stack, 1);
push(stack, 2);
push(stack, 3);

// Peek and pop elements from the stack


printf("Top element of the stack: %d\n", peek(stack));
printf("Popping elements from the stack: ");
while (!isEmpty(stack)) {
printf("%d ", pop(stack));
}
printf("\n");

return 0;
}

You might also like