Q:

Write a C Program to implement Stack Operations Using Pointer

0

Write a C Program to implement Stack Operations Using Pointer. Here’s simple Program to implement Stack Operations Using Pointer in C Programming Language.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

What are Pointers?


A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location. Like any variable or constant, you must declare a pointer before using it to store any variable address.

 
 

The general form of a pointer variable declaration is −

  • type *var-name;

Here, type is the pointer’s base type; it must be a valid C data type and var-name is the name of the pointer variable.

The asterisk * used to declare a pointer is the same asterisk used for multiplication. However, in this statement the asterisk is being used to designate a variable as a pointer.

 

The unary or monadic operator & gives the “address of a variable’”.

The indirection or dereference operator * gives the “contents of an object pointed to by a pointer”.


Below is the source code for C Program to implement Stack Operations Using Pointer which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :

/*  C Program to implement Stack Operations Using Pointer  */

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

#define MAX 50
int size;

// Defining the stack structure
struct stack {
   int arr[MAX];
   int top;
};

// Initializing the stack(i.e., top=-1)
void init_stk(struct stack *st) {
   st->top = -1;
}

// Entering the elements into stack
void push(struct stack *st, int num) {
   if (st->top == size - 1) {
      printf("\nStack overflow(i.e., stack full).");
      return;
   }
   st->top++;
   st->arr[st->top] = num;
}

//Deleting an element from the stack.
int pop(struct stack *st) {
   int num;
   if (st->top == -1) {
      printf("\nStack underflow(i.e., stack empty).");
      return NULL;
   }
   num = st->arr[st->top];
   st->top--;
   return num;
}

void display(struct stack *st) {
   int i;
   for (i = st->top; i >= 0; i--)
      printf("\n%d", st->arr[i]);
}

int main()
{
   int element, opt, val;
   struct stack ptr;
   init_stk(&ptr);
   printf("Enter Stack Size :: ");
   scanf("%d", &size);
   while (1) {
      printf("\n\tSTACK PRIMITIVE OPERATIONS.........\n");
      printf("\n1.PUSH");
      printf("\n2.POP");
      printf("\n3.DISPLAY");
      printf("\n4.QUIT");
      printf("\n");
      printf("\nEnter your option :: ");
      scanf("%d", &opt);
      switch (opt)
      {
      case 1:
         printf("\nEnter the element into stack:");
         scanf("%d", &val);
         push(&ptr, val);
         break;

      case 2:
         element = pop(&ptr);
         printf("\nThe element popped from stack is : %d", element);
         break;

      case 3:
         printf("\nThe current stack elements are:");
         display(&ptr);
         break;

      case 4:
         exit(0);

      default:
         printf("\nEnter correct option!Try again.");

      }
   }
   return (0);
}

Output  : :


/*  C Program to implement Stack Operations Using Pointer  */

Enter Stack Size :: 6

        STACK PRIMITIVE OPERATIONS.........

1.PUSH
2.POP
3.DISPLAY
4.QUIT

Enter your option :: 1

Enter the element into stack:1

        STACK PRIMITIVE OPERATIONS.........

1.PUSH
2.POP
3.DISPLAY
4.QUIT

Enter your option :: 1

Enter the element into stack:2

        STACK PRIMITIVE OPERATIONS.........

1.PUSH
2.POP
3.DISPLAY
4.QUIT

Enter your option :: 1

Enter the element into stack:3

        STACK PRIMITIVE OPERATIONS.........

1.PUSH
2.POP
3.DISPLAY
4.QUIT

Enter your option :: 1

Enter the element into stack:4

        STACK PRIMITIVE OPERATIONS.........

1.PUSH
2.POP
3.DISPLAY
4.QUIT

Enter your option :: 1

Enter the element into stack:5

        STACK PRIMITIVE OPERATIONS.........

1.PUSH
2.POP
3.DISPLAY
4.QUIT

Enter your option :: 1

Enter the element into stack:6

        STACK PRIMITIVE OPERATIONS.........

1.PUSH
2.POP
3.DISPLAY
4.QUIT

Enter your option :: 1

Enter the element into stack:7

Stack overflow(i.e., stack full).
        STACK PRIMITIVE OPERATIONS.........

1.PUSH
2.POP
3.DISPLAY
4.QUIT

Enter your option :: 3

The current stack elements are:
6
5
4
3
2
1
        STACK PRIMITIVE OPERATIONS.........

1.PUSH
2.POP
3.DISPLAY
4.QUIT

Enter your option :: 2

The element popped from stack is : 6
        STACK PRIMITIVE OPERATIONS.........

1.PUSH
2.POP
3.DISPLAY
4.QUIT

Enter your option :: 2

The element popped from stack is : 5
        STACK PRIMITIVE OPERATIONS.........

1.PUSH
2.POP
3.DISPLAY
4.QUIT

Enter your option :: 2

The element popped from stack is : 4
        STACK PRIMITIVE OPERATIONS.........

1.PUSH
2.POP
3.DISPLAY
4.QUIT

Enter your option :: 2

The element popped from stack is : 3
        STACK PRIMITIVE OPERATIONS.........

1.PUSH
2.POP
3.DISPLAY
4.QUIT

Enter your option :: 2

The element popped from stack is : 2
        STACK PRIMITIVE OPERATIONS.........

1.PUSH
2.POP
3.DISPLAY
4.QUIT

Enter your option :: 2

The element popped from stack is : 1
        STACK PRIMITIVE OPERATIONS.........

1.PUSH
2.POP
3.DISPLAY
4.QUIT

Enter your option :: 2

Stack underflow(i.e., stack empty).
The element popped from stack is : 0
        STACK PRIMITIVE OPERATIONS.........

1.PUSH
2.POP
3.DISPLAY
4.QUIT

Enter your option :: 2

Stack underflow(i.e., stack empty).
The element popped from stack is : 0
        STACK PRIMITIVE OPERATIONS.........

1.PUSH
2.POP
3.DISPLAY
4.QUIT

Enter your option :: 3

The current stack elements are:
        STACK PRIMITIVE OPERATIONS.........

1.PUSH
2.POP
3.DISPLAY
4.QUIT

Enter your option :: 4

Process returned 0

Above is the source code for C Program to implement Stack Operations Using Pointer which is successfully compiled and run on Windows System.The Output of the program is shown above .

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C Pointer Solved Programs – C Programming

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Write a C Program to Find Length of String using P... >>
<< Write a C program to perform Array of pointers...