-
Notifications
You must be signed in to change notification settings - Fork 0
/
StackLink.cpp
82 lines (73 loc) · 999 Bytes
/
StackLink.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
#include "StackLink.h"
#include <iostream>
using namespace std;
int StackLink::IsEmpty()
{
return head->data == 0;
}
void StackLink::ShowData()
{
if (IsEmpty())
cout << "Õ»¿Õ";
else
{
LinkNodeP p = head;
while (p->next)
{
p = p->next;
cout << p->data << " ";
}
}
cout << endl;
}
void StackLink::Push(int data)
{
LinkNodeP p = new LinkNode, temp=head->next;
p->data = data;
head->next = p;
p->next = temp;
head->data++;
}
int StackLink::Pop()
{
if (IsEmpty())
return 0;
else
{
head->data--;
LinkNodeP p = head->next,temp=p->next;
int result = p->data;
delete p;
head->next = temp;
return result;
}
}
int StackLink::Top()
{
if (IsEmpty())
return 0;
else
{
return head->next->data;
}
}
int StackLink::Count()
{
return head->data;
}
StackLink::StackLink()
{
head = new LinkNode;
head->data = 0;
head->next = NULL;
}
StackLink::~StackLink()
{
LinkNodeP p = head,temp;
while (p != NULL)
{
temp = p->next;
delete p;
p = temp;
}
}