0% found this document useful (0 votes)
6 views4 pages

Stack

A stack is a linear data structure that operates on a last-in, first-out (LIFO) basis, allowing insertion and deletion at one end called the top. It can be implemented using arrays or linked lists, with operations including push(), pop(), peek(), isEmpty(), and isFull(). The document also provides an algorithm and sample code for stack operations using an array in C.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views4 pages

Stack

A stack is a linear data structure that operates on a last-in, first-out (LIFO) basis, allowing insertion and deletion at one end called the top. It can be implemented using arrays or linked lists, with operations including push(), pop(), peek(), isEmpty(), and isFull(). The document also provides an algorithm and sample code for stack operations using an array in C.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Stack

● A stack is a linear data structure in which insertion and deletion


of elements are
done at only one end,which is known as the top of the stack.
● Stack is called a last-in, first-out (LIFO) structure because the last
element which is
added to the stack is the first element which is deleted from the stack.
● In the computer’s memory, stacks can be implemented using arrays
or linked lists.in
both the cases insertion & deletion is allowed at the one end only.
● Example:piles of books, a deck of cards, piles of money, and
many more.

● Every stack has a variable top associated with it. top is used to store
the address of the
topmost element of the stack. It is this position from where the
element will be added or
deleted. There is another variable MAX, which is used to store the
maximum number of
elements that the stack can store.
● If top = NULL, then it indicates that the stack is empty
● and if top = MAX–1, then the stack is full.

OPERATIONS ON A STACK(USING ARRAY OR LINKED


LIST:
1. push() to insert an element into the stack
2. pop() to remove an element from the stack
3. peek() to retrieve the topmost element within the stack, without
deleting it.
used to check the status of the stack with the help of the top pointer.
isEmpty() returns true if stack is empty else false.
isFull() returns true if the stack is full else false.
ARRAY REPRESENTATION OF STACKS:

● In the computer’s memory, stacks can be represented as a linear


array.
● Every stack has a variable called TOP associated with it, which is used
to store the
address of the topmost element of the stack. It is this position
where the element will
be added to or deleted from.
● There is another variable called MAX, which is used to store the
maximum number of
elements that the stack can hold.If TOP = NULL, then it indicates that the
stack is
empty and if TOP = MAX–1, then the stack is full.
(You must be wondering why we have written MAX–1. It is because array
indices start from 0.)

STACK USING ARRAY ALGORITHM:


top = -1 is commonly used in array-based stack
implementations.
PUSH():
Step-1: If TOP == Max-1
Print “Overflow”
Goto Step 4
Step-2: Set TOP= TOP + 1
Step-3: Set Stack[TOP]= ELEMENT Step-4: END
POP():
Step-1: If TOP==-1
Print “Underflow”
Goto Step 4
Step-2: Set VAL= Stack[TOP] Step-3: Set TOP= TOP-1 Step-
4: END
PEEK():
Step-1: If TOP ==-1
PRINT “Stack is Empty”
Goto Step 3
Step-2: Return Stack[TOP] Step-3: END
#include <stdio.h>
#include <stdlib.h>

#define MAX 5

int stack[MAX];
int top = -1;

void push(int x) {
if (top == MAX - 1) {
printf("Stack Overflow\n");
return;
}
stack[++top] = x;
}

int pop() {
if (top == -1) {
printf("Stack Underflow\n");
return -1;
}
return stack[top--];
}

int peek() {
if (top == -1) {
printf("Stack is empty\n");
return -1;
}
return stack[top];
}

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

int main() {
push(1);
push(2);
push(3);
printStack(); // prints: 1 2 3
printf("Top element: %d\n", peek()); // prints: Top element: 3
printf("Popped: %d\n", pop()); // prints: Popped: 3
printStack(); // prints: 1 2
printf("Top element: %d\n", peek()); // prints: Top element: 2
return 0;
}

You might also like