From dfdf77714c5964faf406ae347ab6bbd994eb400b Mon Sep 17 00:00:00 2001 From: Raaggee <56103314+RaaggeeS@users.noreply.github.com> Date: Thu, 6 Oct 2022 12:21:17 +0530 Subject: [PATCH] Add files via upload This folder contains a file about Doubly Linked List. Please review it under Hacktober. --- DSA_CPP/creatingDoublyLL.c | 116 +++++++++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 DSA_CPP/creatingDoublyLL.c diff --git a/DSA_CPP/creatingDoublyLL.c b/DSA_CPP/creatingDoublyLL.c new file mode 100644 index 00000000..7259ec9c --- /dev/null +++ b/DSA_CPP/creatingDoublyLL.c @@ -0,0 +1,116 @@ +#include +#include + +typedef int itemType; + +//self referential structure +typedef struct node +{ + itemType info; + struct node *link, *prev; +}node; + +typedef struct List +{ + node *head, *tail; +}List; + +node *createNode(itemType x) +{ + node *p; + p = (node *)malloc(1*sizeof(node)); + if (p==NULL) + { + printf("Memory not allocated\n"); + exit(1); + } + p->info = x; + p->link = NULL; + p->prev = NULL; + return p; +} +void show(List *lp) +{ + node *p; + + p = lp->head; + printf("Linked list is: "); + while(p != NULL) + { + printf("%d---->", p->info); + p = p->link; + } + printf("NULL\n\n"); +} +void showReverse(List *lp) +{ + node *p; + + p = lp->tail; + printf("Linked list is: "); + while(p != NULL) + { + printf("%d---->", p->info); + p = p->prev; + } + printf("NULL\n\n"); +} + +void insertAtTail(List *lp, itemType x) +{ + node *p; + + p = createNode(x); + + if (lp->head == NULL) + lp->head = p; + else + { + lp->tail->link = p; + p->prev = lp->tail; + } + lp->tail = p; +} + +void initialize(List *lp) +{ + lp->head = NULL; + lp->tail = NULL; +} +List *createList() +{ + List *lp; + itemType x; + int n, i; + + //allocate space for head and tail + lp = (List *)malloc(sizeof(List)); + if (lp==NULL) + { + printf("Memory not allocated\n"); + exit(1); + } + initialize(lp); + + printf("Enter count of items: "); + scanf("%d", &n); + + srand(time(0)); + for (i=0; i