Thursday, 10 August 2017

DAA PRACTICAL MANUAL: PRACTICAL 4

                                                                                           Date:10/08/2017
Experiment No: 04

1. Experiment vision: Write a program to study and implement Stack.
2. OBJECTIVE: Program to implement an object in C in a specific way to add the element and   
                              remove only top element.  
3. THEORY: Stack is a specialized data storage structure (Abstract data type). Unlike, arrays access of elements in a stack is restricted. It has two main functions push and pop. Insertion in a stack is done using push function and removal from a stack is done using pop function. Stack allows access to only the last element inserted hence, an item can be inserted or removed from the stack from one end called the top of the stack. It is therefore, also called Last-In-First-Out (LIFO) list. Stack has three properties: capacity stands for the maximum number of elements stack can hold, size stands for the current size of the stack and elements is the array of elements.
Functions:
1. createStack function– This function takes the maximum number of elements (maxElements) the stack can hold as an argument, creates a stack according to it and returns a pointer to the stack. It initializes Stack S using malloc function and its properties.
2. push function - This function takes the pointer to the top of the stack S and the item (element) to be inserted as arguments. Check for the emptiness of stack
3. pop function - This function takes the pointer to the top of the stack S as an argument.
4. top function – This function takes the pointer to the top of the stack S as an argument and returns the topmost element of the stack S.

4. Algorithm:

   1. Create a stack
   2. Enter a decimal number, which has to be converted into its equivalent binary form.
   3. iteration 1 (while number > 0)
         3.1 Push digit into the stack
         3.2 If the stack is full
              3.2.1 Print an error
              3.2.2 Stop the algorithm
         3.3 End the if condition
         3.4 Divide the number by 2
   4. End iteration1
  
   5. iteration 2 (while stack is not empty)
         5.1 Pop digit from the stack
         5.2 Print the digit
   6. End iteration2
   7. STOP




5.  PROGRAM:
#include<stdio.h>
int stack[100],choice,n,top,x,i;
void push();
void pop();
void display();
void main()
{
    //clrscr();
    top=-1;
    printf("\n enter the size of stack[max=100]:");
    scanf("%d",&n);
    printf("\n\t stack operations using array");
    printf("\n\t--------------------------------");
    printf("\n\t 1.push\n\t 2.pop\n\t 3.display\n\t 4.exit");
    do
    {
        printf("\n enter the choice:");
        scanf("%d",&choice);
        switch(choice)
        {
        case 1:
        {
            push();
            break;
        }
        case 2:
        {
            pop();
            break;
        }
        case 3:
        {
            display();
            break;
        }
        case 4:
        {
            printf("\n\t exit point ");
            break;
        }
        default:
        {
            printf ("\n\t please enter a valid choice(1/2/3/4)");
        }
      }
    }
    while(choice!=4);
}

void push()
{
    if(top>=n-1)
    {
        printf("\n\tstack is over flow");
       
    }
    else
    {
        printf(" enter a value to be pushed:");
        scanf("%d",&x);
        top++;
        stack[top]=x;
    }
}
void pop()
{
    if(top<=-1)
    {
        printf("\n\t stack is under flow");
    }
    else
    {
        printf("\n\t the popped elements is %d",stack[top]);
        top--;
    }
}
void display()
{
    if(top>=0)
    {
        printf("\n the elements in stack \n");
        for(i=top; i>=0; i--)
            printf("\n%d",stack[i]);
        printf("\n press next choice");
    }
    else
    {
        printf("\n the stack is empty");
    }
}
6. OUTPUT:
------------------------------------------
------------------------------------------

7. conclusion:
Thus a C Programs by using method of stack has been executed successfully.
8.  REMARK:
Program to implement in C in a specific way to add the element and remove only top element just like a stack procedure of LIFO.


  9. Discussion Questions:
1) what is stack?
_________________________________________________________________________________________________________________________________________________________
2) How is stack working, explain with example? (diagrammatically)
________________________________________________________________________________________________________________________________________________________
3) explain example of tower of Hanoi using stack?
____________________________________________________________________________________________________________________________________________________________

                                    

No comments:

Post a Comment

About Me

Hi, I am Prof. Amol Zade, working in the field of teaching and research from last more than 10 years. My area of interests are Artificial Intelligence, Wireless Networking, Algorithms. I have 16 International paper publications along with 4 International Conference; also 3 Books published. published on Object Oriented Programming with open source approach.