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

Double and circular linked list

Uploaded by

premsrnvs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Double and circular linked list

Uploaded by

premsrnvs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

#include <stdio.

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 insertAtBeginning(int value) {


struct Node* newNode;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
if (head == NULL) {
newNode->next = newNode;
head = newNode;
} else {
struct Node* temp = head;
while (temp->next != head) {
temp = temp->next;
}
temp->next = newNode;
newNode->next = head;
head = newNode;
}
printf("\nOne node with value %d inserted at the beginning!\n", value);
}

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;
}

struct Node* temp = head;


printf("\nList elements are:\n");
printf("%d -> ", temp->data); // Print head node first
temp = temp->next;

while (temp != head) { // Continue until we loop back to head


printf("%d -> ", temp->data);
temp = temp->next;
}
printf("(back to head)\n");
}

#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 insertAtBeginning(int value) {


struct Node* newNode;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->prev = NULL;
if (head == NULL) {
newNode->next = NULL;
head = newNode;
} else {
newNode->next = head;
head->prev = newNode;
head = newNode;
}
printf("\nOne node with value %d inserted at the beginning!\n", value);
}

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");
}
}

You might also like