stack implementation

#include <stdio.h>

int MAXSIZE = 8;       
int stack[8];     
int top = -1;            

int isempty() {

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

   if(top == MAXSIZE)
      return 1;
   else
      return 0;
}

int peek() {
   return stack[top];
}

int pop() {
   int data;
	
   if(!isempty()) {
      data = stack[top];
      top = top - 1;   
      return data;
   } else {
      printf("Could not retrieve data, Stack is empty.\n");
   }
}

int push(int data) {

   if(!isfull()) {
      top = top + 1;   
      stack[top] = data;
   } else {
      printf("Could not insert data, Stack is full.\n");
   }
}

int main() {
   // push items on to the stack 
   push(3);
   push(5);
   push(9);
   push(1);
   push(12);
   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;
}

0
5
Raqshrag 75 points

                                    #include &lt;iostream&gt;
using namespace std;
int top = -1; //Globally defining the value of top as the stack is empty

    void push (int stack[ ] , int x , int n)
    {
        if ( top == n-1 )       //If the top position is the last of position of the stack, this means that the stack is full.
        {
            cout &lt;&lt; &quot;Stack is full.Overflow condition!&quot; ;
        }
        else
        {
            top = top +1 ;            //Incrementing the top position 
            stack[ top ] = x ;       //Inserting an element on incremented position  
        }
    }
    bool isEmpty ( )
    {
        if ( top == -1 )  //Stack is empty
            return true ; 
        else
            return false;
    }
    void pop ( ) 
    {

        if( isEmpty ( ) )
        {
            cout &lt;&lt; &quot;Stack is empty. Underflow condition! &quot; &lt;&lt; endl ;
        }
        else    
        {
             top = top - 1 ; //Decrementing top&rsquo;s position will detach last element from stack            
        }
    }
    int size ( )
    {
        return top + 1;
    }
    int topElement (int stack[])
    {
        return stack[ top ];
    }
    //Let's implement these functions on the stack given above 

    int main( )
    {
        int stack[ 3 ];
        // pushing element 5 in the stack .
        push(stack , 5 , 3 ) ;

        cout &lt;&lt; &quot;Current size of stack is &quot; &lt;&lt; size ( ) &lt;&lt; endl ;

        push(stack , 10 , 3);
        push (stack , 24 , 3) ;

        cout &lt;&lt; &quot;Current size of stack is &quot; &lt;&lt; size( ) &lt;&lt; endl ;

        //As the stack is full, further pushing will show an overflow condition.
        push(stack , 12 , 3) ;

        //Accessing the top element
        cout &lt;&lt; &quot;The current top element in stack is &quot; &lt;&lt; topElement(stack) &lt;&lt; endl;

        //Removing all the elements from the stack
        for(int i = 0 ; i &lt; 3;i++ )
            pop( );
        cout &lt;&lt; &quot;Current size of stack is &quot; &lt;&lt; size( ) &lt;&lt; endl ;

        //As the stack is empty , further popping will show an underflow condition.
        pop ( );  

    }

0
0
4
7
Weidler 100 points

                                    typedef struct Nodo{
   Elem val;
   struct Nodo *next;
} *Stack;
Stack Empty(){return NULL;}
bool IsEmpty(Stack a){return a==NULL;}
Elem Top(Stack a){return a-&gt;val;} 
Stack Pop(Stack l){return l-&gt;next;}
Stack Push(Elem x,Stack res){
    Stack nuevo=(Stack)malloc(sizeof(struct Nodo));
    nuevo-&gt;val=x;
    nuevo-&gt;next=res;
    return nuevo;
}

4 (7 Votes)
0
3.75
4
Ddodsworthii 105 points

                                    #include &lt;iostream&gt;
using namespace std;
int stack[100], n=100, top=-1;
void push(int val) {
&nbsp; &nbsp;if(top&gt;=n-1)
&nbsp; &nbsp;cout&lt;&lt;&quot;Stack Overflow&quot;&lt;&lt;endl;
&nbsp; &nbsp;else {
&nbsp; &nbsp; &nbsp; top++;
&nbsp; &nbsp; &nbsp; stack[top]=val;
&nbsp; &nbsp;}
}
void pop() {
&nbsp; &nbsp;if(top&lt;=-1)
&nbsp; &nbsp;cout&lt;&lt;&quot;Stack Underflow&quot;&lt;&lt;endl;
&nbsp; &nbsp;else {
&nbsp; &nbsp; &nbsp; cout&lt;&lt;&quot;The popped element is &quot;&lt;&lt; stack[top] &lt;&lt;endl;
&nbsp; &nbsp; &nbsp; top--;
&nbsp; &nbsp;}
}
void display() {
&nbsp; &nbsp;if(top&gt;=0) {
&nbsp; &nbsp; &nbsp; cout&lt;&lt;&quot;Stack elements are:&quot;;
&nbsp; &nbsp; &nbsp; for(int i=top; i&gt;=0; i--)
&nbsp; &nbsp; &nbsp; cout&lt;&lt;stack[i]&lt;&lt;&quot; &quot;;
&nbsp; &nbsp; &nbsp; cout&lt;&lt;endl;
&nbsp; &nbsp;} else
&nbsp; &nbsp;cout&lt;&lt;&quot;Stack is empty&quot;;
}
int main() {
&nbsp; &nbsp;int ch, val;
&nbsp; &nbsp;cout&lt;&lt;&quot;1) Push in stack&quot;&lt;&lt;endl;
&nbsp; &nbsp;cout&lt;&lt;&quot;2) Pop from stack&quot;&lt;&lt;endl;
&nbsp; &nbsp;cout&lt;&lt;&quot;3) Display stack&quot;&lt;&lt;endl;
&nbsp; &nbsp;cout&lt;&lt;&quot;4) Exit&quot;&lt;&lt;endl;
&nbsp; &nbsp;do {
&nbsp; &nbsp; &nbsp; cout&lt;&lt;&quot;Enter choice: &quot;&lt;&lt;endl;
&nbsp; &nbsp; &nbsp; cin&gt;&gt;ch;
&nbsp; &nbsp; &nbsp; switch(ch) {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case 1: {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cout&lt;&lt;&quot;Enter value to be pushed:&quot;&lt;&lt;endl;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cin&gt;&gt;val;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; push(val);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case 2: {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pop();
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case 3: {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; display();
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case 4: {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cout&lt;&lt;&quot;Exit&quot;&lt;&lt;endl;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;default: {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cout&lt;&lt;&quot;Invalid Choice&quot;&lt;&lt;endl;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}
&nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp;}while(ch!=4);
&nbsp; &nbsp;return 0;
}

3.75 (4 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
stack implementation in c using array stack implementation in c using structure how to implementation a stack in c using structure static stack implementation in c algorithm static stack implementation in c stack program in data structure in c stack in function stack in data structure in c stack implement in c stack approach implement stack what is a stack in c programming synta stack implementation ways stack applications in c program with algorithm stack example in c implementation of stack c stack explained stack example how to implement stack stack using array implementation in c can we use stack in c programming declare stack in c stack methods why using stack in C implement stack c c simple stack implementation display the stack in c programming stack implementation c with list function and stack in C stack &lt;int&gt; c stack builtin function in c how to implement stack in c using library c built in stack implementation of stack using array in c stacking c how a stack is implemented in c create a stack using structure in c stack structure in c c stack example stack c implementation call stack in c Write the code in C to implement a stack. simple program for implementing stack in c c language stack implementation c language stack functions implementation of stack program in c is c stack based simple stack in c implement stack in c program whats on the stack c function stack in c c programming stack struct c programming stack using stack in c Implement stack using array in C stack operations c operations on a stack c static stack program in c simple stack operations c stack node implementation c stack implementation c how does a stack work c stack implementation on c stack c library function stack algorithm c program stack in c stack meaning in c Implement stack operation using array in C. stack operations c library stack c program implement stack using array in c program stack adt in c How to create a stack in c stack stl in c stack program in c language stack in c language implementation of stack in c what is a stack in c c stack *stack does c have a stack stack method in c what is stack in c stack using array in c program stack using structure in c how to define the stack in c language c stack explained basic stack operations in c stack in programming c stack implementation in jaba pop push and peek how to use implementation of stack for n numbers write a program of n number of stack basic stack operations c++ stack implementation c++ stacks implementation basic common operations on stack stack implementation in java with array stack implementation c++ main operations for stack how to declare a stack in c List out the operations performed in stack stack operation write a program to implement stack data structure with push pop and display as function stack operations program in c write a program that implements a stack ussing array and support push() pop() top() operations test the program using 5 integers stack push implemtation stack adt in c++ HOW TO WRITE A STACK FUNCTION stack code example Write the C code to push an element (e) into the stack? stack implementation using array in c++ c stack full c stack program stack operation program in c c pop stack STACK implement c pop from stack list out all applications of stacks geeksforgeeks how to pop from a stack in c time complexity in array version implementation of stack stack using arary What operations can be performed on stacks? b) Explain the various operations of a stack data structure. Write C functions to implement the how to code for stacking stack c code c push stack array of stack exemple stack data structure in java example stack implementation in cpp stack in array implementation what is a stack in c programming stack implemention using arrays implement stack in C how to creat stack input a sequence in c in stack operations of stack coding push and pop operation in stack in data structure in java stack in c implementation write a c program for stack using array in data structure how to push data in stack in c stack data structure c++ code three basic basic operations on stack push pop in java using array cbasic operations on a stack using choices in stack in c stack push pop in c stack c++ code C program to implement stack operations common operations on stack b. Given a stack implement the following operations on stack push (), pop () and display () WAP adt in data structure stack implimentation in c print stack top after three operation push pop inc implementing stack in c programs using stack in c++ stack functions in c java stack using array stack insertion and deletion program in c stack implementation in c++ how to make stack in C stack opertaion in c stack operation in c create stack in c implement stack in c++ push pop stack in c example create stack c code to create a stack in c with output stack methods in c stack pop and push how to implement stack using array in c++ geeksforgeeks stack creation in c code implement stack using array java implimentys stack using array create a stack implement stack from scratch creating a stack array C program using stack pop stack implementation stack implementation push stack array implementation in c contents of stack in c the stack in c stack implementation in java using array stack data structure in c++ with example stack using queue in c stack architecture python stack implementation how to define stack in c stack using queue method in stack implementation of stack using queue in C array stack structure implementation of stack all the operations on stacks using inbuilt functions all the operations on stacks A stack is a linear data structure with three basic operations stacks using array data structure stack implementation program for push operation in stack stack using c .,push ,[] using these in c programming how to use stack in c stack c++ implementation how to declare stack in c push(S,S);PUSH(D,S);Pop();Pop(); stack implementation without using array methods stack as struct array in C stack list as an array in C java how to implement a stack how to create stack in c underflow in case of stack implementation implwmwnt concept of push operation in c push and pop in c stack data structure c++ stack array implementation stack programs Show implementation of Stack using array with size 5. show implementation of stack using array with size 5 in java stack in c using structure in c Which coding stack in mainely working with? Write a C Program to implement LIFO structure. Use array Implementation. stack implementation using array in c stack cpp gfg how to implement an array stack array implementation of stack c++ stack data structure implementation java stack array implementation java stacks can be implemented using what basic common operations on a Stack how to create a stack of any type application example for a stack in operating system c++ stack implementations using stack implement stack stack program output stack implementation in c language stack peek pop push insertion via stack in c implementing a stack using an array in c a stack implements an arrya The stack provides 3 major operations: push (add an element at the top of the stack), pop (take the last added element from the top of the stack) and peek (get the element from the top of the stack without removing it). stack implementationin c java stack implementation how to implement a stack in java Implement stack in C? c stack structure stacks in c programming program stack c stack implementation push and pop stack using array java algorithm simple stack[-1] diagram stack and operations program in c Common implementation of stacks stack algorithm in c stack operations in c example output for an stack ways of implement stack in programming pushing in stack in app implementing stack using arrays is better?? stacks example programs in data structure stack basic operations opetions performed on stack stack array push, pop, peek, how to implement stack in c how to make stack howto make stack how to use stack data structure in java c use the programm stack c use stack Stack using an Array wap in c language to perform all operation in a stack array statck pop in stack in c stacks in c' stack implementation usig arry gfg running a stack array stack implementation in c exampele code example code for stack in c the basic common operations on a stack are push function for stack in c pop from stack in c implement a stack wave2vec implementation stack implementing a stack in c stack geeksforgeeks c how is stack structure in c how is stack in c stack in data structure pop function in stack in c push stack code java push stack code stack adt with array java implementing stack ADT with arrays implementing stacks adt implement stack with an array explain how stack push works in c Show the stack operations and stack contents as the traversal occurs. implement a stack in java stack using implementing stack using array all ways to implement a stack c program application of the stack implementation ofstack in java how to push the elements of an array to a stack pop operation in stack stack using array java stack implementtaion in c++ on gfg stack implementation using stack stack implementation using array in c sanford basic implementation of stack stack en c STACK USINGH ARRAY basic operation of stack using array using JAVA how to program a stack The three basic stack operations are creating a stack with a node c++ stack application program in c stack java push stack in c implementation of stack java array implementation of stack in java how many stacks in c implement stack using structure in c++ c implementation of stack condition to check for push in stack implementation of stack push and pop operations in c++ stack best implementation data structure implementation of stack using array in data structure in c stack java implementation array stack in c Push, pop &amp; display stack elements stack implementation using array in java code for stack with output pop element from stack in c Stack Operations Implementation how to create a user driven driver code for push and pop operations of stack implement stack using array gfg create stack using array in java code for stack in c implementation of stack stack push and pop stack data structure program in c Stack using array emlmntaion stack implementation using java a stack of integers in c array stack in java first in last out stack in java The basic common operations on a Stack are: how to make a stack stack c operations ins atck pop() push() stack how to display a stack with all elements in c++ stack implimetation stack in c programming Implementation of Stack using Array push pop operation in stack in c for three numbers push and pop operation in stack in C stack using array in java implementing stack implement stack using arrays implenting a stack in java program to take input and push it to stack in c how many arrays to implement stack 3 operations of stack what are three basic operations of a stack simple c program for stack implementation condition to pop an element from the stack c program stack c program stack structure stack data structure C implement stack using array with o(n) When writing a programming language in C++ how do you implement stack stack operation push pop functions program using c implementation of stack from stack stack in c geeksforgeeks making stack in c geeksforgeeks define stack data structure and explain different ways of implementation of stack which one will you choose and why stacks in cpp geeksforgeeks stacks with arrays stack of stacks c stack with array in c stack program implement stacks with C implementing stacks in C pop implementation in a stack how to write stack in c stack data structure in c java implementation of stack using arrays c ctack pop implementation in c c language stack structure c stack implementation create stack in java for(Stack s : c) {} best way to implement stack stack in java implementation implementation of stack using structure in c implementing stack in java stack operations stack methodin c &bull; A stack data structure is given with push and pop operations. WAP to implement a queue using instances of stack data structure and operations on them. implement a stack using array push and pop in stack in c implement stacks in java stack program c stack and its operations stack c stack implementation of stack in java stacks data structure in C time taken for array implementation of stack in c++ stack insertion program in c stack using struct in c stack push and pop program in c how to make a stack in c stack top c stack implementation javs stack in c coding a stack using node class in javascript stack program in c how to implement a stack data structure in java stack cofe implement a stack java basic operations iof stack in java stack data structure in c++ stack in java stack implimentation java insertion and deletion in stack using array stack java implementation array implementation of stack stack in c using array Write a Java or C++ program that will perform the following sequence of operations: stacks Write a program to implement a Stack using Array. stack with array java implementation of stack stack program in c++ pseudo code for Menu driven C program to implement stack using linked list. Stack program java stack data structure in java stack program for push and pop in java stack code in c write a program to implement stack using array in c programming implement stack in java how to implement stack using arrays in javaa stack C++ stack using array in c WAP to implement stack using array. develop an application cpp stack to store struct stack array implementation c++ push c code stack isFull code in c stack as array stack using arrays stack using array stack implementation array c++ stack implementation array create an array stack stack implementation in array stacks in c using arrays array implementation of stack in c stack implementation using array immplementing stack using array implement stack push pop inc functions cpp Stack data from an array stack implementation using array java stack code stack implementaion c++ gfg stack functions stack array operations on stack in java using an array to implement a stack how to implement stack in java stack implementation in java stack implementation in c how to implement a stack initialize stack with list initialize stack with lsit implement stack operations using arrays stack push pop creating a stack in c stack declaration in c how to pop without stack or array stacks in c stack implementation java from stack create a stack in c various operations on a stack using stack class c add to stack c stack code stacks stack implementation in java geeksforgeeks implement stack using array stack implementation
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source