Grounded Header LL
Grounded Header LL
#include <stdio.h>
#include <stdlib.h>
// Function to insert a node at the beginning of the list (after the header)
struct node* insertAtBeginning(struct node* header, int newData) {
// Create a new node
struct node* newNode=createNode(newData);
// Function to insert a node at the end of the grounded header linked list
struct node* insertAtEnd(struct node* header, int data) {
struct node* temp = header;
// Traverse the list to find the node before the insertion point
for (int i = 1; i < position; i++) {
if (temp->next == NULL) {
printf("Position %d is out of bounds. Node will be inserted at the
end.\n", position);
break;
}
temp = temp->next;
}
// The node to be deleted is the first actual node after the header
struct node* temp = header->next;
// If prev is still NULL, it means there's only one element (excluding header)
if (prev == header) {
header->next = NULL;
} else {
prev->next = NULL; // Disconnect the last node from the list
}
// Update the previous node's next pointer to skip the node at the position
prev->next = temp->next;
printf("NULL\n");
printf("The number of nodes are : %d" , header->data);
// Main function
int main() {
// Create the header node
struct node* header = (struct node*)malloc(sizeof(struct node));
header-> data=0;
header->next = NULL; // Initialize header's next to NULL
// insert at end
printf("\nInsertion at end\n");
header = insertAtEnd(header,70);
displayList(header);
//insert at position
printf("\n");
header = insertAtPosition(header,3,80);
displayList(header);
// Delete at position
printf("\n");
header = deleteAtPosition(header, 2);
displayList(header);
return 0;
}