Answer to Question #265545 in C++ for ygk

Question #265545

Write a program in c++ of doubly linked list and implement create a new node, insertion in list, search element in list, delete from list, reverse, traversal.


1
Expert's answer
2021-11-13T23:52:39-0500
#include <iostream>
#include<malloc.h>
using namespace std;
struct LinkedList {
   int item;
   struct LinkedList *prevNode;
   struct LinkedList *nextNode;
};
struct LinkedList * head = NULL;
void insert(int data) {
   struct LinkedList *node = (struct LinkedList *) malloc(sizeof(struct LinkedList));
   node->item = data;
   node->prevNode = NULL;
   node->nextNode = head;
   if(head != NULL)
   head->prevNode = node ;
   head = node;
}
void traverse() {
   struct LinkedList * ptr;
   ptr = head;
   while(ptr != NULL) {
      cout<< ptr->item <<" ";
      ptr = ptr->nextNode;
   }
}
int main() {
   insert(1);
   insert(2);
   insert(3);
   insert(4);
   insert(5);
   cout<<"The content of the doubly linked list is: ";
   traverse();
   return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS