You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Write a program to implement a Stack using Array. Your task is to use the class as shown in the comments in the code editor and complete the functions push() and pop() to implement a stack.
Example 1:
Input:
push(2)
push(3)
pop()
push(4)
pop()
Output: 3, 4
Explanation:
push(2) the stack will be {2}
push(3) the stack will be {2 3}
pop() poped element will be 3,
the stack will be {2}
push(4) the stack will be {2 4}
pop() poped element will be 4
Example 2:
Input:
pop()
push(4)
push(5)
pop()
Output: -1, 5
Your Task:
You are required to complete two methods push() and pop(). The push() method takes one argument, an integer 'x' to be pushed into the stack and pop() which returns an integer present at the top and popped out from the stack. If the stack is empty then return -1 from the pop() method.
Expected Time Complexity : O(1) for both push() and pop().
Expected Auixilliary Space : O(1) for both push() and pop().
Constraints:
1 <= Q <= 100
1 <= x <= 100
The text was updated successfully, but these errors were encountered:
// C program for array implementation of stack#include<limits.h>#include<stdio.h>#include<stdlib.h>// A structure to represent a stackstructStack {
inttop;
unsignedcapacity;
int*array;
};
// function to create a stack of given capacity. It initializes size of// stack as 0structStack*createStack(unsignedcapacity)
{
structStack*stack= (structStack*)malloc(sizeof(structStack));
stack->capacity=capacity;
stack->top=-1;
stack->array= (int*)malloc(stack->capacity*sizeof(int));
returnstack;
}
// Stack is full when top is equal to the last indexintisFull(structStack*stack)
{
returnstack->top==stack->capacity-1;
}
// Stack is empty when top is equal to -1intisEmpty(structStack*stack)
{
returnstack->top==-1;
}
// Function to add an item to stack. It increases top by 1voidpush(structStack*stack, intitem)
{
if (isFull(stack))
return;
stack->array[++stack->top] =item;
printf("%d pushed to stack\n", item);
}
// Function to remove an item from stack. It decreases top by 1intpop(structStack*stack)
{
if (isEmpty(stack))
returnINT_MIN;
returnstack->array[stack->top--];
}
// Function to return the top from stack without removing itintpeek(structStack*stack)
{
if (isEmpty(stack))
returnINT_MIN;
returnstack->array[stack->top];
}
// Driver program to test above functionsintmain()
{
structStack*stack=createStack(100);
push(stack, 4);
push(stack, 5);
printf("%d popped from stack\n", pop(stack));
return0;
}
Write a program to implement a Stack using Array. Your task is to use the class as shown in the comments in the code editor and complete the functions push() and pop() to implement a stack.
Example 1:
Input:
push(2)
push(3)
pop()
push(4)
pop()
Output: 3, 4
Explanation:
push(2) the stack will be {2}
push(3) the stack will be {2 3}
pop() poped element will be 3,
the stack will be {2}
push(4) the stack will be {2 4}
pop() poped element will be 4
Example 2:
Input:
pop()
push(4)
push(5)
pop()
Output: -1, 5
Your Task:
You are required to complete two methods push() and pop(). The push() method takes one argument, an integer 'x' to be pushed into the stack and pop() which returns an integer present at the top and popped out from the stack. If the stack is empty then return -1 from the pop() method.
Expected Time Complexity : O(1) for both push() and pop().
Expected Auixilliary Space : O(1) for both push() and pop().
Constraints:
1 <= Q <= 100
1 <= x <= 100
The text was updated successfully, but these errors were encountered: