-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConsoleApplication3.cpp
203 lines (176 loc) · 4.6 KB
/
ConsoleApplication3.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// ConsoleApplication3.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <iostream>
using namespace std;
class Node {
public:
Node() {
next = 0;
prevoius = 0;
//write your modification here
}
Node(int el, Node* ptr = 0,Node *ptr2=0) { //write your modification for the constructor arguments here
info = el;
next = ptr;
prevoius = ptr2;
//write your modification here
}
int info;
Node* next;
Node* prevoius;
};
class LList {
private:
Node* head, * tail;
public:
LList() {
head = tail = 0;
}
~LList() {
for (Node* p; !isEmpty(); ) {
p = head->next;
delete head;
head = p;
}
}
int isEmpty() {
return head == 0;
}
void addToHead(int el) {
//head = new Node(el, head);
head->prevoius = new Node(el,head,NULL);
//head = new Node(el, head);
if (tail == 0)
tail = head;
}
void addToTail(int el) {
if (tail != 0) { // if list not empty;
tail->next = new Node(el,0,tail);
tail = tail->next;
//tail
}
else head = tail = new Node(el);
}
int deleteFromHead() { // delete the head and return its info;
int el = head->info;
Node* tmp = head;
if (head == tail) // if only one node in the list;
head = tail = 0;
else {
//head->prevoius = head;
head = head->next;
head->prevoius = NULL;
}
delete tmp;
return el;
}
int deleteFromTail() { // delete the tail and return its info;
int el = tail->info;
if (head == tail) { // if only one node in the list;
delete head;
head = tail = 0;
}
else { // if more than one node in the list,
Node* tmp; // find the predecessor of tail;
for (tmp = head; tmp->next != tail; tmp = tmp->next);
tmp->prevoius =tail;
delete tail;
tail = tmp; // the predecessor of tail becomes tail;
tail->next = 0;
}
return el;
}
bool isInList(int el) const {
Node* tmp;
for (tmp = head; tmp != 0 && !(tmp->info == el); tmp = tmp->next);
return tmp != 0;
}
void displayList() {
if (head == 0) // if empty list;
return;
Node* tmp = head;
for (tmp = head; tmp != 0; tmp = tmp->next) {
cout << tmp->info << endl;
}
}
};
class LLStack {
private:
LList lst;
public:
LLStack() {
}
void clearStack() {
/*while (true)
{
}*/
}
bool isEmpty() {
if (head!=NULL)
{
return 1;
}
else
{
return 0;
}
}
int topEl() {
return head->info;
}
int pop() {
if (head == NULL) {
cout << "list is empty";
return 0;
}
Node* ptr = head;
head = head->next;
delete ptr;
return head;
//write your code here
}
void push(int el) {
if (head == NULL) {
head = new Node(el);
}
else
{
Node* ptr = new Node(el, head);
head = ptr;
}
}
void displayStack() {
lst.displayList();
}
};
int main()
{
/*LList myList;
myList.addToHead(5);
myList.addToHead(4);
myList.addToHead(3);
myList.addToHead(2);
myList.addToHead(1);
myList.displayList();*/
LLStack myStack;
myStack.push(1);
myStack.push(2);
myStack.push(3);
cout << myStack.topEl(); //should print 3
myStack.push(4);
int top = myStack.pop();
myStack.push(5);
myStack.displayStack(); //should print 5 3 2 1
myStack.clearStack();
myStack.displayStack(); // should print nothing
return 0;
}
// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu
// Tips for Getting Started:
// 1. Use the Solution Explorer window to add/manage files
// 2. Use the Team Explorer window to connect to source control
// 3. Use the Output window to see build output and other messages
// 4. Use the Error List window to view errors
// 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
// 6. In the future, to open this project again, go to File > Open > Project and select the .sln file