Skip to content

Commit 3266cc4

Browse files
committed
use template class for Stack_LL.cpp
1 parent 092b7cb commit 3266cc4

File tree

1 file changed

+98
-69
lines changed

1 file changed

+98
-69
lines changed
Lines changed: 98 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,117 @@
11
#include <iostream>
2+
#include <stdlib.h>
23

34
using namespace std;
45

6+
template<class DataType>
57
class Node{
6-
public:
7-
int value;
8-
Node *next;
9-
10-
Node(int val, Node* ptr){
11-
value = val;
12-
next = ptr;
13-
}
14-
15-
~Node(){
16-
free(next);
17-
}
8+
public:
9+
DataType value;
10+
Node<DataType> *next;
11+
12+
Node(DataType val, Node<DataType>* ptr) {
13+
value = val;
14+
next = ptr;
15+
}
16+
17+
~Node(){
18+
free(next);
19+
}
20+
1821
};
1922

23+
template<class DataType>
2024
class Stack_LL{
25+
private:
26+
Node<DataType> *top;
27+
int len;
28+
29+
public:
30+
31+
Stack_LL(){
32+
top = NULL;
33+
len = 0;
34+
}
35+
36+
~Stack_LL(){
37+
Node<DataType>* tempNode;
38+
while( top != NULL ){
39+
tempNode = top;
40+
top = top->next;
41+
tempNode->next = NULL;
42+
free(tempNode);
43+
}
44+
}
45+
46+
bool is_empty(){
47+
return (top == NULL);
48+
}
49+
50+
void push(DataType value) {
51+
top = new Node<DataType>(value, top);
52+
len++;
53+
}
54+
55+
DataType pop(){
56+
57+
if( is_empty() )
58+
return -1;
59+
60+
DataType tempValue = top->value;
61+
62+
Node<DataType> *tempNode = top;
63+
top = top->next;
64+
tempNode->next = NULL;
65+
free(tempNode);
66+
len--;
67+
68+
return tempValue;
69+
70+
}
71+
72+
int length(){
73+
return len;
74+
}
75+
76+
void display(){
77+
if( ! is_empty() ){
78+
Node<DataType> *temp = top;
79+
while( temp != NULL){
80+
cout<<temp->value <<", ";
81+
temp = temp->next;
82+
}
83+
}
84+
}
2185

22-
private:
23-
Node *top;
24-
int len;
25-
26-
public:
27-
Stack_LL(){
28-
top = NULL;
29-
len = 0;
30-
}
31-
32-
~Stack_LL(){
33-
free(top);
34-
}
35-
36-
int is_empty(){
37-
return (top == NULL);
38-
}
39-
40-
void push(int value){
41-
top = new Node(value, top);
42-
len++;
43-
}
44-
45-
int pop(){
46-
if( is_empty() )
47-
return -1;
48-
Node *temp = top;
49-
top = top->next;
50-
len--;
51-
return temp->value;
52-
}
53-
54-
int length(){
55-
return len;
56-
}
57-
58-
void display(){
59-
if( ! is_empty() ){
60-
Node *temp = top;
61-
while( temp != NULL){
62-
cout<<temp->value <<", ";
63-
temp = temp->next;
64-
}
65-
}
66-
}
6786
};
6887

88+
6989
int main(){
7090

71-
Stack_LL S1;
91+
srand(time(NULL));
92+
93+
Stack_LL<int> S1;
94+
int temp;
95+
cout <<"Push 5 random values in stack :";
96+
for(int i = 0; i < 5; i++) {
97+
temp = random() % 100;
98+
cout<<"\nPushing : " <<temp;
99+
S1.push(temp);
100+
}
72101

73-
for(int i=1; i<5; i++){
74-
cout<<"\nPush " <<(i*10);
75-
S1.push(i*10);
76-
}
102+
cout<<"\n\nStack = ";
103+
S1.display();
77104

78-
cout<<"\n\nStack = ";
79-
S1.display();
105+
cout<<"\n\nPop: popped value = " <<S1.pop();
80106

81-
cout<<"\n\nPop, popped value = " <<S1.pop();
107+
cout<<"\n\nStack = ";
108+
S1.display();
82109

83-
cout<<"\n\nStack = ";
84-
S1.display();
110+
cout <<"\n\nPop stack until empty :";
111+
while( ! S1.is_empty() ){
112+
cout <<"\nPopped : " <<S1.pop();
113+
}
85114

86-
cout<<endl;
87-
return 0;
115+
cout<<endl;
116+
return 0;
88117
}

0 commit comments

Comments
 (0)