-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstacklinkedlist.cpp
More file actions
96 lines (85 loc) · 1.53 KB
/
stacklinkedlist.cpp
File metadata and controls
96 lines (85 loc) · 1.53 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include<iostream>
using namespace std;
struct stackN{//stack ADT
int data;
stackN* next;
public:
stackN(int val){
data = val;
next = NULL;
}
};
class stack{
private:
//stackN* node;
stackN* top;//holds the address of last node
public:
stack(){
top = NULL;
}
~stack(){
stackN* temp=top;
stackN*ptr;
while(temp){
ptr=temp;
delete ptr;
temp=temp->next;
}
cout<<"done";
}
/*stack(int d){
stackN* ins=new stackN(d);
//ins->data=d;
//ins->next=NULL;
node=ins;
top=ins;
}*/
void push(int d);
int pop();
void traverse();
int isempty();
};
void stack:: push(int d){
// cout<<"!";
stackN* ins=new stackN(d);
//ins->data=d;
//ins->next=NULL;
ins->next = top;
top = ins;
//top->next=ins;
//top=ins;
}
int stack:: pop(){
if(top){
stackN* ptr = top;
top=ptr->next;
int k=ptr->data;
ptr->data=0;
delete ptr;
ptr=NULL;
return k;
}
cout<<"stack is empty";
return 100000;
}
int stack:: isempty(){
if(top==NULL)
return 1;
return 0;
}
void stack:: traverse(){//traverse the stack.
stackN* ptr=top;
while(ptr){
cout<<ptr->data;
ptr=ptr->next;
}
}
int main(){
stack s;
s.push(10);
s.push(3);
//s.push(2);
//s.traverse();
//cout<<s.pop()<<"\n";
s.traverse();
}