Write a program that connects singly linked list with a doubly linked list
#include<iostream>
using namespace std;
struct Node
{
int data;
struct Node *prev;
struct Node *next;
Node(int _data) :data(_data), prev(NULL), next(NULL) {}
};
struct SLLNode
{
int data;
struct SLLNode *next;
SLLNode(int _data) :data(_data),next(NULL) {}
};
class DLL
{
Node *Head;
Node *Tail;
public:
DLL() :Head(NULL), Tail(NULL) {}
void Insert(int value, struct Node *root)
{
struct Node *p = new Node(value);
if (Head != 0)
{
p->prev = Tail;
Tail->next = p;
Tail = p;
}
else
{
p->prev = NULL;
Head = Tail = p;
}
}
void InsertSLL(struct SLLNode *SLLroot)
{
if (Head != 0&& SLLroot!=0)
{
while (SLLroot != NULL)
{
struct Node* p = new Node(SLLroot->data);
p->prev = Tail;
Tail->next = p;
Tail = p;
SLLroot = SLLroot->next;
}
}
}
void Display()
{
struct Node *p = Head;
while (p!=NULL)
{
cout << p->data << " ";
p = p->next;
}
}
};
int main()
{
struct Node *root = NULL;
DLL dl;
dl.Insert(56, root);
dl.Insert(90, root);
dl.Insert(50, root);
dl.Insert(45, root);
cout << "\nDLL: ";
dl.Display();
struct SLLNode *rootSLL = new SLLNode(1);
rootSLL->next = new SLLNode(2);
rootSLL->next->next = new SLLNode(3);
cout << "\nSLL: ";
struct SLLNode* temp = rootSLL;
while (temp != NULL)
{
cout << temp->data << " ";
temp = temp->next;
}
dl.InsertSLL(rootSLL);
cout << "\nDLL+SLL: ";
dl.Display();
}
Comments
Leave a comment