-
Notifications
You must be signed in to change notification settings - Fork 4
/
Stack.cpp
68 lines (62 loc) · 1.06 KB
/
Stack.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
#include <stdio.h>
#include "Headers/Stack.h"
Stack::Stack(){
start=NULL;
}
Stack::~Stack(){
List_of_Return_Addresses *p1=start;
List_of_Return_Addresses *p2;
while (p1!=NULL){
p2=p1->pointer;
delete p1;
p1=p2;
}
}
void Stack::Push(const int a){
if (start==NULL){
start=new List_of_Return_Addresses;
start->address=a;
start->pointer=NULL;
return;
}
List_of_Return_Addresses* p=start;
while(p->pointer!=NULL){
p=p->pointer;
}
p->pointer=new List_of_Return_Addresses;
p=p->pointer;
p->address=a;
p->pointer=NULL;
return;
}
void Stack::Pop(int & a){
if (start==NULL){
//printf("Stack is empty! Don't know where to go!\n");
//exit(0);
//exit(33);
a=-1;
return;
}
List_of_Return_Addresses* p=start;
List_of_Return_Addresses* p2=start;
int kol=0;
while(p->pointer!=NULL){
p2=p;
p=p->pointer;
kol++;
}
a=p->address;
delete p;
if (kol==0)
start=NULL;
else
p2->pointer=NULL;
return;
}
void Stack::Print(){
List_of_Return_Addresses* p=start;
while (p!=NULL){
//printf("Address=%d\n", p->address);
p=p->pointer;
}
}