wkładanie 2 elementu na stos

0

Witam mam problem z metodą Push ponieważ program wysypuje się przy nałożeniu 2 elementu na stos. Proszę was o pomoc i z góry dziękuję.
tak wygląda definicja klasy

 
class stack
{
public:
    stack();
    ~stack();
    void push(int a);
    int pop();
    void clear();
    bool isempty();

private:
    int top;
    int *dane;
    int size;
};
 #include <assert.h>
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include "stack.h"

using namespace std;

stack::stack()
{
	this->top=0;
	this->size=1;
	this->dane=new int[1];
}

stack::~stack()
{
}

void stack::clear()
{
	this->top=0;
	this->size=1;
}

void stack::push(int a)
{

if(top==(size-1)){

	int newsize=2*(this->size);
	int *newdane=new int[newsize];

	int newtop=newsize-top-1;
	int i=newsize; 
	int x=size;

		if(newdane){
		
			for(int k=0;k<top;k++){

				newdane[i-k]=this->dane[x-k];
			}

			this->top=newtop;
			this->size=newsize;
			this->dane=newdane;
			delete[] newdane;
			}

		this->dane[this->top]=a;
		this->top--;
		}

else{
	
	this->dane[this->top]=a;
	this->top--;
	}
}

int stack::pop()
{
	this->top--;
	return this->dane[this->top+1];
}

bool stack::isempty(){

if( (this->top) == 0 )
	 return true;
else
	 return false;
}
0

ponieważ próbujesz wykonać: dane[-1]=a;

0

problem pojawia się podczas petli for w czasie przypisania starych danych do nowych danych

0

Przepisz całość:

  1. Nie wpisuj liczb od końca tablicy tylko od początku (dziwny ten pomysł - od tyłu).
  2. Użyj funkcji memcpy()
  3. Nie powtarzaj fragmentów kodu.
  4. Nie używaj bez sensu this
  5. Napisz poprawnie destruktor
  6. Nie używaj int dla określenia rozmiaru, użyj unsigned lub size_t
0

wrzucam te metody jeszcze raz ale problem pojawia się teraz nie przy przypsywaniu a do elementu tablicy przy 2 i 3 razie gdy inciuje push

 #include <assert.h>
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include "stack.h"
#include <string>

using namespace std;

stack::stack()
{
	this->top=0;
	this->size=1;
	this->dane=new int[1];
}

stack::~stack()
{
}

void stack::clear()
{
	this->top=0;
	this->size=1;
}

void stack::push(int a)
{
	static int n = 1;
if(size<n){

	int newsize=2*(size);
	int *newdane=new int[newsize];
	
	if (newdane == NULL) cout << "nie mozna stworzyc wiekszej tablicy" << endl;
	else{

		if (newdane){

			for (int k = 0; k < top; k++){
				memcpy(&newdane[k],&dane[k],sizeof(int));
				
			}

			size = newsize;
			delete[] dane;
			dane = newdane;
			n++;
			dane[top++] = a;

		}
		
		
		
	}
		}

else{
	n++;
	memcpy(&dane[top++],&a,sizeof(int));
	
	}
}

int stack::pop()
{
	
	return dane[top--];
}

bool stack::isempty(){

if( (this->top) == 0 )
	 return true;
else
	 return false;
}
0

Z 6 punktów tylko dwa zrozumiałeś :/
Trzeba myśleć zamiast kombinować, to jest całość:

class stack
  {
   private:
   unsigned top,size;
   int *dane;
   public:
   stack():top(0),size(0),dane(0) {}
   ~stack() { delete[] dane; }
   void push(int v);
   int pop() { return dane[--top]; }
   void clear() { top=size=0; }
   bool empty() { return !top; }
  };

void stack::push(int v)
  {
   if(top>=size)
     {
      size=size?2*size:1;
      int *tmp=new int[size];
      memcpy(tmp,dane,top*sizeof(int));
      delete[] dane;
      dane=tmp;
     }
   dane[top++]=v;
  }

Pisane z palca, już trochę poprawiłem.

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