An algorithm to Push element in a Stack.
##An algorithm to Push element in a Stack.
Flowchart given below:
Source Code in C++:
#include <iostream>
#include <stdlib.h>
#define max 5
using namespace std;
int stack[5],i,top=-1,x;
void push();
void display();
int main(int argc, char const *argv[]) {
int choice;
do {
std::cout << "1.Push an element in the stack." << '\n';
std::cout << "2.Display the stack elements" << '\n';
std::cout << "3.Exit" << '\n';
std::cout << "Enter your choice: ";
std::cin >> choice;
switch (choice) {
case 1:
push();
break;
case 2:
display();
break;
case 3:
exit(0);
}
} while(choice!=3);
return 0;
}
void push(){
std::cout << "Which no you wanna push: " ;
std::cin >> x;
if (top==max-1) {
std::cout << "Stack is full" << '\n';
}else{
top=top+1;
stack[top]=x;
}
}
void display(){
if (top==-1) {
std::cout << "Stack is empty" << '\n';
}else{
std::cout << "Elements of the stack: " << '\n';
for(i=0;i<=top;i++){
std::cout << stack[i] << '\n';
}
}
}
Output:
1.Push an element in the stack.
2.Display the stack elements
3.Exit
Enter your choice: 1
Which no you wanna push: 11
1.Push an element in the stack.
2.Display the stack elements
3.Exit
Enter your choice: 1
Which no you wanna push: 22
1.Push an element in the stack.
2.Display the stack elements
3.Exit
Enter your choice: 1
Which no you wanna push: 33
1.Push an element in the stack.
2.Display the stack elements
3.Exit
Enter your choice: 1
Which no you wanna push: 44
1.Push an element in the stack.
2.Display the stack elements
3.Exit
Enter your choice: 2
Elements of the stack:
11
22
33
44
1.Push an element in the stack.
2.Display the stack elements
3.Exit
Enter your choice: 3
Press any key to continue . . .
Explanation:
Here we were asked for push an element in a stack.So we first checked if the stack is full or not.If the stack is full,then we can not push any element because there is no memory left to be allocated.
Then we used a menue program to show this.If "top" is at the last element,then the stack is full.If the stack is not full,we can push element and then display which element we have pushed in.
**Happy Coding😊😊**
Flowchart given below:
Source Code in C++:
#include <iostream>
#include <stdlib.h>
#define max 5
using namespace std;
int stack[5],i,top=-1,x;
void push();
void display();
int main(int argc, char const *argv[]) {
int choice;
do {
std::cout << "1.Push an element in the stack." << '\n';
std::cout << "2.Display the stack elements" << '\n';
std::cout << "3.Exit" << '\n';
std::cout << "Enter your choice: ";
std::cin >> choice;
switch (choice) {
case 1:
push();
break;
case 2:
display();
break;
case 3:
exit(0);
}
} while(choice!=3);
return 0;
}
void push(){
std::cout << "Which no you wanna push: " ;
std::cin >> x;
if (top==max-1) {
std::cout << "Stack is full" << '\n';
}else{
top=top+1;
stack[top]=x;
}
}
void display(){
if (top==-1) {
std::cout << "Stack is empty" << '\n';
}else{
std::cout << "Elements of the stack: " << '\n';
for(i=0;i<=top;i++){
std::cout << stack[i] << '\n';
}
}
}
Output:
1.Push an element in the stack.
2.Display the stack elements
3.Exit
Enter your choice: 1
Which no you wanna push: 11
1.Push an element in the stack.
2.Display the stack elements
3.Exit
Enter your choice: 1
Which no you wanna push: 22
1.Push an element in the stack.
2.Display the stack elements
3.Exit
Enter your choice: 1
Which no you wanna push: 33
1.Push an element in the stack.
2.Display the stack elements
3.Exit
Enter your choice: 1
Which no you wanna push: 44
1.Push an element in the stack.
2.Display the stack elements
3.Exit
Enter your choice: 2
Elements of the stack:
11
22
33
44
1.Push an element in the stack.
2.Display the stack elements
3.Exit
Enter your choice: 3
Press any key to continue . . .
Explanation:
Here we were asked for push an element in a stack.So we first checked if the stack is full or not.If the stack is full,then we can not push any element because there is no memory left to be allocated.
Then we used a menue program to show this.If "top" is at the last element,then the stack is full.If the stack is not full,we can push element and then display which element we have pushed in.
**Happy Coding😊😊**
No comments