NO

Author Topic: ABOUT STACKS  (Read 2792 times)

digrev

  • Guest
ABOUT STACKS
« on: July 31, 2012, 08:54:19 PM »
Code: [Select]


#include<stdio.h>
#include<stdlib.h>
#define SIZE 10
 
void push(int i);
int pop(void);
int *tos,*p1,stack[SIZE];
int main(void){
    int value;
tos=stack;
p1=stack;
do {
    printf("ENTER VALUE");
    scanf("%d",&value);
    if(value!=0) push(value);
    else printf("value on top %d",pop());
   
   
    }while(value!=-1);
 
    }
    void push(int i){
         
         p1++;
         if(p1==(tos+SIZE)){
         printf("stack overflow.\n");
         exit(1);
         }
         *p1=i;
         }
int pop(void){
   
    if(p1==tos){
               
                printf("stack underflow");
                exit(1);
                }
                p1--;
                return *(p1+1);
    }




Cevapla   Alıntı Yaparak Yanıtla          .
 
--------------------------------------------------------------------------------





hi everybody i am trying to understand  about stacks There are a few things I do not understand here
tos=stack;
p1=stack;

 stack  points to more than one address at the same time i  couldnt understand which one  is for what ..secondly
do {
 printf("ENTER VALUE");
 scanf("%d",&value);
 if(value!=0) push(value);
 else printf("value on top %d",pop());
 }while(value!=-1);
}

these values (0 and -1 )are indexses?? for example 0  means stack is empty -1 is full? or somethink like that

CommonTater

  • Guest
Re: ABOUT STACKS
« Reply #1 on: July 31, 2012, 11:39:05 PM »
tos=stack;
p1=stack;
stack  points to more than one address at the same time i  couldnt understand which one  is for what ..


Assignments run right to left... more than one variable points to stack.  They are initializing the stack array to an empty state. 
 
Quote
secondly
do {
 printf("ENTER VALUE");
 scanf("%d",&value);
 if(value!=0) push(value);
 else printf("value on top %d",pop());
 }while(value!=-1);
}

these values (0 and -1 )are indexses?? for example 0  means stack is empty -1 is full? or somethink like that

Nope... value comes from the keyboard via scanf() ... In the source editor, put your text cursor on the colourized keyword scanf() and press F1 on your keyboard... you can read all about what it does from the help file (This works with any keyword or library function, btw.)
 
The main reason you're finding it so hard to follow is that it's poorly formatted...
Code: [Select]

 do
   {
       printf("ENTER VALUE");
      scanf("%d",&value);

      if (value != 0)
        push(value);
      else
        printf("value on top %d",pop());
    }
 while(value != -1);
See it now?
 
This is why source code is set up as it is... it's so you can follow the activity step by step. Jamming it all up into a couple of lines makes it's function much less clear. 
 
Code setup ... commenting the non-obvious parts, even indenting with vertical and horizontal whitespaces make it much easier to follow.  This is a good habit to get in right from the start.  Ask yourself: "If I have to come back to this in a year, will I still be able to follow it?"
 
So ... reformat your code, all nice and tidy... then follow what's going on step by step... if you have any further questions, post them up and we'll see what we can do...
« Last Edit: July 31, 2012, 11:43:52 PM by CommonTater »

digrev

  • Guest
Re: ABOUT STACKS
« Reply #2 on: August 01, 2012, 04:47:05 PM »
hi friend and thanks for  detailed answer really appriciate