Write a C Program to implement Stack Operations Using Arrays

By | 17.11.2016

Implement Stack Operations Using Arrays


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


What is an Array ?


Arrays a kind of data structure that can store a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.

Instead of declaring individual variables, such as number0, number1, …, and number99, you declare one array variable such as numbers and use numbers[0], numbers[1], and …, numbers[99] to represent individual variables. A specific element in an array is accessed by an index.

All arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element.


Here is source code of the C Program to implement Stack Operations Using Arrays. The C program is successfully compiled and run(on Codeblocks) on a Windows system. The program output is also shown in below.


SOURCE CODE : :


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

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

#define size 5
struct stack {
   int s[size];
   int top;
} st;

int stfull() {
   if (st.top >= size - 1)
      return 1;
   else
      return 0;
}

void push(int item) {
   st.top++;
   st.s[st.top] = item;
}

int stempty() {
   if (st.top == -1)
      return 1;
   else
      return 0;
}

int pop() {
   int item;
   item = st.s[st.top];
   st.top--;
   return (item);
}

void display() {
   int i;
   if (stempty())
      printf("\nStack Is Empty!");
   else {
      for (i = st.top; i >= 0; i--)
         printf("\n%d", st.s[i]);
   }
}

int main() {
   int item, choice;
   char ans;
   st.top = -1;

   printf("\t\t\tImplementation Of Stack ");
   do {
      printf("\n\n1.Push \n2.Pop \n3.Display \n4.exit\n");
      printf("\nEnter Your Choice :: ");
      scanf("%d", &choice);
      switch (choice) {
      case 1:
         printf("\nEnter The item to be pushed :: ");
         scanf("%d", &item);
         if (stfull())
            printf("\nStack is Full!");
         else
            push(item);
         break;
      case 2:
         if (stempty())
            printf("\nEmpty stack!Underflow !!");
         else {
            item = pop();
            printf("\nThe popped element is %d", item);
         }
         break;
      case 3:
         display();
         break;
      case 4:
         exit(0);
      }
      printf("\nDo You want To Continue? ");
      ans = getche();
   } while (ans == 'Y' || ans == 'y');

return 0;
}

OUTPUT : :


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

                 Implementation Of Stack

1.Push
2.Pop
3.Display
4.exit

Enter Your Choice :: 1

Enter The item to be pushed :: 1

Do You want To Continue? y

1.Push
2.Pop
3.Display
4.exit

Enter Your Choice :: 1

Enter The item to be pushed :: 2

Do You want To Continue? y

1.Push
2.Pop
3.Display
4.exit

Enter Your Choice :: 1

Enter The item to be pushed :: 3

Do You want To Continue? y

1.Push
2.Pop
3.Display
4.exit

Enter Your Choice :: 3

3
2
1
Do You want To Continue? y

1.Push
2.Pop
3.Display
4.exit

Enter Your Choice :: 2

The popped element is 3
Do You want To Continue? y

1.Push
2.Pop
3.Display
4.exit

Enter Your Choice :: 2

The popped element is 2
Do You want To Continue? y

1.Push
2.Pop
3.Display
4.exit

Enter Your Choice :: 2

The popped element is 1
Do You want To Continue? y

1.Push
2.Pop
3.Display
4.exit

Enter Your Choice :: 3

Stack Is Empty!
Do You want To Continue? 4

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach up to you in short interval.


Thanks for reading the post….

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments