Movatterモバイル変換


[0]ホーム

URL:


Data Structures & Algorithms Tutorial

Stack Program in C



C Implementation of Stack

Following is the implementation of basic operations (push(), pop(), peek(), isEmpty(), isFull()) in Stack ADT and printing the output in C programming language −

#include <stdio.h>int MAXSIZE = 8;int stack[8];int top = -1;/* Check if the stack is empty */int isempty(){   if(top == -1)      return 1;   else      return 0;}/* Check if the stack is full */int isfull(){   if(top == MAXSIZE)      return 1;   else      return 0;}/* Function to return the topmost element in the stack */int peek(){   return stack[top];}/* Function to delete from the stack */int pop(){   int data;   if(!isempty()) {      data = stack[top];      top = top - 1;      return data;   } else {      printf("Could not retrieve data, Stack is empty.\n");   }}/* Function to insert into the stack */int push(int data){   if(!isfull()) {      top = top + 1;      stack[top] = data;   } else {      printf("Could not insert data, Stack is full.\n");   }}/* Main function */int main(){   push(44);   push(10);   push(62);   push(123);   push(15);   printf("Element at top of the stack: %d\n" ,peek());   printf("Elements: \n");   // print stack data   while(!isempty()) {      int data = pop();      printf("%d\n",data);   }   printf("Stack full: %s\n" , isfull()?"true":"false");   printf("Stack empty: %s\n" , isempty()?"true":"false");   return 0;}

Output

Element at top of the stack: 15Elements: 15123621044Stack full: falseStack empty: true
stack_algorithm.htm
Print Page
Advertisements

[8]ページ先頭

©2009-2025 Movatter.jp