-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaula20.java
105 lines (96 loc) · 1.88 KB
/
aula20.java
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
/*class Node{
Node next;
Node prev;
int data;
Node(int value){
data = value;
next = null;
prev = null;
}
}
class Stack{
private Node head;
private Node tail;
Stack()
{
head = null;
tail = null;
}
public void push(int value)
{
Node newNode = new Node(value);
if(head == null)
{
head = newNode;
head.next = null;
head.prev = null;
tail = newNode;
}
else
{
newNode.prev = tail;
tail.next = newNode;
tail = newNode;
}
}
public void pop()
{
if(head == null)
System.out.println("stack underflow");
if(head == tail)
{
head = null;
tail = null;
}
else
{
Node n = tail;
tail = tail.prev;
n.prev = null;
tail.next = null;
}
}
public void merge(Stack s)
{
if(s.tail != null)
{
head.prev = s.tail;
s.tail.next = head;
}
head = s.head;
s.tail = null;
s.head = null;
}
public void display()
{
if(tail != null)
{
Node n = tail;
while(n != null)
{
System.out.print(n.data + " ");
n = n.prev;
}
System.out.println();
}
else
{
System.out.println("Stack Underflow");
}
}
}
class aula20{
public static void main(String[] args)
{
Stack ms1 = new Stack();
Stack ms2 = new Stack();
ms1.push(6);
ms1.push(5);
ms1.push(4);
ms1.push(9);
ms1.push(8);
ms1.push(7);
ms1.merge(ms2);
ms1.display();
}
} */