Friday 14 September 2012

Program that implement stack operations by using the pointers

Description:
In this program we have to implement the stack operation by using the pointers. Here they stack operation are  push and pop. Push operation is used to insert the elements into a stack and pop operation is used to remove the elements in to a stack.

Algorithm:
Step 1: Start
Step 2: Declare the structure for the stack pointers.
Step 3: Define the push function
Step 4: Define the pop function
Step 5: Define the display function
Step 6: Read the choice
Step 7: if choice = push
                           Create a cell for the TOP cell in the stack.
                           Place the date in the TOP cell
                           Place the TOP pointer to the new cell
Step 8: if choice=pop
                           Check if empty stack. If so, print stack is empty.
                           Otherwise, remove the TOP cell.
Step 9: if choice=display
                           Display all the elements in the Stack.
Step 10: Stop

Program:

#include<stdio.h>
#include<conio.h>

struct st_point
{
  int ele;
  struct st_point *l;
}

*t;
int i;

void push_ele(int j);
int pop_ele();
void display_ele();

void main()
{
  char choice,num1=0,num2=0;
  int i;
  while(1)
  {
    clrscr();
    printf("======================================");
    printf("\n\t\t MENU ");
    printf("\n======================================");
    printf("\n[1] Using Push Function");
    printf("\n[2] Using Pop Function");
    printf("\n[3] Elements present in Stack");
    printf("\n[4] Exit\n");
    printf("\n\tEnter your choice: ");
    fflush(stdin);
    scanf("%c",&choice);

    switch(choice-'0')
    {
      case 1:
      {
            printf("\n\tElement to be pushed:");
            scanf("%d",&num1);
            push_ele(num1);
            break;
      }

      case 2:
      {
            num2=pop_ele(1);
            printf("\n\tElement to be popped: %d\n\t",num2);
            getch();
            break;
      }

      case 3:
      {
            printf("\n\tElements present in the stack are:\n\t");
            display_ele();
            getch();
            break;
      }

      case 4:
            exit(1);
            break;

      default:
            printf("\nYour choice is invalid.\n");
            break;
    }
  }
}
/*Inserting the elements using push function*/
void push_ele(int j)
{
  struct st_point *m;
  m=(struct st_point*)malloc(sizeof(struct st_point));
  m->ele=j;
  m->l=t;
  t=m;
  return;
}

/*Removing the elements using pop function*/
int pop_ele()
{
  if(t==NULL)
  {
    printf("\n\STACK is Empty.");
    getch();
    exit(1);
  }
  else
  {
    int i=t->ele;
    t=t->l;
    return (i);
  }
return 0;
}

/*Displaying the elements */
void display_ele()
{
  struct st_point *pointer=NULL;
  pointer=t;
  while(pointer!=NULL)
  {
    printf("%d\t",pointer->ele);
    pointer=pointer->l;
  }
}


Output:

======================================
                 MENU
======================================
[1] Using Push Function
[2] Using Pop Function
[3] Elements present in Stack
[4] Exit

        Enter your choice: 1

        Element to be pushed:23

======================================
                 MENU
======================================
[1] Using Push Function
[2] Using Pop Function
[3] Elements present in Stack
[4] Exit

        Enter your choice: 3

        Elements present in the stack are:
        23
======================================
                 MENU
======================================
[1] Using Push Function
[2] Using Pop Function
[3] Elements present in Stack
[4] Exit

        Enter your choice: 2

        Element to be popped: 23
======================================
                 MENU
======================================
[1] Using Push Function
[2] Using Pop Function
[3] Elements present in Stack
[4] Exit

        Enter your choice: 4
             Exit the program



VIVA QUESATIONS:
1) Define Stack ?
Ans: A stack is a linear data structure in which a data item is inserted and deleted at one end
2) Define data structure ?
Ans: A data structure is a collection of organized data that are related to each other

3)  What are the various operation performed on the stack ?
Ans: push(), pop()





3 comments:

  1. stack operations are used for storing elements in a stack. The input method for stack is last in first out. Thank you for sharing this program.
    regards:
    srinath reddy.
    admin of Programming Tutorials

    ReplyDelete

  2. Very informative article.Thank you author for posting this kind of article .

    http://www.wikitechy.com/view-article/write-a-program-in-cpp-to-implement-stack-operations-using-an-array-with-example


    Both are really good,
    Cheers,
    Venkat

    ReplyDelete