Download Our Beta Android App And Help Us Build Awesome Stuff!  Download Now.

Stack Implementation Using Array C Program

stack_array.txt
File size: 1.08 KB

File content type: text/plain

Category: Assignment

Course: B.Tech.

Semester: 3

#include<stdio.h>
#define MAXSIZE 5

void push(int);
int pop();
void peek();
int isEmpty();
int isFull();

int stk[MAXSIZE];
int top = -1;

void main() {
	
	int value, choice;
	
	while(1) {
		
		printf("Enter choice.\n1. Push\n2. Pop\n3. Display\n4. Exit\n");
		scanf("%d",&choice);
		
		switch(choice) {
			case 1: if(isFull()) {
						printf("Cannot push. Overflow.\n");
						break;
					}
					printf("Enter number\n");
					scanf("%d",&value);
					push(value);
					break;
					
			case 2: if(isEmpty()) {
						printf("Cannot pop. Underflow.\n");
						break;
					}
					value = pop();
					printf("Value popped = %d\n",value);
					break;
					
			case 3: peek();
					break;
			
			case 4: return;
						
		}
	}
}

int isFull() {
	if(top == MAXSIZE-1)
		return 1;
	return 0;
}

int isEmpty() {
	if(top == -1)
		return 1;
	return 0;
}

void push(int value) {
	stk[++top] = value;
}

int pop() {
	int value = stk[top--];
	return value;
}

void peek() {
	int i;
	for(i=0;i<=top;i++)
		printf("%d ",stk[i]);
	printf("\n");
}

Added by passhojao

Comments