Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdlib.h>
- using namespace std;
- template <typename type>
- class Stack {
- struct Element {
- type value;
- Element *prev; //wskaźnik na poprzedni element
- } *tail; //ogon listy
- public:
- Stack(){
- tail=NULL; // ogon listy - wierzchołek stosu na poczatku jest NULL
- };
- void push(type val){ //tworzy liste jedna kierunkowa ze wskaznikiem na wczesniejszy element listy
- struct Element *e = new Element;
- e->value = val;
- e->prev = tail;
- tail = e; // nowy element staje sie ogonem - wierzcholkiem stosu
- };
- type pop(){
- if(tail == NULL) {
- cout << "\nStos pusty!!!!\n" << endl << endl; // jesli tail NULL to stos pusty
- }
- else{
- type a = tail->value;
- struct Element *tmp = tail->prev;
- tail=NULL;
- delete tail;
- tail = tmp;
- return a;
- }
- };
- void wypisz(){
- if(tail==NULL){
- cout << "\nStos pusty!!!!\n" << endl;
- }
- else{
- struct Element *tmp;
- tmp = tail;
- cout << "\n---------- Stos: ----------\n";
- while(tail != NULL){
- cout << tail->value << endl;
- tail = tail -> prev;
- }
- tail = tmp;
- }
- };
- ~Stack(){
- while(tail!=NULL){
- this->pop();
- }
- if(tail==NULL) cout<<"\nUsunieto Stos\n";
- }
- };
- int main(){
- Stack<int> stack ;
- stack.push(10);
- stack.push(22);
- stack.push(36);
- stack.push(172);
- stack.wypisz();
- stack.pop();
- stack.wypisz();
- stack.pop();
- stack.wypisz();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement