this lectures are saving me, tnx! by the way, cant isEmpty be like this public boolean isEmpty() { return top == 0; } or public boolean isEmpty() { return zise() == 0; } why the < symbol ? even tho it works still
The top has to be top=top-1 before performing seek or pop?? because if we are inserting any value with that value the top is also increased. plz tell????
import java.lang.reflect.Array; public class GenericStack{ private T[] array; private int top=0;
public GenericStack(Class clazz, int capacity){ array = (T[])Array.newInstance(clazz,capacity); }
public void show(){ for(T element:array){ System.out.print(element+" "); } System.out.println(); }
public void push(T data){ if(top==array.length){ System.out.println("stack is full !!!"); return; }
array[top] = data; top++; show(); }
public T peek(){ if(isEmpty()){ System.out.println("Stack is empty !!!"); return null; } System.out.println("last element:->"+array[top-1]); return array[top-1]; }
public T pop(){ if(isEmpty()){ System.out.println("Stack is empty !!!"); return null; } top--; T data = array[top]; array[top]=null; show(); return data; }
public boolean isEmpty(){ System.out.println("Empty:-->>"+(top
In Java, int fields(class level variables) values are automatically initialized to 0 by default. Therefore reverting the value in the index back to 0 is setting it to its default value. Int is a primitive data type and cannot be null.
@@richardmaduka4747 Yes but Aswin has a point. if 0 is one of the actual integer values stored in the stack then how would you differentiate ? A stack is only delimited by the index of the top, not by the values in the array (whether they are 0 or not). Even if you stored values other than zero, someone will know where the stack starts and stops in the array as long we maintain a pointer to the top. If you know the top, then you know where your stack stops, it does not matter if the indices out of the stack contain zero or non zero elements. Putting 0 is actually misunderstanding what stacks are.
Hi sir, I can't able to understand your "InterThread Communication" program on multithreading concept. You can find that video on your java playlist. Can you please make a video to explain that program step by step.???
sir if the stack is full and still I want to add more values in above case, after getting stack is full message instead of it I want to add more elements, so how we can do this?-for this problem we have to create one more array but how we can maintain previously added values.?
Instead of using an array where you can't add when the stack is full, use a dynamic array, aka ArrayList (I think #12 in the playlist shows implementation of stacks using ArrayList)
@Telusko thank you so much for your contribution.
Really sir I like ur videos...coz ur teaching method is best..keep it up and make more videos for us..
Finally I found my online programming tutor
should've initialized Top =-1;
Sir please make video on....
What is prerequisites for making an application........
Thanks for this wonderful video. May gbu with lots of success love and life. Keep Rocking
Bhai DSA Pura kardo please..ALL VIDEOS ARE GREAT
Please make a video on algorithms and data structure using Java sir!
Loved it. From USA
this lectures are saving me, tnx!
by the way, cant isEmpty be like this
public boolean isEmpty() {
return top == 0;
}
or
public boolean isEmpty() {
return zise() == 0;
}
why the < symbol ? even tho it works still
Amazing idea and explanation again thanks
The top has to be top=top-1 before performing seek or pop??
because if we are inserting any value with that value the top is also increased.
plz tell????
import java.lang.reflect.Array;
public class GenericStack{
private T[] array;
private int top=0;
public GenericStack(Class clazz, int capacity){
array = (T[])Array.newInstance(clazz,capacity);
}
public void show(){
for(T element:array){
System.out.print(element+" ");
}
System.out.println();
}
public void push(T data){
if(top==array.length){
System.out.println("stack is full !!!");
return;
}
array[top] = data;
top++;
show();
}
public T peek(){
if(isEmpty()){
System.out.println("Stack is empty !!!");
return null;
}
System.out.println("last element:->"+array[top-1]);
return array[top-1];
}
public T pop(){
if(isEmpty()){
System.out.println("Stack is empty !!!");
return null;
}
top--;
T data = array[top];
array[top]=null;
show();
return data;
}
public boolean isEmpty(){
System.out.println("Empty:-->>"+(top
why add 0 while pop ? adding 0 is not deletion and 0 is also a value. Not convinced.
In Java, int fields(class level variables) values are automatically initialized to 0 by default. Therefore reverting the value in the index back to 0 is setting it to its default value. Int is a primitive data type and cannot be null.
@@richardmaduka4747 Yes but Aswin has a point. if 0 is one of the actual integer values stored in the stack then how would you differentiate ?
A stack is only delimited by the index of the top, not by the values in the array (whether they are 0 or not). Even if you stored values other than zero, someone will know where the stack starts and stops in the array as long we maintain a pointer to the top. If you know the top, then you know where your stack stops, it does not matter if the indices out of the stack contain zero or non zero elements. Putting 0 is actually misunderstanding what stacks are.
adding 0 will probably be because he used a primitive type int, if a generic type or an object were used, then null would have been a good option.
Hi sir, I can't able to understand your "InterThread Communication" program on multithreading concept. You can find that video on your java playlist. Can you please make a video to explain that program step by step.???
Thank you so much sir! 😊
woooooow really thank you for helping us, your way to writing the code is really sexy and its amazing.
thank you brother
best teacher
sir if the stack is full and still I want to add more values in above case, after getting stack is full message instead of it I want to add more elements, so how we can do this?-for this problem we have to create one more array but how we can maintain previously added values.?
Instead of using an array where you can't add when the stack is full, use a dynamic array, aka ArrayList (I think #12 in the playlist shows implementation of stacks using ArrayList)
You can also implement a stack data structure using a LinkedList if you are worried about array size. Else you use a dynamic array or ArrayList
Baaga Telusukuna Sir :P
can we get the code ?
sir..in size(), top variable was supposed to be decremented by one, as in push() it was incremented by one each time push() called....isn't that???
size is starting from 1 rather than 0. thats why he left it as such