What is a Stack?
A Stack is a straight information structure that follows the LIFO (Last-In-First-Out) guideline. Stack has one end, though the Queue has two finishes (front and back). It contains just a single pointer top pointer highlighting the highest component of the stack. At whatever point a component is included the stack, it is added on the highest point of the stack, and the component can be erased uniquely from the stack. All inall, a stack can be characterized as a compartment in which inclusion and erasure should be possible from the one end known as the highest point of the stack.
Working of Stack
Stack chips away at the LIFO design. As we can see in the underneath figure there are five memory blocks in the stack; along these lines, the size of the stack is 5. Assume we need to store the components in a stack and how about we expect that stack is vacant. We have taken the pile of size 5 as appeared underneath in which we are pushing the components individually until the stack turns out to be full.
Since our stack is full as the size of the stack is 5. In the above cases, we can see that it goes from the top to the base when we were entering the new component in the stack. The stack gets topped off from the base to the top. At the point when we play out the erase procedure on the stack, there is just a single
route for passage and exit as the opposite end is shut. It follows the LIFO design, which implies that the worth entered first will be eliminated last. In the above case, the worth 5 is entered first, so it will be taken out simply after the cancellation of the multitude of different components.
Standard Stack Operations
push(): When we embed a component in a stack then the activity is known as
a push. On the off chance that the stack is full, at that point the flood condition
happens.
procedure push(item)
if stack is full then
print "Stack overflow"
return
end if
top = top + 1
stack[top] = item
end procedure
pop(): When we erase a component from the stack, the activity is known as a pop. In the event that the stack is unfilled implies that no component exists in the
stack, this state is known as an undercurrent state.
procedure pop()
if stack is empty then
print "Stack underflow"
return
end if
item = stack[top]
top = top - 1
return item
end procedure
peek(): It restores the component at the given position.
procedure peek()
if stack is empty then
print "Stack underflow"
return
end if
return stack[top]
end procedure
display(): It prints all the components accessible in the stack.
Program
#include <stdio.h>
#define MAX 100
int stack[MAX];
int top = -1;
void push(int item) {
if (top == MAX - 1) {
printf("Stack overflow\n");
return;
}
top++;
stack[top] = item;
}
int pop() {
if (top == -1) {
printf("Stack underflow\n");
return -1;
}
int item = stack[top];
top--;
return item;
}
int peek() {
if (top == -1) {
printf("Stack underflow\n");
return -1;
}
return stack[top];
}
void printStack() {
if (top == -1) {
printf("Stack is empty\n");
return;
}
for (int i = top; i >= 0; i--) {
printf("%d ", stack[i]);
}
printf("\n");
}
int main() {
int choice, item;
do {
printf("\n\nStack operations\n");
printf("1. Push\n");
printf("2. Pop\n");
printf("3. Peek\n");
printf("4. Print\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the item to push: ");
scanf("%d", &item);
push(item);
break;
case 2:
item = pop();
if (item != -1) {
printf("Popped item: %d\n", item);
}
break;
case 3:
item = peek();
if (item != -1) {
printf("Top item: %d\n", item);
}
break;
case 4:
printStack();
break;
case 5:
return 0;
default:
printf("Invalid choice\n");
}
} while (1);
}
No comments:
Post a Comment