c++ - How to remove base element from the stack and return it in the same order without using push, pop, any method -


i programming method called popbuttom () in c ++ using stacks.

the method must following: ¨eliminate element of base , leave stack in same order without element elminado¨, can not use pop or push.

for example:

ini stack:

a
b
c
d

end stack:

a
b
c

i have programmed following, not know can have bad:

void popfull() {     struct node *a, *b;      top1 = top;      while (top1 != null)     {         b = top1->ptr;                 = top1;                               b->ptr = a;          top1 = b;     } } 

regards mariam

so, i'll see can answer this, though helpful if include more complete version of code, because i'm not entirely sure type of data structure of variables because there no declarations included. well, clarify mean "but not know can have bad:"? think these changes make question easier answer.

in case, i'll try answer question interpreting "how eliminate element @ base , leave stack in same order, not using pop or push." (i assume sort of assignment?)

to end i'll propose several options. c++11 has function isn't push() or pop() can use doing stack.emplace() adds item top of stack. functionally same stack.push might nice hack. it's bit of technicality, , there difference (it's nuanced though, here's link if you're interested: c++: stack's push() vs emplace()) might able away it.

next, i'll if cannot use stack.pop() or stack.push() next option possibility, if initialize stack container class of vector, because otherwise items not contiguous in memory , there no guarantee work. i'm referring to, of course, pointer arithmetic. here: copy std::stack std::vector answer deals this, i'll give brief overview of did. if initialize stack using std::vector in this example in documention, can copy stack vector, , operate freely on vector, copy stack.

here's mean (keep in mind works if container class vector because seem you're designing function take in argument , not initialize own).

//this how have have been initailized //for guarenteed work     std::stack<int, std::vector<int>> mystack;   int* begin = &stack.top()+1; int* end = being+stack.size(); std::vector stackcontents(begin,end); 

and hurray, smooth sailing here, can remove item freely using method of choice on vector. then, when you've modified vector, can create stack return doing opposite:

std::stack<int, std::vector<int>> newstack (stackcontents); return newstack; 

obviously major workaround, , in real world pop() , push() useful functions , included reason. might time touch on idea stacks designed accessed either end. that's why it's been categorized last in first out, because idea of order matters , trying circumvent order means stack wasn't proper data structure use in first place. either way, that's 2 cents, , hope helps.


Comments

Popular posts from this blog

sql server - Cannot query correctly (MSSQL - PHP - JSON) -

php - trouble displaying mysqli database results in correct order -

C++ Linked List -