Stos C++ na strukturach

0
#include <iostream>

using namespace std;

struct stos       
{
	int l;			
	stos *prev;		
};

void push(stos **s, int liczba)		
{
	stos *nowy = new stos;		
	nowy->prev = *s;		
	nowy->l = liczba;		
	*s = nowy;		
}

void pop(stos **s)
{
	if ((*s)->prev != NULL)
	{
		cout << (*s)->l << endl;
		stos *nowy = *s;
		nowy = (*s)->prev;
		*s = nowy;
		//delete nowy;
	}
	else
	{
		cout << "Stos jest pusty\n";
	}
	
}

void isEmpty(stos **s)
{
	if ((*s)->prev == NULL)
		cout << "Stos jest pusty\n";
	else
		cout << "Stos nie jest pusty\n";
}


int main()
{
	stos *stosik = new stos; 
	stosik->prev = NULL;
	stosik->l = NULL;

	push(&stosik, 4);
	push(&stosik, 6);
	push(&stosik, 9);

	pop(&stosik);
	pop(&stosik);
	pop(&stosik);
	isEmpty(&stosik);

	cin.get();
	delete stosik;
	return 0;
}
 

Czy dobrze zrobiłem ten stos? Czy czegoś tutaj brakuje?

0

Skoro używasz C++... to go używaj.
Powinieneś się wzorować na stosie z biblioteki standardowej, aka http://www.cplusplus.com/reference/stack/stack/
Czym więc powinien być stos? Adapterem!

Swoją drogą masz tutaj tylko inaczej nazwaną... linked list.
A o liście dołączanej(podczepianej?) możesz poczytać chociażby tutaj: _

0
#include <iostream>

using namespace std;

struct stack
{
	int value;
	int size = 0;
	stack *next;
};

struct stack *wierzch = NULL;


void push(int num)
{
	if (wierzch == NULL)
	{
		stack *temp;
		temp = wierzch;
		wierzch = new stack;
		wierzch->size = wierzch->size + 1;
		wierzch->value = num;
		wierzch->next = temp;
	}
	else
	{
		stack *temp;
		temp = wierzch;
		temp->size++;
		wierzch = new stack;
		wierzch->size = temp->size;
		wierzch->value = num;
		wierzch->next = temp;
	}
}

int pop()
{
	if (wierzch != NULL)
	{
		int r;
		stack* temp;
		r = wierzch->value;
		temp = wierzch->next;
		delete wierzch;
		wierzch = temp;
		return r;
	}
	
}

bool empty()
{
	if (wierzch == NULL)
		return true;
	else return false;
}

int top()
{
	return wierzch->value;
}

int size()
{
	return wierzch->size;
}

int main()
{
	push(2);
	push(4);
	push(6);
	pop();
	cout << size();
	cin.get();
	return 0;
}
 

Cos takiego moze byc?

0

Stos powinien być tylko adapterem. Takim, jak ten teraz machnięty:

#include <iostream>
#include <deque>
using namespace std;

template<
	typename T, 
	class Container = deque<T>
> class stack{
public:
	using value_type = T;
	using container_type = Container;
	using reference = typename container_type::reference;
	using const_reference = typename container_type::const_reference;
	using size_type = ::size_t;
	
	stack(const container_type &container_): container(container_){}
	stack(container_type &&container_ = container_type()): container(container_){}
	
	bool empty() const{
		return container.empty();
	}
	
	size_type size() const{
		return container.size();
	}
	
	reference &top(){
		return container.back();
	}
	
	const reference &top() const{
		return container.back();
	}
	
	void push(const value_type &val){
		container.push_back(val);
	}
	
	void push(value_type &&val){
		container.push_back(val);
	}
	
	template <class... Args> 
	void emplace(Args &&...args){
		container.emplace_back(args...);
	}
	
	void pop(){
		container.pop_back();
	}
private:
	container_type container;
};

template<typename T>
struct push_many_proxy{
	using pushable_type = T;
	using value_type = typename pushable_type::value_type;
	pushable_type &pushable;
	
	push_many_proxy(pushable_type &pushable_): pushable(pushable_){}
	
	push_many_proxy &operator()(const value_type &val){
		pushable.push(val);
		return *this;
	}
	
	push_many_proxy &operator()(value_type &&val){
		pushable.push(val);
		return *this;
	}
};

template<typename T>
push_many_proxy<T> push_many(T &pushable){
	return push_many_proxy<T>(pushable);
}


int main(){
	using nums_stack = stack<int>;
	nums_stack nums;
	push_many(nums)(1)(2)(3)(4)(5);
	
	while(!nums.empty()){
		cout << nums.top();
		nums.pop();
	}
	return 0;
}

Dodałem drobny smaczek w nawiązaniu do tego wątku: http://4programmers.net/Forum/C_i_C++/255088-boost_dziwna_konstrukcja

uruchomienie(na ideone)</code> | Wyjście: <code class="python">54321

Oczywiście, gdyby takie proxy miałoby być więcej używane, powinno raczej używać jakiegoś insertera:

template<typename Inserter>
struct insert_many_proxy{
	using inserter_type = Inserter;
	
	inserter_type inserter;
	
	insert_many_proxy(inserter_type inserter_): inserter(inserter_){}
	
	template<typename T>
	insert_many_proxy &operator()(const T &val){
		inserter = val;
		return *this;
	}
	
	template<typename T>
	insert_many_proxy &operator()(T &&val){
		inserter = val;
		return *this;
	}
};

template<typename Inserter>
insert_many_proxy<Inserter> insert_many(Inserter inserter){
	return insert_many_proxy<Inserter>(inserter);
}

1 użytkowników online, w tym zalogowanych: 0, gości: 1