dsalglib
1.0.0
dsalglib is a ready to use data structures and algorithms library written in C++ . Object Oriented Template implementations are written.
Main Page
Namespaces
Classes
Files
File List
File Members
source
include
stack.h
Go to the documentation of this file.
1
//
2
// Created by moghya_s on 10/1/2016.
3
//
4
5
#ifndef DSALGLIB_STACK_H
6
#define DSALGLIB_STACK_H
7
namespace
dsa
{
8
9
template
<
class
type>
10
class
stack
{
11
private
:
12
13
struct
snode {
14
snode *prev;
15
type data;
16
snode *next;
17
18
snode() {
19
prev =
nullptr
;
20
next =
nullptr
;
21
}
22
23
snode(type param) {
24
prev =
nullptr
;
25
data = param;
26
next =
nullptr
;
27
}
28
};
29
snode *top;
30
long
long
int
count;
31
32
/*
33
Function: creates qnode of type type
34
Pre: param of type type to create qnode
35
Post: qnode created
36
*/
37
snode* create_snode(type param) {
38
snode *temp =
new
snode(param);
39
return
temp;
40
}
41
42
public
:
43
44
stack
() {
45
top =
nullptr
;
46
count = 0;
47
}
48
49
/*
50
* Function: Pushes element on stack
51
* Pre: param element of type type
52
* Post: element added
53
*/
54
type
push
(type param) {
55
snode *temp = create_snode(param);
56
if
(count == 0) {
57
top = temp;
58
}
else
{
59
top->next = temp;
60
temp->prev = top;
61
top = temp;
62
63
}
64
count++;
65
return
top->data;
66
}
67
68
/*
69
* Function: Pop element from stack
70
* Pre: none
71
* Post: element removed
72
*/
73
type
pop
() {
74
type param;
75
if
(count > 0) {
76
snode *temp = top->prev;
77
if
(count == 1) {
78
param = top->data;
79
delete
top;
80
top =
nullptr
;
81
}
else
{
82
param = top->data;
83
temp->next =
nullptr
;
84
delete
top;
85
top = temp;
86
}
87
count--;
88
}
89
return
param;
90
}
91
92
/*
93
* Function: gives top element
94
* Pre: none
95
* Post: top element returned
96
*/
97
type
top_element
() {
98
type param;
99
if
(count > 0)
100
param = top->data;
101
102
return
param;
103
}
104
105
/*
106
* Function: no of elements in stack
107
* Pre: none
108
* Post: size returned
109
*/
110
long
long
int
size
() {
111
return
count;
112
}
113
114
/*
115
* Function: checks if empty
116
* Pre: none
117
* Post: true if isempty else false
118
*/
119
bool
isempty
() {
120
if
(count == 0)
121
return
true
;
122
else
123
return
false
;
124
}
125
126
/*
127
* Function: Removes all elements from stack
128
* Pre: none
129
* Post: stack empty
130
*/
131
void
clear
() {
132
while
(count != 0) {
133
pop
();
134
}
135
}
136
};
137
}
138
#endif //DSALGLIB_STACK_H
dsa
Definition:
alginc.h:12
dsa::stack::isempty
bool isempty()
Definition:
stack.h:119
dsa::stack::stack
stack()
Definition:
stack.h:44
dsa::stack::clear
void clear()
Definition:
stack.h:131
dsa::stack::size
long long int size()
Definition:
stack.h:110
dsa::stack::top_element
type top_element()
Definition:
stack.h:97
dsa::stack
Definition:
stack.h:10
dsa::stack::push
type push(type param)
Definition:
stack.h:54
dsa::stack::pop
type pop()
Definition:
stack.h:73
Generated by
1.8.11