Wednesday, April 13, 2016

A details on stack using C language with Code



Question: What is stack?

Answer:
A stack is a list of element in which elements are added to the list in one end at the top of the stack.
That means it is an ordered group of homogeneous items of elements. Elements are added to and removed from the top of the stack (the most recently added items are at the top of the stack).
The last element to be added is the first to be removed and first element will be removed in last. That is why stack is called LIFO à Last in First Out

OR
Stack is a special type of data structure where elements are inserted from one end and elements are deleted from the same end. The position from where elements a are inserted and from where elements are deleted
is termed as top of the stack. Thus stack is a homogeneous collection of elements of any one type, arranged linearly with access at one end only.
Stack is also called the LIFO. That means Last On First Out. Which elements enter to the stack at last, will out first from the stack.

Stack has basically two functions.
1.       Push( )
2.       Pop( )


Stack push() method:
 

Stack push



Before push the value stack top was = 5 || After push 20 the stack, now top is 20

Stack pop() method:

Before pop the stack top was = 5 ; After pop of the stack the top will be popped and now top = 7


Stack pop



 For clearing the stack all code please follow the following instruction. Just stay with me only 20 minutes. I think stack's idea will be clear to you.


Stack full source code:

#include<stdio.h>
#include<stdlib.h>
#define MAX 100

        struct stack{

               int data[MAX];
               int top;

        };
        typedef struct stack Stack;
        Stack s;

//Functions
void push(int value);
int pop();
void display();
int getCount();

//main() method starts here
int main( ){
        int chose, pushValue;
        s.top = -1;
        printf("\n\t\t\tStack\t\t\t");
        printf("\n\t\tPress 1 for Push a value\n\t\tPress 2 for pop a value\n\t\tPress 3 for displaying total values\n\t\tPress 4 for total size\n\t\tPress 0 to exit");
        printf("\n-----------------------------------------------------\n");
        while(1){
               printf("Enter your option : ");
               scanf("%d", &chose);
               switch(chose){
                       case 1:

                               printf("Enter a value to push in  the stack : ");
                               scanf("%d", &pushValue);
                               push(pushValue);
                               break;
                       case 2:
                               pop();
                               break;
                       case 3:
                               display();
                               break;

                       case 4:
                               printf("The total element  in the stack is : ");
                               getCount();
                               break;
                       case 0:
                               printf("Program exited successfully\n\n");
                               return 0 ;
                               break;
                       default:
                               printf("Please chose a value from given option\n");

               }
        }
}
//Functions
void push(int value){

               if(s.top == (MAX -1)){
                       printf("Sorry stack is full\n");
               }else{
                       s.top++;
                       s.data[s.top] = value;
                       printf("\tValue inserted successfully\n");
               }


        }
        int pop(){
               int popValue, i;
               if(s.top == -1){
                       printf("Their is no value to pop\n");
               }else{
                       popValue = s.data[s.top];
                         printf("The element %d is popped from the stack successfully\n", popValue);
                         s.top--;
                         return popValue;
               }

        }
        void display(){
               int printvalue, i;
               if(s.top == -1){
                       printf("Sorry stack is empty\n");
               }else{
                       printf("The total stack is : ");
                       for(i = s.top; i >=0; i--){
                               printvalue = s.data[i];
                               printf("%d\t", printvalue);
                       }
               }

               printf("\n");
        }
        int getCount(){
               int count = 0, i;
               for(i = s.top; i >=0; i--){
                               s.data[i];
                               count++;
               }
               printf("%d\n", count);
               return count;
        }






Explanation:

Create a stack as a struct data type: we are not making it using array. We can also do that. But here we’ve just done it using struct. Just look it, I think I think, your stack idea will must be clear.

Step 1: Creating a struct named stack . We put them a top and a maximum data limit with array.


struct stack{

                 int data[MAX];
                 int top;

         };
         typedef struct stack Stack;
         Stack s;

Step 2: Declare the function list:


//Functions
void push(int value);
int pop();
void display();
int getCount();

Step 3: Use a while loop and a switch case statements to evaluate our all methods which we want.

//main() method starts here
int main( ){
         int chose, pushValue;
         s.top = -1;
         printf("\n\t\t\tStack\t\t\t");
         printf("\n\t\tPress 1 for Push a value\n\t\tPress 2 for pop a value\n\t\tPress 3 for displaying total values\n\t\tPress 4 for total size\n\t\tPress 0 to exit");
         printf("\n-----------------------------------------------------\n");
         while(1){
                 printf("Enter your option : ");
                 scanf("%d", &chose);
                 switch(chose){
                          case 1:

                                   printf("Enter a value to push in  the stack : ");
                                   scanf("%d", &pushValue);
                                   push(pushValue);
                                   break;
                          case 2:
                                   pop();
                                   break;
                          case 3:
                                   display();
                                   break;

                          case 4:
                                   printf("The total element  in the stack is : ");
                                   getCount();
                                   break;
                          case 0:
                                   printf("Program exited successfully\n\n");
                                   return 0 ;
                                   break;
                          default:
                                   printf("Please chose a value from given option\n");

                 }
         }
}

Step 4: Stack push method or insert data method:

We have to push a value in the stack. Look at the main file here we see that we first initialize the top = -1 ..By,
         s.top = -1;

We know that we have to insert value or keep value at the top of the end. So, our previous top = -1 . and now if we make top++ , that means top = top + 1; then top = -1 + 1 = 0 . So, now the position of  top of stack is : 0
And the value will be set as the top of the stack. And then keep the to the array data[] =  value or  s.data[s.top] = value; our parameter value which we passed from the main method. So, see the code:
void push(int value){

                 if(s.top == (MAX -1)){
                          printf("Sorry stack is full\n");
                 }else{
                          s.top++;         //make top++, previous was s.top = -1      
                          s.data[s.top] = value;
                          printf("\tValue inserted successfully\n");
                 }


         }

Step 5: stack pop method:

Now we want to remove the top value from the stack that is called pop of stack. So, now after pushing a value in the stack top index = 0 . Now we have to make the top = top – 1 ‘s index’s value. So just do the code:
int pop(){
                 int popValue, i;
                 if(s.top == -1){
                          printf("Their is no value to pop\n");
                 }else{
                          popValue = s.data[s.top];
                            printf("The element %d is popped from the stack successfully\n", popValue);
                            s.top--;
                            return popValue;
                 }

         }

Step 6: Display the stack

For displaying the stack. We need a loop must. Create a for loop as your choice. We need t start the loop top’s index’s value. Which is : s.top  And we’ll continue the loop until top’s index’s greater than or equal to 0 which is i >=0  And then make decrement operator i-- 
for(i = s.top; i >=0; i--){

void display(){
                 int printvalue, i;
                 if(s.top == -1){
                          printf("Sorry stack is empty\n");
                 }else{
                          printf("The total stack is : ");
                          for(i = s.top; i >=0; i--){
                                   printvalue = s.data[i];
                                   printf("%d\t", printvalue);
                          }
                 }


Step 7: Get count method:

It is the same as the display stack. Just create a new count variable. If an element found loop will execute of that exact time. Thus if 6 elements in the stack then loop will execute 6 times and we
Store that count making count = count + 1  || or count++
int getCount(){
                 int count = 0, i;
                 for(i = s.top; i >=0; i--){
                                   s.data[i];
                                   count++;
                 }
                 printf("%d\n", count);
                 return count;
         }








No comments:

Post a Comment