Город МОСКОВСКИЙ
00:07:40

Stack data structure using java

Аватар
Code Tips
Просмотры:
101
Дата загрузки:
05.12.2023 02:36
Длительность:
00:07:40
Категория:
Обучение

Описание

/*Programming Sucks*/
//Stack implementation

class stack{
int top;
int arr[];
int size;
stack(int size){
top = -1;
arr = new int[size];
this.size = size;
}

public void push(int value){
if(top(Greater than sign)=size-1){
System.out.println("The stack is full");
}
else{
arr[++top] = value;
System.out.println("Pushed :- " + arr[top]);
}
}

public void pop(){
if(top==-1){
System.out.println("The stack is empty");
}
else{
System.out.println("Popped value is :- " + arr[top--]);
}
}

public void print(){
int i = 0;
System.out.println("Following is the stack :- ");
while(i(less than sign)=top){
System.out.println(arr[i++]);
}
}

}

public class Main
{
public static void main(String[] args) {
int size = 5; //you can set it as per your wish
stack obj = new stack(size);
for(int i=0;i(less than sign)size;i++){
obj.push(i);
}
obj.pop();
obj.print();
obj.pop();
obj.print();

}
}

Рекомендуемые видео