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.
#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;
}
Comments
Leave a comment