Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Header file for abstract data type "STACK" implemented using a linked list */
- #ifndef STACK_H
- #define STACK_H
- template <class T>
- class stack {
- public:
- void push(T); //Function that inserts elements into the stack
- bool empty(); //Function to test whether the stack is empty
- T top(); //Returns top element of stack
- void pop(); //Removes element at the top of the stack
- int size(); //Returns size of stack
- void print(); //Prints stack contents
- struct node { //Definition of node structure with constructor and destructor
- T node_data;
- node *next;
- //default ctor
- node() { next = nullptr; }
- //default dtor
- ~node() { delete root_node; }
- };
- private:
- node *root_node = nullptr;
- int elements = 0;
- };
- #endif //STACK_H
- /* Definitons of the STACK data structure implemented using a linked list */
- #include <iostream>
- #include "stack.h"
- using std::cout;
- using std::endl;
- /* FUNCTION: Test to check if the stack is empty or has at least one element */
- template <class T>
- bool stack<T>::empty() { return (root_node == nullptr); }
- /* FUNCTION: Returns current size of the stack */
- template <class T>
- int stack<T>::size() { return elements; }
- /* FUNCTION: Adds nodes to the stack with one argument which is the data to be inserted */
- template <class T>
- void stack<T>::push(T data) {
- //Operation to preform if the stack is empty.
- //Root element is popped off last (First in, Last out)
- if ( empty() ) {
- root_node = new node;
- root_node->node_data = data;
- root_node->next = nullptr;
- elements++;
- }
- //Operation to preform if stack is not empty.
- //Elements inserted into stack with dynamic allocation.
- else {
- node *new_node = new node;
- *new_node = *root_node;
- root_node->next = new_node;
- root_node->node_data = data;
- elements++;
- }
- }
- /* FUNCTION: Removes element at the top of the stack */
- template <class T>
- void stack<T>::pop() {
- if (size() > 1) {
- node *temp_node = new node;
- temp_node = root_node->next;
- root_node = temp_node;
- elements--;
- }
- else if (size() == 1) {
- root_node = nullptr;
- elements--;
- }
- else {cout << "nOperation pop() failed: Stack is empty!" << endl;}
- }
- /* FUNCTION: Retrieves element at the top of the stack */
- template <class T>
- T stack<T>::top() {
- if (!empty()) {return root_node->node_data;}
- else {cout << "nOperation top() failed: Stack is empty!" << endl; return -1;}
- }
- /* FUNCTION: Prints the stack contents */
- template <class T>
- void stack<T>::print() {
- int index = size();
- for (int i = 1; i < index; i++) {
- cout << "Element " << i << ": " << " " << top() << endl;
- pop();
- }
- }
- void function(const argument& a);
- void function(argument& a); // a is modified in the function (I/O parameter)
- void function(argument a); // function "owns" a (gets it's own exclusive copy)
- template<typename T>
- void stack<T>::push(T data) // pass by value here
- {
- root = new node{ std::move(data), root }; // and move the value here
- ++elements;
- }
- template<typename T>
- const T& // return a const T&
- stack<T>::top() const // and function is const
- {
- if (!root)
- throw std::runtime_error("stack<T>::top: empty stack");
- return root->data;
- }
- int size();
- std::size_t size() const;
- root_node = new node;
- root_node->node_data = data;
- root_node->next = nullptr;
- root_node = new node{ std::move(data), root_node }; // use std::move on the data
- template <class T>
- class stack {
- // ...
- struct node { // struct, because it is internal and only
- // access will be in stack
- // (i.e. we can guarantee correct use in client code)
- T *node_data;
- node* next;
- };
- }
- template <class T>
- void stack<T>::pop() {
- if(empty())
- throw std::runtime_error{"stack<T>::pop(): empty stack"};
- node* new_root = root_node->next;
- delete root_node;
- root_node = new_root;
- --elements;
- }
- struct node {
- node *next;
- //default ctor
- //default dtor
- ~node() { delete root_node; }
- #include <iostream>
- #include "stack.h"
- using std::cout;
- using std::endl;
- void stack<T>::push(T data) {
- root_node = new node; // `node` contains a T and it must be
- // be created by default construction.
- root_node->node_data = data; // Force a copy of the object into node.
- void stack<T>::push(T data) {
- root_node = new node(data, root_node);
- elements++;
- }
- node *temp_node = new node; // Create a node we don't need.
- temp_node = root_node->next; // Then we leak it the net line.
- root_node = temp_node; // The old value of root node was just
- // lost and no way to delete it.
- void stack<T>::pop() {
- node* tmp = root_node; // Keep track of the old root.
- root_node = root_node->next; // Remove the root node from the list.
- elements--;
- delete tmp; // delete the old root.
- }
- void print(std::ostream& stream = std::cout);
- else {cout << "nOperation top() failed: Stack is empty!" << endl; return -1;}
- else { throw std::runtime_error("XXXX");}
- class Stack
- {
- public:
- bool empty() const;
- int size() const;
- #ifndef STACK_H
- #define STACK_H
- #include <iostream>
- #include <stdexcept>
- template <typename T>
- class Stack {
- struct Node {
- T data;
- Node* next;
- Node(T const& data, Node* next)
- : data(data)
- , next(next)
- {}
- Node(T&& data, Node* next) // With C++11 allow move construction
- : data(std::move(data))
- , next(next)
- {}
- };
- public:
- ~Stack();
- void push(T const& data);
- void push(T&& data); // WIth C++ 11 allow move construction
- bool empty() const;
- int size() const;
- T top() const;
- void pop();
- void print(std::ostream& str = std::cout) const;
- private:
- Node* root = nullptr;
- int elements = 0;
- };
- template<typename T>
- Stack<T>::~Stack()
- {
- Node* next;
- for(Node* loop = root; loop != nullptr; loop = next)
- {
- next = loop->next;
- delete loop;
- }
- }
- template<typename T>
- void Stack<T>::push(T const& data)
- {
- root = new Node(data, root);
- ++elements;
- }
- template<typename T>
- void Stack<T>::push(T&& data)
- {
- root = new Node(std::forward(data), root);
- ++elements;
- }
- template<typename T>
- bool Stack<T>::empty() const
- {
- return root == nullptr;
- }
- template<typename T>
- int Stack<T>::size() const
- {
- return elements;
- }
- template<typename T>
- T Stack<T>::top() const
- {
- if (root == nullptr)
- { throw std::runtime_error("Invalid Action");
- }
- return root->data;
- }
- template<typename T>
- void Stack<T>::pop()
- {
- if (root == nullptr)
- { throw std::runtime_error("Invalid Action");
- }
- Node* tmp = root;
- root = root->next;
- --elements;
- delete tmp;
- }
- template<typename T>
- void Stack<T>::print(std::ostream& str) const
- {
- int id = 0;
- for(Node* loop=root; loop != nullptr; loop=loop->next, ++id)
- {
- str << "Element: " << id << " = " << loop->data << "n";
- }
- }
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement