-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.cpp
More file actions
executable file
·102 lines (83 loc) · 1.75 KB
/
stack.cpp
File metadata and controls
executable file
·102 lines (83 loc) · 1.75 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
97
98
99
100
101
102
/*
* File: main.cpp
* Author: ngupta94
*
* Created on January 21, 2014, 11:53 AM
*/
#include <cstdlib>
#include <iostream>
using namespace std;
struct node //one element of stack
{
int data; //data item
node* next; //pointer to next link
};
class stack //a list of links
{
private:
node* head; //pointer to first link
public:
stack() //no-argument constructor
{
head = NULL;
} //no first link
void push(int d); //add data item (one link)
void display(); //display all link
int pop();
};
void stack::push(int d) //add data item
{
node *newNode = new node;
newNode->data = d;
newNode->next = head;
head = newNode;
return;
}
int stack::pop() {
node *tmp;
int d;
if (head == NULL) {
cout << "Stack Empty " << endl;
return(-1);
}
else {
tmp = head;
d = head->data;
head = head->next;
delete tmp;
}
return(d);
}
void stack::display() //display all links
{
node* current = head; //set ptr to first link
while (current != NULL) //quit on last link
{
cout << current->data << endl; //print data
current = current->next; //move to next link
}
}
/*
*
*/
int main(int argc, char** argv) {
stack q;
int ret;
ret = q.pop();
if (ret >=0 ) {
cout << "stack out = " << ret << endl;
}
q.push(56);
q.push(23);
q.push(87);
ret = q.pop();
if (ret >=0 ) {
cout << "stack out = " << ret << endl;
}
ret = q.pop();
if (ret >=0 ) {
cout << "stack out = " << ret << endl;
}
q.display();
return 0;
}