Thread: What is a Stack, Heap and Queue ?

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    29

    What is a Stack, Heap and Queue ?

    Hi !!
    I have read some books to know exactly what is a stack a heap and a queue. But I couldnt find much information. What really is a stack or a heap ? Many books just mention that during program execution the variables are "pushed on to" a stack ( or was that a heap or a queue ??).Another confusion is FIFO. To illstrate the FIFO concept the "plates" example was given. But if something is accesed on a First In First Out basis, what happens to the other variables that are below this variable. Does this mean that in a FIFO sequence only the variable that is First In gets out first and the remaining variables just remain there? .
    I tried to search for information but could not find much. I would be extremely thankful if someone could help me with these.

  2. #2
    Registered User dharh's Avatar
    Join Date
    Jan 2002
    Posts
    51
    Ok, first a heap. A heap is a region of free memory that your program can use with C's dynamic memory allocation functions. Such as malloc() and free(). This is memory NOT yet in use by any program, including windows, but set aside for use for the program in question that created the heap.

    A queue is best represented with FIFO. First in First out. Best way to think of it as an array with two variables that will represent the position you are at. Basicaly one variable remembers how many you've put in and the other variable remembers how many you've taken out. Like this:

    Code:
    #define MAX 100
    
    int queue[MAX];
    int storePosition = 0;
    int retrievePosition = 0;
    
    void store(int num) {
      if(storePosition >= MAX) {
        printf("Full.\n");
      }
      else {
        queue[storePosition] = num;
        storePosition++;
      }
    }
    
    int retrieve(void) {
      int temp;
    
      if(retrievePosition >= storePosition) {
        printf("Empty.\n");
        temp = 0;
      }
      else {
        temp = queue[retrievePosition];
        retrievePosition++;
      }
      return temp;
    }
    A stack on the other hand is best represented as FILO. First in Last Out. Typicaly a stack is thought to have two functions that will push() data into the stack and pop() data out of the stack. In this case only one variable will be needed to keep track of the position your in. Essentialy you put two, it incrememts position by two. When you retrieve one, it decrememts position by one.

    Code:
    #define MAX 100
    
    int stack[MAX];
    int topPosition;
    
    void push(int num) {
      if(topPosition >= MAX) {
        printf("Full.\n");
      }
      else {
        stack[topPosition] = num;
        topPosition++;
      }
    }
    
    int pop(void) {
      int temp;
    
      topPosition--;
      if(topPosition < 0) {
        printf("Empty.\n");
        temp = 0;
      }
      else {
        temp = stack[topPosition];
      }
      return temp;
    }
    Hope this helps.
    Last edited by dharh; 03-17-2002 at 03:47 AM.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    29

    Could you suggest any good book/site for this ?

    Hi !!

    Thanks a lot for the answer. But could you tell me a good book or a site whre i could learn moer about stacks heap and queue. I was told that to be a good C/C++ programmer it is necessary to have a good understanding of these. Thanks again for the answer.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Algorithms in C by Sedgewick. It's hard to understand at first, but if you read it a few times the logic begins to sink in.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stack and heap objects
    By John_L in forum C++ Programming
    Replies: 4
    Last Post: 03-18-2008, 10:20 AM
  2. Simple Queue Question
    By RobJ in forum C++ Programming
    Replies: 5
    Last Post: 05-27-2007, 09:28 AM
  3. Heap management
    By Frost Drake in forum C Programming
    Replies: 12
    Last Post: 10-15-2006, 10:06 PM
  4. a priority queue quesiton..
    By NightWalker in forum C Programming
    Replies: 2
    Last Post: 10-07-2003, 09:50 AM
  5. Anyone Know HEAP ?
    By Kam in forum C Programming
    Replies: 5
    Last Post: 09-14-2002, 07:47 AM