Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // zad9hawryl2.cpp : Defines the entry point for the console application.
- //
- /*
- Opracuj klasę do budowy stosu w oparciu o listę dowiązaniową. Nowe elementy są wstawiane na początku listy.
- Wykorzystaj następujące klasy:
- struct Element {
- int dane;
- Element * nast;
- // konstruktor
- Element(int n, Element* p) : dane(n), nast(p)
- {}
- };
- class Stos {
- Element* szczyt; // wskaźnik na element na szczycie stosu
- int lb_elem; // rozmiar stosu
- public:
- Stos (); // konstruktor domyślny
- ~Stos(); // destruktor
- Stos(Stos & s); // konstruktor kopiujący
- Stos& operator=(Stos const &s); // przypisanie
- void push(int n); // wstawianie na stos
- void pop(); // zdejmowanie ze stosu
- int top() const; // udostepnienie liczby na szczycie stosu
- int & top(); // udostepnienie referencji do liczby
- // na szczycie stosu
- int size() const; // bieżący rozmiar stosu
- bool empty() const; // czy stos jest pusty
- }
- */
- #include "stdafx.h"
- #include <iostream>
- using namespace std;
- // Element( int dane, Element* nast)
- struct Element {
- int dane;
- Element * nast;
- // konstruktor
- Element(int n, Element* p) : dane(n), nast(p) {}
- };
- //Stos(Element* szczyt, int lb_elem);
- class Stos {
- Element* szczyt; // wskaźnik na element na szczycie stosu
- int lb_elem; // rozmiar stosu
- public:
- // konstruktor domyślny
- Stos (){
- szczyt = NULL;
- lb_elem = 0;
- }
- // destruktor
- ~Stos(){
- Element* nast;
- Element* tmp = szczyt;
- while(tmp != NULL){
- nast = tmp->nast;
- delete tmp;
- tmp = nast;
- }
- };
- // konstruktor kopiujący
- Stos(Stos & s){
- if(s.szczyt != NULL){
- Element* nast;
- Element* tmp = s.szczyt;
- szczyt = s.szczyt;
- while(tmp != NULL){
- nast = tmp->nast;
- tmp = nast;
- }
- lb_elem = s.lb_elem;
- }
- }
- // przypisanie
- Stos& operator=(Stos const &s){
- if(s.szczyt != NULL){
- Element* nast;
- Element* tmp = s.szczyt;
- szczyt = s.szczyt;
- while(tmp != NULL){
- nast = tmp->nast;
- tmp = nast;
- }
- lb_elem = s.lb_elem;
- }
- return *this;
- }
- // wstawianie na stos
- void push(int n){
- Element* nowyElem = new Element(n,szczyt);
- szczyt = nowyElem;
- lb_elem++;
- }
- // zdejmowanie ze stosu
- void pop(){
- lb_elem--;
- szczyt = szczyt->nast;
- }
- // udostepnienie liczby na szczycie stosu
- int top() const{
- return szczyt->dane;
- }
- // udostepnienie referencji do liczby na szczycie stosu
- int& top(){
- int* tmp = new int;
- *tmp = (szczyt->dane);
- return *tmp;
- }
- // bieżący rozmiar stosu
- int size() const{
- return lb_elem;
- }
- // czy stos jest pusty
- bool empty() const{
- if(lb_elem == 0 ) return true;
- else return false;
- }
- friend ostream& operator<<(ostream& os, Stos& stos){
- Element* nast = stos.szczyt;
- Element* tmp = stos.szczyt;
- while(tmp != NULL){
- os << nast->dane << " ";
- nast = tmp->nast;
- tmp = nast;
- }
- os << "Rozmiar: " << stos.lb_elem << endl;
- return os;
- }
- };
- int _tmain(int argc, _TCHAR* argv[]){
- Stos s1;
- s1.push(2);
- s1.push(4);
- s1.push(6);
- cout << s1 << endl;
- Stos s2 = s1;
- cout << s2 << endl;
- s2.pop();
- cout << s2 << endl;
- int* a = &s2.top();
- cout << a << endl;
- getchar();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment