From 4bf2cab36c448fc744e6c812c1aaa8ca30609c7c Mon Sep 17 00:00:00 2001 From: Mayank Kumar <126708321+Mayank0604@users.noreply.github.com> Date: Fri, 20 Oct 2023 19:23:05 +0530 Subject: [PATCH 1/2] Create doublylinkedlist.c Program for creating doubly linked list in c --- doublylinkedlist.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 doublylinkedlist.c diff --git a/doublylinkedlist.c b/doublylinkedlist.c new file mode 100644 index 00000000..d4d4b789 --- /dev/null +++ b/doublylinkedlist.c @@ -0,0 +1,59 @@ +#include +#include + +// Define the structure for a doubly linked list node +struct Node { + int data; + struct Node* prev; + struct Node* next; +}; + +// 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->prev = NULL; + newNode->next = NULL; + return newNode; +} + +// Function to insert a node at the beginning of the list +void insertAtBeginning(struct Node** head, int data) { + struct Node* newNode = createNode(data); + if (*head == NULL) { + *head = newNode; + } else { + newNode->next = *head; + (*head)->prev = newNode; + *head = newNode; + } +} + +// Function to display the doubly linked list +void displayList(struct Node* head) { + struct Node* current = head; + while (current != NULL) { + printf("%d <-> ", current->data); + current = current->next; + } + printf("NULL\n"); +} + +int main() { + struct Node* head = NULL; + + // Insert some elements at the beginning of the list + insertAtBeginning(&head, 3); + insertAtBeginning(&head, 5); + insertAtBeginning(&head, 7); + + // Display the list + printf("Doubly Linked List: "); + displayList(head); + + return 0; +} From 86a935d5860b561391a63e4d2e3e548f522597ce Mon Sep 17 00:00:00 2001 From: Mayank Kumar <126708321+Mayank0604@users.noreply.github.com> Date: Sat, 21 Oct 2023 00:05:37 +0530 Subject: [PATCH 2/2] Create doublyll.c program for creating doubly linked list --- doublyll.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 doublyll.c diff --git a/doublyll.c b/doublyll.c new file mode 100644 index 00000000..d4d4b789 --- /dev/null +++ b/doublyll.c @@ -0,0 +1,59 @@ +#include +#include + +// Define the structure for a doubly linked list node +struct Node { + int data; + struct Node* prev; + struct Node* next; +}; + +// 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->prev = NULL; + newNode->next = NULL; + return newNode; +} + +// Function to insert a node at the beginning of the list +void insertAtBeginning(struct Node** head, int data) { + struct Node* newNode = createNode(data); + if (*head == NULL) { + *head = newNode; + } else { + newNode->next = *head; + (*head)->prev = newNode; + *head = newNode; + } +} + +// Function to display the doubly linked list +void displayList(struct Node* head) { + struct Node* current = head; + while (current != NULL) { + printf("%d <-> ", current->data); + current = current->next; + } + printf("NULL\n"); +} + +int main() { + struct Node* head = NULL; + + // Insert some elements at the beginning of the list + insertAtBeginning(&head, 3); + insertAtBeginning(&head, 5); + insertAtBeginning(&head, 7); + + // Display the list + printf("Doubly Linked List: "); + displayList(head); + + return 0; +}