Created
May 30, 2018 13:51
-
-
Save tsathis/1b4843f99cd6117054a1349f2e13a8b1 to your computer and use it in GitHub Desktop.
Integer Stack class using Linked Lists C++
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> | |
//Node class | |
class Node { | |
private: | |
//private members | |
int data; | |
Node *next; | |
public: | |
//Setters and Getters | |
void setData(int v) { data = v; } | |
void setNext(Node *p) { next = p; } | |
int getData() { return data; } | |
Node *getNext() { return next; } | |
}; | |
//Integer Linked List class | |
class IntLinkedList { | |
private: | |
Node *first; | |
public: | |
//Construct. | |
IntLinkedList() { first = NULL; } | |
//func. add to start | |
void addBeg(int v) { | |
Node *node = new Node; | |
(*node).setData(v); | |
(*node).setNext(first); | |
first = node; | |
} | |
//func: add to end. | |
void addEnd(int v) { | |
Node *node = new Node; | |
(*node).setData(v); | |
(*node).setNext(NULL); | |
if (first == NULL) { | |
first = node; | |
return; | |
} | |
Node *nodeLast = new Node; | |
nodeLast = first; | |
while ((*nodeLast).getNext() != NULL) { | |
nodeLast = (*nodeLast).getNext(); | |
} | |
(*nodeLast).setNext(node); | |
} | |
//func. : del one from start | |
void deleteBeg() { | |
if (first == NULL) | |
return; | |
Node *node = new Node; | |
node = first; | |
first = (*first).getNext(); | |
delete node; | |
} | |
//func. : del one from end | |
void deleteEnd() { | |
if (first == NULL) | |
return; | |
Node *node = new Node; | |
Node *nodeBeforeLast = new Node; | |
node = first; | |
while ((*node).getNext() != NULL) { | |
nodeBeforeLast = node; | |
node = (*node).getNext(); | |
} | |
(*nodeBeforeLast).setNext(NULL); | |
delete node; | |
} | |
//func. : del the nodes containing the given value | |
void deleteVal(int v) { | |
if (first == NULL) | |
return; | |
Node *node = new Node; | |
Node *previousNode = new Node; | |
node = first; | |
while (node != NULL) { | |
if ((*node).getData() == v) { | |
first == (*node).getNext(); | |
if (node == first) { | |
first = (*node).getNext(); | |
} else { | |
(*previousNode).setNext((*node).getNext()); | |
} | |
} | |
previousNode = node; | |
node = (*node).getNext(); | |
} | |
} | |
//print the list | |
void printList() { | |
if (first == NULL) | |
return; | |
Node *node = new Node; | |
node = first; | |
std::cout << "\nList: "; | |
while (node != NULL) { | |
std::cout << (*node).getData() << " "; | |
node = (*node).getNext(); | |
} | |
std::cout << "\n"; | |
} | |
//reverse the list (Exercise 2) | |
void reverseList() { | |
if (first == NULL) | |
return; | |
Node *before = NULL, *after = NULL, *node; | |
node = first; | |
while (node != NULL) { | |
after = (*node).getNext(); | |
(*node).setNext(before); | |
before = node; | |
node = after; | |
} | |
first = before; | |
} | |
//return the last value of list | |
int getLast() { | |
if(first==NULL) | |
return -1; | |
Node *last = new Node; | |
last = first; | |
while ((*last).getNext() != NULL) { | |
last = (*last).getNext(); | |
} | |
return (*last).getData(); | |
} | |
}; | |
//Stack class | |
class Stack { | |
private: | |
int top; | |
IntLinkedList list; | |
public: | |
//func: Add to top | |
void push(int v) { list.addEnd(v);} | |
//func: Delete the top | |
void pop() { list.deleteEnd(); } | |
//func: return top | |
int peep() { return list.getLast(); } | |
//print stack | |
void printStack() { list.printList(); } | |
//reverse stack | |
void reverseStack() { list.reverseList(); } | |
}; | |
//func. MAIN | |
int main() { | |
Stack stack; //obj: stack | |
for (;;) { //endless loop | |
//getting options and executing according to the given option | |
char option; | |
std::cout << "Available Options:" | |
<< "\n"; | |
std::cout << "\t1 --> Add an element to the top of the stack." | |
<< "\n"; | |
std::cout << "\t2 --> Remove an element from the top of the stack" | |
<< "\n"; | |
std::cout << "\t3 --> View the value of the topmost element." | |
<< "\n"; | |
std::cout << "\t4 --> Print all the values" | |
<< "\n"; | |
std::cout << "\t5 --> Reverse all the values in the stack." | |
<< "\n"; | |
std::cout << "\nEnter operation number(1-5):" | |
<< " "; | |
std::cin >> option; | |
int val; | |
switch (option) { | |
case '1': { | |
std::cout << "\nEnter Value(Integer):" | |
<< " "; | |
std::cin >> val; | |
stack.push(val); | |
stack.printStack(); | |
} break; | |
case '2': { | |
stack.pop(); | |
stack.printStack(); | |
} break; | |
case '3': { | |
int top = stack.peep(); | |
std::cout << "\nTopmost element:\t" << top << "\n"; | |
} break; | |
case '4': { | |
stack.printStack(); | |
} break; | |
case '5': { | |
stack.reverseStack(); | |
stack.printStack(); | |
} break; | |
default: | |
std::cout << "\nNot a valid option." | |
<< "\n"; | |
std::cin.get(); | |
} | |
std::cin.get(); | |
std::cin.get(); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment