Talk:Stack (abstract data type)

message: (to Martin) Hi, I see you merged stack pointer into Stack. Are you sure it's not an important enough idea to merit a separate article? There are a number of pages that link to stack pointer (and I was about to add a few more :-). Noel 20:04, 18 Oct 2003 (UTC)
replies: text: Prove me wrong and write a long and well-written article on a stack pointer if you like.. but the version I merged didn't seem worth a seperate article. Martin 22:29, 20 Oct 2003 (UTC)
thread_title:  Merge 

message: I made an image for the german pendant to a stack (de:Kellerspeicher), perhaps you want to include it here. Is there a possibility to reuse the one from there or does it need a second upload? TRauMa, 2003-11-26
: 
I made my own stack image because I have a hard time seeing the one that is currently in the article (the contrast of the different shades of blue makes it hard for me to see the edges). The different layers are slightly transparent to make it more apparent that there are different layers. Is this acceptable? Aranel ("Sarah") 17:27, 25 Oct 2004 (UTC)
thread_title:  Image 

message: #include <stdio.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>
#define MAX 50
char stack[MAX] ;
char infix[MAX] ;
char postfix[MAX] ;
char eval[MAX] ;
char *s, *t ; /*pointers to input and output strings*/
int top; /*Stack top*/
/*Function Prototypes*/
void Initialize (void);
void SetExpression (char *);
char Pop (void );
void Push (char);
int priority (char);
void Convert (void);
int Evaluate(void);
void main( )
{
int m;
clrscr( ) ;
Initialize ( ) ;
printf ( "\nEnter an infix expression: " ) ;
gets ( infix ) ;
SetExpression (infix) ;
Convert( ) ;
printf ( "\nThe Postfix expression is: " ) ;
puts(postfix);
strcpy(eval,postfix);
m=Evaluate( );
printf("answer: %d", m );
getch( ) ;
}
void Initialize (void)
{
top = -1 ;/*Make stack empty*/
}
void SetExpression ( char *str )
{
s = str ;
t = postfix;
}
/* adds operator to the stack */
void Push ( char c )
{
if ( top == MAX - 1 )
printf ( "\nStack is full.\n" ) ;
else
{
top++ ;
stack[top] = c ;
}
}
/* pops an operator from the stack */
char Pop ( void )
{
if ( top == -1 ) /* Stack is empty*/
return -1 ;
else
{
char item = stack[top] ;
top-- ;
return item ;
}
}
int priority(char c)
{
if ( c == '*' || c == '/' || c == '%' ) 
return 2;
else if ( c == '+' || c == '-' )
return 1;
else
return 0;
}
/* converts the infix expr. to postfix form */
void Convert (void)
{
char x ;
while ( *( s ) )
{ /*Skip white spaces, if any*/
if ( *( s ) == ' ' || *( s ) == '\t' )
{
s++ ;
continue ;
}
if ( isdigit ( *( s ) ) )/*Operands*/
{
while ( isdigit ( *( s ) ) )
{
*( t ) = *( s ) ;
s++ ;
t++ ;
}
}
if ( *( s ) == '(' )/*Opening Parenthesis*/
{
Push ( *( s ) ) ;
s++ ;
}
if ( *( s ) == '*' || *( s ) == '+' || *( s ) == '/' || *( s ) == '%' || *( s ) == '-' ) /*operators*/
{
if ( top != -1 )
{
x = Pop ( ) ;
while ( priority ( x ) >= priority ( *( s ) ) )
{
*( t ) = x ;
t++ ;
x = Pop ( ) ;
}
Push( x ) ;
Push ( *( s ) ) ;
}
else Push( *( s ) ) ;
s++ ;
}
if ( *( s ) == ')' )/*Closing Parenthesis*/
{
x = Pop ( ) ;
while ( x != '(' )
{
*( t ) = x ;
t++ ;
x = Pop ( ) ;
}
s++ ;
}
}
while ( top != -1 )/*While stack is not empty*/
{
x = Pop ( ) ;
*( t ) = x ;
t++ ;
}
t++ ;
}
int Evaluate(void)
{
int i,l,a,b,q,z;
l=strlen(eval);
for(i=0;i<l;i++)
{
if ( isdigit ( eval[i] ) )
{
Push(eval[i]);
}
else if ( eval[i] == '*' || eval[i] == '+' || eval[i] == '/' || eval[i] == '%' || eval[i] == '-' )
{
a = Pop ( );
b = Pop ( );
switch( eval[i] )
{
case '+' : q=b+a; break;
case '-' : q=b-a; break;
case '*' : q=b*a; break;
case '/' : q=b/a; break;
}
Push ( q );
}
}
z = Pop ( );
return z;
}
— Preceding unsigned comment added by 121.97.230.167 (talk • contribs) 03:57, 24 January 2009 (UTC)
thread_title:  Postfix 

message: Any particular reason for using overly complicated code to illustrate what is really a very simple idea? There are plenty of examples on the web that illustrate a stack implementation in simpler terms.  How about using an array based example that just stacks doubles (I'm avoiding the use of ++ operators because not everyone is familiar with the notation) ? 
#define MAXSTACK 10
#define EMPTYSTACK -1
int top = EMPTYSTACK;
double items[MAXSTACK];
void push(double value) {
   top = top + 1;
   items[top] = value;
}
double pop() {
   int value = items[top];
   top = top - 1;
   return value;
}
double peek() {
  return items[top];
}
int full()  {
   return top == MAXSTACK - 1;
}
— Preceding unsigned comment added by Rhodydog (talk • contribs) 18:30, 19 April 2009 (UTC)
thread_title:  Convoluted 

message: Considering a stack is a fundamental computer science data structure there seems to be a great lack of abstraction in this article. Shouldn't we define what a stack is before we start plunging into implementations?
/Definition/
Function signatures: 
   init: -> Stack 
   push: N x Stack -> Stack 
   top: Stack -> (N U ERROR) 
   remove: Stack -> Stack 
   isempty: Stack -> B 
(where N indicates a element (natural numbers in this sace), B 
indicates a boolean and U indicates set union) 
Semantics 
   top(init()) = ERROR 
   top(push(i,s)) = i 
   remove(init()) = init() 
   remove(push(i, s)) = s 
   isempty(init()) = true 
   isempty(push(i, s)) = false 
Jones "Systematic Software Development Using VDM"
ps. is there a bug with the wiki editor- none of the buttons work...
Salocin-yel (talk) 08:18, 7 June 2010 (UTC)
thread_title:  Lack of Abstraction 

message: An image used in this article, , has been nominated for deletion at Wikimedia Commons in the following category: Media without a source as of 17 September 2011 
;What should I do?
Don't panic; a discussion will now take place over on Commons about whether to remove the file. This gives you an opportunity to contest the deletion, although please review Commons guidelines before doing so.
* If the image is non-free then you may need to upload it to Wikipedia (Commons does not allow fair use)
* If the image isn't freely licensed and there is no fair use rationale then it cannot be uploaded or used.
This notification is provided by a Bot --CommonsNotificationBot (talk) 15:46, 17 September 2011 (UTC)
thread_title: File:Towers of Hanoi.jpeg Nominated for Deletion

message: It's far too long and it looks like splitting it up could be done at the same time as general cleanup. I'll go through each sub-section...
;6.1 Converting a decimal number into a binary number
:This could probably stay, it's fairly short.
;6.2 Towers of Hanoi
:The bulk of this should be moved into Towers of Hanoi and/or merged with the algorithms described there. A few paragraphs describing the problem and why stacks are applicable to it should be left on this page, with a "main article" template link.
;6.3 Expression evaluation and syntax parsing
:I haven't looked around for a page on this subject, but there probably is one; if not, there deserves to be one! It can then be summarized again in a few paragraphs with a "main article" template link to the existing/new page.
;6.4 Conversion of an Infix expression that is fully parenthesized into a Postfix expression
:This sub-section could be merged with the existing/new page that the content under 6.3 would be moved to.
;6.5 Rearranging railroad cars
:This seems short enough to keep on this page.
;6.6 Quicksort
:This should be moved/merged with the Quicksort page, again with a summary and main article link.
;6.7 The Stock Span Problem
:I'm not well versed in this subject, but it seems like another thing that could be moved to either a new page or an existing one, again with a summary and main article link.
;6.8 Runtime memory management
:This is already summarized and main article linked, so it can be left as is. This is how most of the other sections should ideally be.
I'm putting this here rather than actually doing it because it's a huge amount of work and should probably be discussed further before anything is done, to avoid doing anything that would turn out to be unnecessary in the long run, and also because of course there are some sections I've suggested to be split for which target pages need to be identified if possible. ~ Keiji (iNVERTED) (Talk) 21:00, 18 November 2011 (UTC)
replies: text: By the way, feel free to reply in-line with my comments on each section above, but please copypaste my signature there if you do to avoid it becoming a mess. ~ Keiji (iNVERTED) (Talk) 21:02, 18 November 2011 (UTC)

text: Hi Nipun. I think the application section is—at least in its current form—is not suited for an encyclopedic article on stacks. It would be more appropriate for a textbook, such as the WikiBook on data structures. One or two of the applications could remain (parsing expression is a classical example of the use of stacks, so that would be a good choice), be would need to be made more concise and probably use pseudocode. —Ruud 18:06, 21 August 2012 (UTC)
thread_title:  The "Applications" section should be split. 

message: I'd prefer if somebody more knowledgeable about this topic fixed this, but I think there may be a mistake in this section: Stack_(abstract_data_type)#Evaluation_of_an_infix_expression_that_is_fully_parenthesized. Under the "closing brackets" part, the step "Go to step (2.4)" seems wrong. It should be "Go to step (1)". You can't pop from the character stack unless you're ready to evaluate thanks to a closing parenthesis, right? Proxyma (talk) 04:04, 24 November 2013 (UTC)
thread_title:  incorrect step in expression-evaluation section? 

message: Apropos Turing and the sentence I had removed – like I wrote – subroutines were already implemented practically in the Z4 – which does not mean, that Zuse characterized the stack data structure – in the same way one has to ask – when Turing speaks about subroutines using the words bury /unbury – does he describe the stack data structure? If so, could it be cited? Could it be clarified? The way it’s in the article now makes no sense. 87.159.108.183 (talk) 10:46, 26 October 2014 (UTC)
thread_title:  @David Eppstein, cf. Turing 

message: 213.149.62.35 (talk) 01:16, 28 March 2017 (UTC)
thread_title:  The "Hardware stacks" section should be merged into [[Call stack]] 

message: From the article:
"Basic architecture of a stack
A typical stack is an area of computer memory with a fixed origin and a variable size. Initially the size of the stack is zero. A stack pointer, usually in the form of a hardware register, points to the most recently referenced location on the stack; when the stack has a size of zero, the stack pointer points to the origin of the stack.
The two operations applicable to all stacks are:
a push operation, in which a data item is placed at the location pointed to by the stack pointer, and the address in the stack pointer is adjusted by the size of the data item;
a pop or pull operation: a data item at the current location pointed to by the stack pointer is removed, and the stack pointer is adjusted by the size of the data item.
There are many variations on the basic principle of stack operations. Every stack has a fixed location, in memory, at which it begins. As data items are added to the stack, the stack pointer is displaced to indicate the current extent of the stack, which expands away from the origin.
Stack pointers may point to the origin of a stack or to a limited range of addresses either above or below the origin (depending on the direction in which the stack grows); however, the stack pointer cannot cross the origin of the stack."
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
May I presume that when it says Hardware Section we are talking assembler code or machine code ???
My experience is asm on the Z80. There are a few contradictions here pertaining to this particular domain.
First: points to the most recently referenced location on the stack; this is correct, though we call it the "top of the stack".
Then: a push operation, in which a data item is placed at the location pointed to by the stack pointer, this is incorrect. I hope it is obvious that the SP must first be decremented to point at the next available address below the top-of-stack. As written, this operation will overwrite the word topping the stack.
Now we come to: Every stack has a fixed location,. On the Z80 the range of addresses occupied by the stack is fixed, provided that the stack area is not changed to a new location with the LD SP,nn, LD SP,HL or LD SP,(nn) instructions. It is common practice on the Z80 to switch stack areas for various reasons. In fact, sometimes it is imperative.
Now we come to: the stack pointer cannot cross the origin of the stack. On the Z80 the origin would be the initial value loaded into the SP register. And yes, on the Z80 it is indeed possible to POP words off the stack and thereby increment SP past the "origin". This is an underflow condition. Speaking of which, there more common occurrence is PUSHing too many words, causing an overflow. Yet this eventuality is not described in the article.
The Z80 can directly manipulate the SP using the INC SP and DEC SP instructions. HL can swap with (SP) using EX (SP),HL. The ADD HL,SP and LD SP,HL instructions are often used to perform arithmetic on the SP; we are not limited to immediate loads with absolute values. This is how the stack is modified in relative terms on the Z80, since it lacks a formal relative addressing mode for data loads. The article should address arithmetic done on the SP.
I feel the main problem is that too many editors have worked over this article, each attempting to describe in too general a way the various idiosyncracies of different processors and languages. It's a real mess! Each particular situation should be separated and sorted out severally. These examples I have cited are just the tip of this article's iceberg...this is just too confusing for somebody relatively new who doesn't know how to tease out the various cross-purposes of each section. IMO.Wikkileaker (talk) 21:37, 14 July 2020 (UTC)
thread_title:  We got some problems with Hardware Section 

message: There is a move discussion in progress on Talk:Associative array which affects this page. Please participate on that page and not in this talk page section. Thank you.  —RMCD bot 00:32, 27 January 2022 (UTC)
thread_title:  Move discussion in progress 

message: This article dives into the details of what a stack is, but doesn't clearly explain why a stack might be preferable to other data structures such as a queue. I am not a computer scientist so don't know the correct way of stating it, but it's something like "stacks are useful when you have some result A that depends on result B, which in turn depends on C. You need some way of remembering where you were in the calculation of A, while you go off and calculate B, and then C, before coming back to B, and then A."
You can't do that with a queue, because when you've finished C you'll come straight back to A before you have done B.  — Preceding unsigned comment added by 80.47.212.205 (talk) 21:28, 7 November 2023 (UTC)
thread_title:  Too much "what", not enough "why" 

message: The redirect Stack (computer science) has been listed at redirects for discussion to determine whether its use and function meets the redirect guidelines. Readers of this page are welcome to comment on this redirect at  until a consensus is reached.  —Panamitsu (talk) 01:09, 15 October 2023 (UTC)
thread_title:  "[[:Stack (computer science)]]" listed at [[Wikipedia:Redirects for discussion|Redirects for discussion]]