MCPcopy Create free account
hub / github.com/codemistic/Data-Structures-and-Algorithms / _stack

Class _stack

CPP/structures/stack using linked list.cpp:8–68  ·  view source on GitHub ↗

class for _stack

Source from the content-addressed store, hash-verified

6
7// class for _stack
8class _stack
9{
10 // private data---->
11
12
13 // node structure
14 struct node
15 {
16 int data;
17 node* next;
18 };
19 node* head = NULL;
20 int n = 0;// for size of stack
21public:
22 // public data---->
23
24
25 // push fuction
26 void push(int a) {
27 // making node
28 node* temp = new node();
29 // check whether a stack overflows or not
30 if(temp==NULL){cout<<"Stack Overflow\n";return;}
31
32 // adding nodes at the beginning of linkked list
33 temp->data = a;
34 temp->next = head;
35 head = temp;
36 n++;// increamenting size
37 }
38
39 // pop function
40 void pop() {
41 // check stack is empty or not
42 if (head == NULL) {cout << "Empty stack\n"; return;}
43 // points temp node pointer to the top
44 node *temp = head;
45 // move head to next node
46 head = head->next;
47 // delete temp node pointer
48 delete(temp);
49 n--;// decreamenting size
50 }
51
52 // top function
53 int top() {
54 // check stack is empty or not
55 if (head == NULL) {cout << "Empty stack"; return -1;}
56 // return top node data
57 return head->data;
58 }
59
60 // stack emptly check function
61 bool isEmpty() {
62 if (head == NULL)return true;
63 else return false;
64 }
65

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected