0% found this document useful (0 votes)
0 views

Task 7-Stack ADT Using Arrays

The document contains a C program that implements a Stack Abstract Data Type (ADT) using arrays. It includes functions for pushing, popping, printing, peeking, and checking the size of the stack, as well as handling user input through a menu-driven interface. The program utilizes a structure to represent the stack and manages its operations with appropriate checks for overflow and underflow conditions.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Task 7-Stack ADT Using Arrays

The document contains a C program that implements a Stack Abstract Data Type (ADT) using arrays. It includes functions for pushing, popping, printing, peeking, and checking the size of the stack, as well as handling user input through a menu-driven interface. The program utilizes a structure to represent the stack and manages its operations with appropriate checks for overflow and underflow conditions.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Task 7:

1. Write a C Program to implement StackADT using Arrays.

#include <stdio.h>

#include <stdlib.h>

#define MAX 10

struct stack {

int items[MAX];

int top;

};

typedef struct stack st;

int isfull(st *s) {

if (s->top == MAX - 1)

return 1;

else

return 0;

int isempty(st *s) {

if (s->top == -1)

return 1;

else

return 0;

void push(st *s) {

int ele;

if (isfull(s)) {

printf("STACK FULL");

} else {

printf("Enter the element to push\n");

scanf("%d",&ele);

s->top++;
s->items[s->top] = ele;

void pop(st *s) {

if (isempty(s)) {

printf("\n STACK EMPTY \n");

} else {

printf("Item popped= %d", s->items[s->top]);

s->top--;

printf("\n");

// Print elements of stack

void printStack(st *s) {

if (isempty(s)) {

printf("\n STACK EMPTY \n");

else

printf("Stack: ");

for (int i = s->top; i >=0;i--) {

printf("%d ", s->items[i]);

printf("\n");

}}

void peek(st *s) {

if (isempty(s)) {

printf("\n STACK EMPTY \n");

} else {

printf("Item on the top= %d\n", s->items[s->top]);

}
void size(st *s)

if (isempty(s)) {

printf("\n STACK EMPTY \n");

else

printf("Size of the stack : %d\n",s->top+1);

int main() {

int ch;

st *s ;

s= (st *)malloc(sizeof(st));

s->top=-1;

while(1)

printf("\n1.PUSH\n2.POP\n3.DISPLAY\n4.PEEK\n5.SIZE OF STACK\n6.EXIT\n");

printf("Enter your choice\n");

scanf("%d",&ch);

switch(ch)

case 1:

push(s);

break;

case 2: pop(s);

break;

case 3: printStack(s);

break;

case 4: peek(s);

break;

case 5: size(s);
break;

case 6: exit(1);

default:printf("Enter correct choice\n");

}}}

You might also like