diff --git a/cpp_codes/middleOfDoublyLinkedList.cpp b/cpp_codes/middleOfDoublyLinkedList.cpp new file mode 100644 index 0000000..b46c31a --- /dev/null +++ b/cpp_codes/middleOfDoublyLinkedList.cpp @@ -0,0 +1,88 @@ +/* +Problem statement - WAP to print the middle of a double linked list. +*/ + +#include +#include + +struct node{ + int data; + struct node* prev; + struct node* next; +}; + +struct node* create(int* , int); +void display(struct node*); +int getMiddleElement(struct node*); + +int main(){ + int n; + printf("Enter size of list : "); + scanf("%d",&n); + int* arr = (int*)malloc(n*sizeof(int)); + printf("Enter %d elements:\n",n); + for(int i=0;idata = arr[0]; + head->prev = NULL; + head->next = NULL; + ptr =head; + } + else{ + struct node* newNode = (struct node*)malloc(sizeof(struct node)); + newNode->data = arr[i]; + newNode->prev = ptr; + newNode->next = NULL; + ptr = ptr->next = newNode; + } + } + return head; +} + +void display(struct node* head){ + while(head){ + printf("%d",head->data); + if(head->next){ + printf(" -> <- "); + } + head=head->next; + } + printf("\n"); + return; +} + +int getMiddleElement(struct node* head){ + struct node* front = head; + struct node* rear = head; + while(rear->next){ + rear = rear->next; + } + while(front!=rear){ + front = front->next; + rear = rear->prev; + if(rear == head){ + return -1; + } + } + return front->data; +} \ No newline at end of file