Double and circular linked list
Double and circular linked list
h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
} *head = NULL;
void insertAtBeginning(int);
void deleteFromBeginning();
void display();
int main() {
insertAtBeginning(7);
insertAtBeginning(6);
insertAtBeginning(5);
insertAtBeginning(4);
printf("Circular Linked List after inserting elements:\n");
display();
deleteFromBeginning();
printf("Circular Linked List after deleting the first element:\n");
display();
return 0;
}
void deleteFromBeginning() {
if (head == NULL) {
printf("\nList is empty, cannot delete the first element!\n");
return;
}
if (head->next == head) { // Only one node in the list
free(head);
head = NULL;
printf("\nFirst node deleted!\n");
return;
}
struct Node* temp = head;
struct Node* last = head;
while (last->next != head) { // Find the last node
last = last->next;
}
last->next = head->next;
head = head->next;
free(temp);
printf("\nFirst node deleted!\n");
}
void display() {
if (head == NULL) {
printf("\nList is Empty\n");
return;
}
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
struct Node* prev;
} *head = NULL;
void insertAtBeginning(int);
void deleteFromBeginning();
void display();
int main() {
insertAtBeginning(7);
insertAtBeginning(6);
insertAtBeginning(5);
insertAtBeginning(4);
printf("Doubly Linked List after inserting elements:\n");
display();
deleteFromBeginning();
printf("Doubly Linked List after deleting the first element:\n");
display();
return 0;
}
void deleteFromBeginning() {
if (head == NULL) {
printf("\nList is empty, cannot delete the first element!\n");
return;
}
struct Node* temp = head;
head = head->next;
if (head != NULL) {
head->prev = NULL;
}
free(temp);
printf("\nFirst node deleted!\n");
}
void display() {
if (head == NULL) {
printf("\nList is Empty\n");
} else {
struct Node* temp = head;
printf("\nList elements are:\n");
while (temp != NULL) {
printf("%d <-> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
}