-
Notifications
You must be signed in to change notification settings - Fork 0
/
linkStack.hpp
71 lines (64 loc) · 1.38 KB
/
linkStack.hpp
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
//
// linkStack.hpp
// datastructure
//
// Created by duchunhui on 2018/10/18.
// Copyright © 2018 duchunhui. All rights reserved.
//
#ifndef linkStack_hpp
#define linkStack_hpp
#include <stdio.h>
#include "stack.hpp"
template <class elemType>
class linkStack:public stack<elemType>{
private:
struct node{
elemType data;
node* next;
node(const elemType &x,node* n=NULL):data(x),next(n){}
node(node* n=NULL):next(n){}
~node(){}
};
node *top_p;
public:
linkStack();
~linkStack();
void push(const elemType &x);
elemType pop();
elemType top() const;
bool isEmpty() const;
};
template <class elemType>
linkStack<elemType>::linkStack(){
top_p = NULL;
}
template <class elemType>
linkStack<elemType>::~linkStack(){
node* tmp;
while (top_p!=NULL) {
tmp = top_p;
top_p = top_p->next;
delete tmp;
}
}
template <class elemType>
void linkStack<elemType>::push(const elemType &x){
top_p = new node(x,top_p);
}
template <class elemType>
elemType linkStack<elemType>::pop(){
node* p = top_p;
elemType x = p->data;
delete p;
top_p = top_p->next;
return x;
}
template <class elemType>
elemType linkStack<elemType>::top() const{
return top_p->data;
}
template <class elemType>
bool linkStack<elemType>::isEmpty() const{
return top_p == NULL;
}
#endif /* linkStack_hpp */