Skip to content

Commit 574230d

Browse files
Add files via upload
1 parent db53792 commit 574230d

File tree

82 files changed

+3027
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+3027
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
## in JAVA
2+
3+
**Step 1:** [Stack DS by Apna College(10min only)](https://youtu.be/JvuaAgDar1c)
4+
5+
**Step 2:** [Implementation of Stack DS in CPP by Simple Snippets](https://youtu.be/08QSylWv6jM)
6+
7+
**Step 3:** [See my Implemented code in java](https://github.com/thepranaygupta/Data-Structures-and-Algorithms/blob/main/02.%20Stack/Basic%20Operation/1.%20Stack%20using%20Array/Stack_Array.java)
8+
9+
## in C++
10+
11+
**Step 1:** [Visualize here](https://www.youtube.com/watch?v=sFVxsglODoo)
12+
13+
**Step 2:** [See my code](./stack_using_array.cpp)
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#include<stdio.h>
2+
#include<stdlib.h>
3+
4+
// #define N 5;
5+
int st[5];
6+
int top = -1;
7+
8+
void push(int n){
9+
if(top == 5-1){
10+
printf("\nOverflow Condition No Further Data Can Be Pushed!\n");
11+
}
12+
else{
13+
top++;
14+
st[top] = n;
15+
}
16+
}
17+
18+
void pop(){
19+
if(top == -1){
20+
printf("\nNO DATA TO POP\n");
21+
}
22+
else{
23+
int item = st[top];
24+
top--;
25+
printf("\nThe deleted item is %d\n" , item);
26+
27+
}
28+
}
29+
30+
void display(){
31+
for (int i = top; i >= 0; i--)
32+
{
33+
printf("%d " , st[i]);
34+
}
35+
36+
}
37+
void peek(){
38+
int item ;
39+
if (top == -1)
40+
{
41+
printf("No Data To Peek!");
42+
}
43+
else{
44+
item = st[top];
45+
printf("The Item at the top is %d" , item);
46+
}
47+
48+
}
49+
50+
51+
int main(){
52+
int choice = 1;
53+
int num;
54+
while (choice)
55+
{
56+
system("CLS");
57+
// clrscr(); clear screen
58+
display();
59+
printf("\nPRESS - 1 FOR PUSH\nPRESS - 2 FOR POP\nPRESS - 3 FOR DISPLAY\nPRESS - 4 TO PEEK \nPRESS - 5 TO EXIT");
60+
printf("\nEnter your Choice : ");
61+
scanf("%d" , &choice);
62+
switch (choice)
63+
{
64+
case 1:
65+
66+
printf("\nEnter the number : ");
67+
scanf("%d" , &num);
68+
push(num);
69+
break;
70+
case 2:
71+
pop();
72+
break;
73+
case 3:
74+
printf("\n");
75+
display();
76+
printf("\n");
77+
break;
78+
case 4:
79+
peek();
80+
break;
81+
case 5:
82+
choice = 0;
83+
break;
84+
85+
default:
86+
printf("\nInvalid Input \n");
87+
break;
88+
}
89+
}
90+
91+
return 0;
92+
}
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
package stackqueue;
2+
3+
import java.util.*;
4+
5+
public class Stack_Array {
6+
7+
public static void main(String args[]) {
8+
Scanner sc = new Scanner(System.in);
9+
System.out.print("Enter size of Stack: ");
10+
int n = sc.nextInt();
11+
stack st = new stack(n);
12+
13+
boolean flag = true;
14+
int val;
15+
int pos = 0;
16+
while (flag) {
17+
18+
System.out.println("1. Push item in Stack");
19+
System.out.println("2. Pop item out from Stack");
20+
System.out.println("3. Current size of stack");
21+
System.out.println("4. Update value at position");
22+
System.out.println("5. View value at position");
23+
System.out.println("6. View Stack");
24+
System.out.println("7. Exit");
25+
26+
System.out.print("Enter Choice: ");
27+
int choice = sc.nextInt();
28+
switch (choice) {
29+
30+
case 1:
31+
System.out.print("Enter value: ");
32+
val = sc.nextInt();
33+
st.push(val);
34+
break;
35+
36+
case 2:
37+
System.out.println(st.pop());
38+
break;
39+
40+
case 3:
41+
System.out.println(st.count());
42+
break;
43+
44+
case 4:
45+
System.out.print("Enter value: ");
46+
val = sc.nextInt();
47+
System.out.print("Enter position: ");
48+
pos = sc.nextInt();
49+
st.updateValue(pos, val);
50+
break;
51+
52+
case 5:
53+
System.out.print("Enter position: ");
54+
pos = sc.nextInt();
55+
st.peek(pos);
56+
break;
57+
58+
case 6:
59+
st.display();
60+
break;
61+
62+
case 7:
63+
flag = false;
64+
break;
65+
66+
default:
67+
System.out.println("Invalid Choice");
68+
}
69+
System.out.println();
70+
}
71+
72+
}
73+
74+
}
75+
76+
class stack {
77+
private int top;
78+
private int arr[];
79+
private int n;
80+
81+
public stack(int n) {
82+
top = -1;
83+
this.n = n;
84+
arr = new int[this.n];
85+
}
86+
87+
// this tells if the stack is empty or not
88+
public boolean isEmpty() {
89+
if (top == -1)
90+
return true;
91+
else
92+
return false;
93+
}
94+
95+
// this tells if the stack is full or not
96+
public boolean isFull() {
97+
if (top == n - 1)
98+
return true;
99+
else
100+
return false;
101+
}
102+
103+
// this will place an item on the stack
104+
public void push(int val) {
105+
if (isFull()) {
106+
System.out.println("Stack Overflow");
107+
} else {
108+
top++;
109+
arr[top] = val;
110+
}
111+
}
112+
113+
// this will return the item at the top of the stack and then remove it
114+
public int pop() {
115+
if (isEmpty()) {
116+
System.out.println("Stack Underflow");
117+
return 0;
118+
} else {
119+
int popValue = arr[top];
120+
arr[top] = 0;
121+
top--;
122+
return popValue;
123+
}
124+
}
125+
126+
// this will return the no of items in the stack
127+
public int count() {
128+
return (top + 1);
129+
}
130+
131+
// this will return top value of a stack
132+
public int Top() {
133+
if (isEmpty()) {
134+
System.out.println("Stack Underflow");
135+
return 0;
136+
}
137+
return arr[top];
138+
}
139+
140+
// this will access the value at given position
141+
public int peek(int pos) {
142+
if (isEmpty()) {
143+
System.out.println("Stack Underflow");
144+
return 0;
145+
} else {
146+
return arr[pos];
147+
}
148+
}
149+
150+
// this will update the value of a given positon
151+
public void updateValue(int pos, int val) {
152+
if (pos > count()) {
153+
System.out.println("Stack Overflow");
154+
} else {
155+
arr[pos - 1] = val;
156+
System.out.println("Value Updated at position " + pos);
157+
}
158+
159+
}
160+
161+
// display all the items of the stack
162+
void display() {
163+
if (isEmpty()) {
164+
System.out.println("Stack is Empty");
165+
} else {
166+
System.out.println("All values in the Stack are: ");
167+
for (int i = top; i >= 0; i--) {
168+
System.out.println(arr[i]);
169+
}
170+
}
171+
}
172+
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
//C++ program of stack using arrays
2+
#include<iostream>
3+
#include<stdio.h>
4+
#include<conio.h>
5+
#include<process.h>
6+
7+
using namespace std;
8+
#define SIZE 5
9+
//make a class & name it stk
10+
class stk
11+
{
12+
int stack[SIZE]; //stack using arrays
13+
int top;
14+
public:
15+
void push();
16+
int pop();
17+
void display();
18+
stk()
19+
{
20+
top=-1;
21+
}
22+
};
23+
24+
void stk::push() //push function definition
25+
{
26+
int item;
27+
if(top==SIZE-1)
28+
{
29+
cout<<"Stack is Full\n";
30+
return(0);
31+
32+
}
33+
else{
34+
cout<<"Enter element to push: \n";
35+
cin>>item;
36+
top=top+1;
37+
stack[top]=item;
38+
39+
}
40+
}
41+
int stk::pop()
42+
{
43+
int temp;
44+
if(top==-1)
45+
{
46+
cout<<"Empty Stack:\n";
47+
return(0);
48+
}
49+
else
50+
{
51+
temp=stack[top];
52+
top=top-1;
53+
}return(temp);
54+
}
55+
56+
//travese and display
57+
void stk::display()
58+
{
59+
if(top==-1)
60+
cout<<"stack empty\n";
61+
else
62+
{
63+
for(int i=top; i>=0; i--)
64+
{
65+
cout<<"Elements are:\t";
66+
cout<<stack[i];
67+
68+
}
69+
}cout<<endl;
70+
}
71+
72+
int main(){
73+
74+
int option=0;
75+
stk s;
76+
int element;
77+
while(1)
78+
{
79+
cout<<"enter 1 to push:\nenter 2 for pop:\n";
80+
cout<<"enter 3 for display:\nenter 4 for exit\n";
81+
82+
83+
cout<<"Enter option: \n";
84+
cin>>option;
85+
86+
switch(option){
87+
case 1:
88+
s.push();
89+
break;
90+
case 2:
91+
element=s.pop();
92+
cout<<" ---- sucessful ----";
93+
cout<<"popped element is= "<<element<<endl;
94+
break;
95+
case 3:
96+
s.display();
97+
break;
98+
case 4:
99+
exit(0);
100+
default:
101+
cout<<"wrong choice\n";
102+
break;
103+
}
104+
}
105+
106+
getch();
107+
return 0;
108+
109+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
**Step 1:** [Watch Implementing Stack using LL by Dinesh Varyani.](https://youtu.be/Us4N22SEbM0)
2+
3+
**Step 2:** [See my implemented code in Java.](https://github.com/thepranaygupta/Data-Structures-and-Algorithms/blob/main/02.%20Stack/Basic%20Operation/2.%20Stack%20using%20LinkList/Stack_LinkList.java)

0 commit comments

Comments
 (0)