-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedListStack.cpp
137 lines (125 loc) · 1.99 KB
/
LinkedListStack.cpp
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <iostream>
using namespace std;
template <class t>
struct node {
t data;
node <t> *next;
node (){
next = 0;
}
node (t item){
data = item;
next = 0;
}
};
template <class t>
class LinkedStack {
node <t> *head ;
int count ;
public :
LinkedStack(){
count=0;
head =0;
}
bool push (t item){
node <t> *temp = new node <t> (item);
if (temp==0)return 0;
temp->next = head;
head= temp;
count++;
return 1;
}
bool empty (){
return count==0;
}
bool pop() {
if (empty())return 0;
node <t> *test = head ;
head = head->next ;
count--;
delete test;
return 1;
}
bool top (t &item){
if (empty())return 0;
item = head->data;
return 1;
}
void print (){
node <t> *test = head ;
while (test != 0 ){
cout<<test->data<<" | ";
test = test->next;
}
cout<<endl;
}
~LinkedStack (){
while (!empty())
pop();
}
LinkedStack (LinkedStack <t>&outer){
while (!outer.empty()){
outer.pop();
}
t load ;
LinkedStack <t> inner ;
while (!empty())
{
top(load);
inner.push(load);
pop();
}
while (!inner.empty()){
inner.top(load);
push(load);
outer.push(load);
inner.pop();
}
}
void operator = (LinkedStack <t>&outer){
while (!empty()){
pop();
}
t load ;
LinkedStack <t> inner ;
while (!outer.empty())
{
outer.top(load);
inner.push(load);
outer.pop();
}
while (!inner.empty()){
inner.top(load);
push(load);
outer.push(load);
inner.pop();
}
}
};
int main(int argc, char *argv[])
{
LinkedStack <int> s1 ;
s1.push(10);
//s2.push(66);
s1.push(122);
s1.push(334);
//s2.push(54);
//s2.push(12);
s1.push(122);
LinkedStack <int> s2;
s1.print();
cout<<"*********s1*******"<<endl;
s2.print();
cout<<"*********s2*******"<<endl;
s1.print();
cout<<"*********s1*******"<<endl;
s2.print();
cout<<"*********s2*******"<<endl;
s1.push(44);
s2.push(50);
s1.print();
cout<<"*********s1*******"<<endl;
s2.print();
cout<<"*********s2*******"<<endl;
return 0;
}