Q:

Write a C Program to implement Stack Operations Using Arrays

0

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

All Answers

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

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

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

total answers (1)

C Arrays 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 Implement Queue using an Arra... >>
<< Write a C Program to Find Union and Intersection o...