Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- template <class T>
- class myStack {
- private:
- T data;
- myStack *next;
- public:
- myStack(T x, myStack *n): data(x), next(n) {}
- myStack(): next(NULL) {}
- void push(T x) {
- next = new myStack(x, next);
- }
- bool pop() {
- if (next != NULL) {
- myStack *D = next;
- next = next -> next;
- delete D;
- return true;
- }
- else return false;
- }
- T top() {
- if (next != NULL)
- return next -> data;
- }
- int count() {
- int i = 0;
- for (myStack *cur = next; cur != NULL; cur = cur -> next)
- i++;
- return i;
- }
- void display() {
- if (next != NULL)
- for (myStack *cur = next; cur != NULL; cur = cur -> next)
- cout << cur -> data << " ";
- else
- cout << "< empty >";
- cout << endl;
- }
- bool empty() {
- return bool(next);
- }
- void sort() {
- if (next != NULL)
- for (myStack *prev = next; prev -> next != NULL; prev = prev -> next)
- for (myStack *cur = prev -> next; cur != NULL; cur = cur -> next)
- if(prev -> data > cur -> data)
- swap(prev -> data, cur -> data);
- }
- };
- int main() {
- myStack <double> D;
- D.push(2.1);
- D.push(1.9);
- D.push(3.1);
- D.display();
- D.sort();
- D.display();
- D.pop();
- D.display();
- D.pop();
- cout << D.top() << endl;
- D.pop();
- D.display();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement