File tree Expand file tree Collapse file tree 4 files changed +1225
-0
lines changed
Data Structures Exam/81277_IM Expand file tree Collapse file tree 4 files changed +1225
-0
lines changed Original file line number Diff line number Diff line change
1
+
Original file line number Diff line number Diff line change
1
+ #include < iostream>
2
+ #include < stack>
3
+ using namespace std ;
4
+
5
+ struct Node
6
+ {
7
+ int data;
8
+ Node* next;
9
+
10
+ Node (): next(nullptr ) {}
11
+ Node (int data,Node* next): data(data), next(nullptr ) {}
12
+ };
13
+
14
+ bool presentStack (stack<int > st, int data)
15
+ {
16
+ while (!st.empty ())
17
+ {
18
+ if (st.top ()==data)
19
+ {
20
+ return 1 ;
21
+ }
22
+ st.pop ();
23
+ }
24
+ return 0 ;
25
+ }
26
+
27
+ void unitedLists (Node* first, Node* second)
28
+ {
29
+ stack<int > result;
30
+
31
+ while (first != nullptr )
32
+ {
33
+ result.push (first->data );
34
+ first=first->next ;
35
+ }
36
+ while (second != nullptr )
37
+ {
38
+ if (!presentStack (result, second->data ))
39
+ {
40
+ result.push (second->data );
41
+ }
42
+ second=second->next ;
43
+ }
44
+
45
+ }
46
+
47
+ void intersection (Node* first, Node* second)
48
+ {
49
+ stack<int > result;
50
+ while (first!=nullptr )
51
+ {
52
+ if (presentStack (second,first->data ))
53
+ {
54
+ result.push (first->data );
55
+ }
56
+ first=first->next ;
57
+ }
58
+ while (!result.empty ())
59
+ {
60
+ cout<<result.top ()<<" " ;
61
+ result.pop ();
62
+ }
63
+
64
+ }
65
+
66
+ int main ()
67
+ {
68
+ Node* sixth (9 ,nullptr );
69
+ Node* fifth (8 ,sixth);
70
+ Node* fourth (7 ,fifth);
71
+ Node* third (3 ,fourth);
72
+ Node* second (2 ,third);
73
+ Node* first (1 ,second);
74
+ unitedLists (first, fourth);
75
+
76
+
77
+ return 0 ;
78
+ }
You can’t perform that action at this time.
0 commit comments