Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Class notes

Coding

Rating
-
Sold
-
Pages
43
Uploaded on
21-03-2025
Written in
2024/2025

Good coding and great work, And the best program

Institution
Course

Content preview

1.array implementation of a stack:

Aim:
Write a program of array implementation of stack in c++
Algorithm:
Step 1: Initialize the stack with MAX_SIZE = 5, top = -1.
Step 2: Push elements into the stack: [1, 2, 3].
Step 3: Pop an element: returns 3, stack becomes [1, 2].
Step 4: Peek top element: returns 2.
Step 5: Check if stack is empty: returns False. 6. Check if stack is full: returns
False.
Program:
#include <iostream>
using namespace std;
const int MAX_SIZE = 5;
class Stack {
private:
int stack[MAX_SIZE];
int top;
public:
Stack() {
top = -1;
}
void push(int item) {
if (top == MAX_SIZE - 1) {
cout << "Stack Overflow!" << endl;
return;
}

, stack[++top] = item;
cout << "Pushed item: " << item << endl;
}
int pop() {
if (top == -1) {
cout << "Stack Underflow!" << endl;
exit(1);
}
int item = stack[top--];
cout << "Popped item: " << item << endl;
return item;
}
void display() {
if (top == -1) {
cout << "Stack is empty!" << endl;
return;
}
cout << "Stack elements: ";
for (int i = 0; i <= top; i++) {
cout << stack[i] << " ";
}
cout << endl;
}
};
int main() {
Stack stack;
stack.push(10);

, stack.push(20);
stack.push(30);
stack.display();
stack.pop();
stack.display();
return 0;
}
Output:
Pushed item: 10
Pushed item: 20
Pushed item: 30
Stack elements: 10 20 30
Popped item: 30
Stack elements: 10 20

, 2.array implementation of Queue



Aim:
Write a program of array implementation of queue in c++
Algorithm:
Step 1: Initialize the queue with MAX_SIZE = 5, front = -1, rear = -1.
Step 2: Enqueue elements into the queue: [1, 2, 3].
Step 3: Dequeue an element: returns 1, queue becomes [2, 3].
Step 4: Peek front element: returns 2.
Step 5: Check if queue is empty: returns False. 6. Check if queue is full: returns
False.
Program:
#include <iostream>
using namespace std;
const int MAX_SIZE = 5;
class Queue {
private:
int queue[MAX_SIZE];
int front;
int rear;
public:
Queue() {
front = -1;
rear = -1;
}
void enqueue(int item) {
if (rear == MAX_SIZE - 1) {

Written for

Institution
Course

Document information

Uploaded on
March 21, 2025
Number of pages
43
Written in
2024/2025
Type
Class notes
Professor(s)
Vishnu
Contains
All classes

Subjects

$9.79
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller
Seller avatar
vishnuvishnu6

Get to know the seller

Seller avatar
vishnuvishnu6 Madras university
Follow You need to be logged in order to follow users or courses
Sold
-
Member since
1 year
Number of followers
0
Documents
29
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions