-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConvertTreeToCircularDoublyLinkedList.cpp
More file actions
112 lines (108 loc) · 1.77 KB
/
ConvertTreeToCircularDoublyLinkedList.cpp
File metadata and controls
112 lines (108 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include<iostream>
#include<stdio.h>
#include<stack>
using namespace std;
struct node
{
int data;
node *left,*right;
};
struct lnode
{
int val;
lnode *next;
lnode *prev;
};
stack <int> st;
void push1(lnode **head,int val)
{
lnode *temp=new lnode;
temp->val=val;
temp->next=(*head);
temp->prev=NULL;
(*head)=temp;
}
node *newNode(int data)
{
node *temp=new node;
temp->data=data;
temp->left=NULL;
temp->right=NULL;
return temp;
}
void inorder(node *root)
{
if(root==NULL)
return ;
inorder(root->left);
//cout<<root->data<<" ";
st.push(root->data);
inorder(root->right);
}
void printStack()
{
while(!st.empty())
{
cout<<st.top()<<"\n";
st.pop();
}
}
void printList(lnode *root)
{
lnode *curr=root;
do
{
cout<<curr->val<<" ";
curr=curr->next;
}while(curr->next!=root);
}
void printList1(lnode *head)
{
lnode *curr=head;
while(curr!=NULL)
{
cout<<curr->val<<"\n";
curr=curr->next;
}
}
void conv()
{
lnode *res=NULL;
while(!st.empty())
{
push1(&res,st.top());
// cout<<st.top()<<" ";
st.pop();
}
// printList1(res);
lnode *curr=res;
lnode *next1;
lnode *temp=res;
while(temp->next!=NULL)
temp=temp->next;
curr->prev=temp;
temp->next=res;
// printList(res);
while(next1!=res)
{
next1=curr->next;
next1->prev=curr;
curr=next1;
next1=next1->next;
}
printList(res);
}
int main()
{
node *root = newNode(10);
root->left = newNode(12);
root->right = newNode(15);
root->left->left = newNode(25);
root->left->right = newNode(30);
root->right->left = newNode(36);
inorder(root);
conv();
//
// printStack();
return 0;
}