Created
July 12, 2019 00:05
-
-
Save Kadajett/e764f343b4cd2892af3a4182973929db to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
// #include <bits/stdc++.h> | |
using namespace std; | |
struct Node { | |
int (*func)(string); | |
string param; | |
struct Node* link; | |
}; | |
struct Node* top; | |
// Utility function to add an element data in the stack | |
// insert at the beginning | |
void push(int (*newFunc)(string), string params) { | |
// create new node temp and allocate memory | |
struct Node* temp; | |
temp = new Node(); | |
// check if stack (heap) is full. Then inserting an element would | |
// lead to stack overflow | |
if (!temp) { | |
cout << "\nHeap Overflow"; | |
exit(1); | |
} | |
// initialize data into temp data field | |
temp->func = newFunc; | |
temp->param = params; | |
// put top pointer reference into temp link | |
temp->link = top; | |
// make temp as top of Stack | |
top = temp; | |
} | |
// Utility function to check if the stack is empty or not | |
int isEmpty() { | |
return top == NULL; | |
} | |
// // Utility function to return top element in a stack | |
Node *peek() { | |
// check for empty stack | |
if (!isEmpty()) | |
return top; | |
else | |
exit(1); | |
} | |
// Utility function to pop top | |
// element from the stack | |
void pop() { | |
struct Node* temp; | |
// check for stack underflow | |
if (top == NULL) { | |
cout << "\nStack Underflow" << endl; | |
exit(1); | |
} | |
else { | |
// top assign into temp | |
temp = top; | |
// assign second node to top | |
top = top->link; | |
// destroy connection between first and second | |
temp->link = NULL; | |
// release memory of top node | |
free(temp); | |
} | |
} | |
// Function to print all the | |
// elements of the stack | |
void display() { | |
struct Node* temp; | |
// check for stack underflow | |
if (top == NULL) { | |
cout << "\nStack Underflow"; | |
exit(1); | |
} | |
else { | |
temp = top; | |
while (temp != NULL) { | |
// print node data | |
// cout << temp->newFunc << " "; | |
// assign temp link to temp | |
temp = temp->link; | |
} | |
} | |
} | |
int testFunc1(string param) { | |
cout << "testFunc1 " << param; | |
return 0; | |
} | |
int testFunc2(string param) { | |
cout << "testFunc2 " << param; | |
return 0; | |
} | |
int testFunc3(string param) { | |
cout << "testFunc3 " << param; | |
return 0; | |
} | |
int testFunc4(string param) { | |
cout << "testFunc4 " << param; | |
return 0; | |
} | |
int main() { | |
bool keepAlive = true; | |
push(&testFunc1, "asdf"); | |
push(&testFunc2, "qwer"); | |
push(&testFunc3, "zcxvx"); | |
push(&testFunc4, "fghj"); | |
while (keepAlive) { | |
// QueueItem current = *s1.pop(); | |
// int res = (current.func)(current.params); | |
// top.func(top.param); | |
// (*top.func)->(top.param); | |
Node current = *peek(); | |
current.func(current.param); | |
pop(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment