facebook like button

16 July, 2011

Concept of Stack in C

      The stack is another type of data structure. This is analogous to a stack of pan cakes or sacs. One more thing I want to make clear about stack concept in C is that the stack is not a physical variable or object which you can point out in the program and say that this is my stack variable. I am emphasizing this because when I was learning for a whole month I wanted to see stack physically saying that yes this variable is stack while stack is not like that. This is an abstraction.
      Let me explain this in a simple way. You people may have seen stack ( a special vertically arranged pile) of sacs or cement bags. What those people do to make that stack is to put a bag initially and after that they go on putting one over the others to make a vertical pile ( the stack). Now when they want to remove the bag, they first remove the top most bag and proceed downward by removing bag by bag. So you can observe that the bag which was put last will be removed first and the bag which was put first will be removed last. This was the real life example of stack. In technical area also concept is exact same. We call this Last In First Out (LIFO) the stack concept. I have already told you people that stack is an abstraction. Its just the implementation of the real world stack concept onto our programs. In programming wherever we treat a bulk of elements in the same manner as cement bags we say that this is implementation of stack. We can implement stack in two ways:
1. Stack implementation in arrays.
2. Stack implementation in linked lists.


     To understand the stack concept in programming perspective you people need to know 4 terms: 
1. element : is a member of stack may be of a derived or built in datatype.
2. top: is the the variable which holds the index(in case of array) or address (in case of linked list) of top element.
3. push() : is the function to user defined push or put an element on stack.
4. pop() : is the user defined function to pop or remove an element from stack.


      Because the concept is same so 2 user defined functions push() and pop() are used in both the implementations. The definitions of push() and pop() are different for both implementations but the working is same as described above. Those definitions will be given in the upcoming programs.
To elaborate these implementations more programs will be put in next posts.


Remarks:
1. While programming you should not worry about the whole stack like where it resides, how to keep track of all elements etc. The stack is abstract thing. only thing you need to concentrate on and to keep track is the top.
2. Both the operations push() or pop() are concerned with top. The element put by push() on the stack always becomes the top and the pop() function also removes only the top element.
3. When pop removes the top element, the top variable points to next element which is now on the top.This is also true in real life examples of cement bags. when the topmost bag is removed we people see the bag just below the previous one is now at top.

No comments:

Post a Comment

feel free to ask your doubts... if any
you can post your doubts on
www.facebook.com/programsimply