Open navigation menu
Close suggestions
Search
Search
en
Change Language
Upload
Sign in
Sign in
Download free for days
0 ratings
0% found this document useful (0 votes)
3 views
Stack Using Array
Uploaded by
Ayush Sharma
AI-enhanced title
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download now
Download
Save stack using array For Later
Download
Save
Save stack using array For Later
0%
0% found this document useful, undefined
0%
, undefined
Embed
Share
Print
Report
0 ratings
0% found this document useful (0 votes)
3 views
Stack Using Array
Uploaded by
Ayush Sharma
AI-enhanced title
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download now
Download
Save stack using array For Later
Carousel Previous
Carousel Next
Save
Save stack using array For Later
0%
0% found this document useful, undefined
0%
, undefined
Embed
Share
Print
Report
Download now
Download
You are on page 1
/ 2
Search
Fullscreen
#include <stdio.
h>
#include <stdlib.h>
#define MAX 100 // Maximum size of the stack
// Structure to represent a stack
struct Stack {
int arr[MAX]; // Array to store stack elements
int top; // Index of the top element
};
// Function to initialize the stack
void initStack(struct Stack* stack) {
stack->top = -1; // Set top to -1 indicating an empty stack
}
// Function to check if the stack is empty
int isEmpty(struct Stack* stack) {
return stack->top == -1;
}
// Function to check if the stack is full
int isFull(struct Stack* stack) {
return stack->top == MAX - 1;
}
// Function to push an element onto the stack
void push(struct Stack* stack, int data) {
if (isFull(stack)) {
printf("Stack overflow! Cannot push %d\n", data);
return;
}
stack->arr[++stack->top] = data;
printf("%d pushed to stack\n", data);
}
// Function to pop an element from the stack
int pop(struct Stack* stack) {
if (isEmpty(stack)) {
printf("Stack underflow! Cannot pop\n");
return -1;
}
return stack->arr[stack->top--];
}
// Function to peek the top element of the stack
int peek(struct Stack* stack) {
if (isEmpty(stack)) {
printf("Stack is empty\n");
return -1;
}
return stack->arr[stack->top];
}
// Driver code to test the stack implementation
int main() {
struct Stack stack;
initStack(&stack);
push(&stack, 10);
push(&stack, 20);
push(&stack, 30);
printf("%d popped from stack\n", pop(&stack));
printf("Top element is %d\n", peek(&stack));
return 0;
}
You might also like
CodeNote_Stack_array1
PDF
No ratings yet
CodeNote_Stack_array1
4 pages
Max - Size
PDF
No ratings yet
Max - Size
7 pages
DS Lab Work 4
PDF
No ratings yet
DS Lab Work 4
6 pages
StackOperations
PDF
No ratings yet
StackOperations
2 pages
Stack Implementation Using Arrays
PDF
No ratings yet
Stack Implementation Using Arrays
5 pages
3 RD
PDF
No ratings yet
3 RD
3 pages
DATA STRUCTURE CODES final
PDF
No ratings yet
DATA STRUCTURE CODES final
75 pages
Stack Operation
PDF
No ratings yet
Stack Operation
5 pages
Stack Questions From Geeks4Geeks
PDF
No ratings yet
Stack Questions From Geeks4Geeks
46 pages
stack1
PDF
No ratings yet
stack1
4 pages
maths
PDF
No ratings yet
maths
20 pages
8
PDF
No ratings yet
8
2 pages
DSU2
PDF
No ratings yet
DSU2
3 pages
lab2.q2
PDF
No ratings yet
lab2.q2
2 pages
Stack (Array Implementation)
PDF
No ratings yet
Stack (Array Implementation)
3 pages
Stack:: Algorithm For PUSH Operation
PDF
No ratings yet
Stack:: Algorithm For PUSH Operation
4 pages
DSA_UNIT-3
PDF
No ratings yet
DSA_UNIT-3
149 pages
Stack Data Structure
PDF
No ratings yet
Stack Data Structure
12 pages
Middel Element
PDF
No ratings yet
Middel Element
5 pages
Queue & Stack
PDF
No ratings yet
Queue & Stack
12 pages
15 - Data Structure and Algorithms - Stack
PDF
No ratings yet
15 - Data Structure and Algorithms - Stack
17 pages
Practical ddsdfbggfdh
PDF
No ratings yet
Practical ddsdfbggfdh
14 pages
DSA LAB Task 1
PDF
No ratings yet
DSA LAB Task 1
7 pages
DS Print No 7 To 15
PDF
No ratings yet
DS Print No 7 To 15
33 pages
Lab Program No. 3
PDF
No ratings yet
Lab Program No. 3
2 pages
Experiment 2
PDF
No ratings yet
Experiment 2
4 pages
QUE 1 C
PDF
No ratings yet
QUE 1 C
5 pages
Implementing Stack in C
PDF
No ratings yet
Implementing Stack in C
6 pages
Stack
PDF
No ratings yet
Stack
2 pages
DCXCV
PDF
No ratings yet
DCXCV
2 pages
DS Programs
PDF
No ratings yet
DS Programs
4 pages
4 Stacks Queues
PDF
No ratings yet
4 Stacks Queues
48 pages
Code Stack Push
PDF
No ratings yet
Code Stack Push
3 pages
Data Stracture Lab
PDF
No ratings yet
Data Stracture Lab
30 pages
5 TH
PDF
No ratings yet
5 TH
3 pages
Data Structure Lab 5 Name: Roll No: Section: Teacher: Task 1
PDF
No ratings yet
Data Structure Lab 5 Name: Roll No: Section: Teacher: Task 1
10 pages
DSU Micro Project
PDF
No ratings yet
DSU Micro Project
5 pages
Stacks: EENG212 - Algorithms and Data Structures
PDF
No ratings yet
Stacks: EENG212 - Algorithms and Data Structures
21 pages
Lecture 4
PDF
No ratings yet
Lecture 4
9 pages
DSA_Unit3_Updated
PDF
No ratings yet
DSA_Unit3_Updated
133 pages
Stacks
PDF
No ratings yet
Stacks
19 pages
Tut 3 Stack
PDF
No ratings yet
Tut 3 Stack
18 pages
DSA Lab Tasks 5 (1)
PDF
No ratings yet
DSA Lab Tasks 5 (1)
6 pages
3 Lab Program
PDF
No ratings yet
3 Lab Program
4 pages
Experiment 3
PDF
No ratings yet
Experiment 3
3 pages
Unit-2 - Data Structures Using C
PDF
No ratings yet
Unit-2 - Data Structures Using C
23 pages
Stack PUSH & POP Implementation Using Arrays
PDF
No ratings yet
Stack PUSH & POP Implementation Using Arrays
3 pages
DS Notes
PDF
No ratings yet
DS Notes
4 pages
DSA LAB Programs
PDF
No ratings yet
DSA LAB Programs
59 pages
8 operator parser[1]
PDF
No ratings yet
8 operator parser[1]
3 pages
Multiple Stack
PDF
No ratings yet
Multiple Stack
11 pages
pathakDS
PDF
No ratings yet
pathakDS
38 pages
dsa model 1 with answer
PDF
No ratings yet
dsa model 1 with answer
79 pages
Stack Problems
PDF
No ratings yet
Stack Problems
15 pages
DSViralSirLABSubmission
PDF
No ratings yet
DSViralSirLABSubmission
144 pages
L9 Stack ImplementationArrays
PDF
No ratings yet
L9 Stack ImplementationArrays
27 pages
DSA 1-6 exp (041)
PDF
No ratings yet
DSA 1-6 exp (041)
28 pages
FDS EXP7 Ashish Gurjar
PDF
No ratings yet
FDS EXP7 Ashish Gurjar
4 pages
EENG212 - Algorithms & Data Structures: Stacks
PDF
No ratings yet
EENG212 - Algorithms & Data Structures: Stacks
5 pages
150+ C Pattern Programs
From Everand
150+ C Pattern Programs
Hernando Abella
No ratings yet