Hello,

how can i check that pushing characters into stack is successful (logically)?

Recommended Answers

All 8 Replies

By providing a member function for your stack abstraction class which returns 'isFull'.

Stacks normally have no upper bound to the amount they can store, so perhaps the first thing you get to notice is an exception.

What about if i check the number of characters for example

char *stk_layer[100];
int current // buttom of the stack
int depth; // the depth of the stack
if the depth of the stack < stk_layer[100]

> char *stk_layer[100];
But this isn't 100 characters

You could compare current with 100

But what is depth for?

> char *stk_layer[100];
But this isn't 100 characters

You could compare current with 100

But what is depth for?

haha trying to figure out
but i have a function related to depth
void set_depth(int size)
i think its the size of the stack

So if you want at most 100 chars, then it would be

char *stack = malloc ( max * sizeof *stack );
base = 0;
limit = max;

Adding stuff is stack[base++] = newValue; Checking is just if ( base == limit )

wel its working fine but the logic of the condition statement in both push and pop probably wrong, i'll appreciate any Help thx ! :)

#include<stdafx.h>
#include<iostream>  /* i'm using Iostream just to pause the program So ITS C*/
using namespace std;
class stack
{
private: 
int D_size; //point to the depth of the stack or the size
int but_stack;  //point to top of the stack and decrease to but of the stack
char *stacker[100]; 
public:
stack(){
but_stack=0;
}
void set_d_size(int size) //here to set the D_size of the stack
{
D_size=size;
}
int push(char *stack_strings) //push string into the stack 
{                                      //check if pushing not
                                       //successfull return value -1
if(but_stack>D_size )         //other return >0
{
return -1;
}

stacker[but_stack] = stack_strings;
but_stack++;
}
char *pop(void)  //pop characters from the stack
{                     //return NONE if pulling isnt successfull
                      //other return the string
if(but_stack==0) 
return "NONE";
but_stack--;
return stacker[but_stack];


}
};
int main()
{
int i=0;
stack stack1[3];
while (i<3)
{
stack1[i].set_d_size(100);
i++;
}
stack1[0].push("the light is on ");
stack1[1].push("but nobody");
stack1[2].push("Home = Hell");
for (int i=0; i<3; i++)
{
printf("Pop Stack %i:%s\n",i,stack1[i].pop());
}
system("pause");
return 0;
}

Looks like you have got your specification of stack wrong out there.
Please take a look HERE.

  • Also what exactly does D_size stand for ? You cant just change the size of an array once declared.
  • Also dont use "system("pause")" for pausing the program, its bad programmign practice. Use "cin.get()" instead.
  • Your push and pop operations dont take any parameters,so how would they know which stack to perform those operations on ?
  • Also write the helper functions like "is_empty( Stack a )" and "is_full( Stack b )" to perform easy runtime checking.

Incorporate those changes and then repost but do read the link i have posted.

Thanks for replying mate ! wel thats true ! i have to use some parameters but thats the problem specifications and i cant change them at all !
they gave us the function parameters for push and pop and we have to create wts inside the functions!
but i think the functions pop and push only for error checking like if push is not successfull !
so i'll use what u just told me ! "is_empty( Stack a )" and "is_full( Stack b )" "
thanks alote mate and i'll post the new code once i finish.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.