السلام عليكم
كل عام و انتم بخير
أريد ان ابرمج برنامج بفكرة stack
الذي اعرفه عن فكرة stack أن اخر من يضاف هو اول من يخرج
يعني نريد أن نضيف بترتب
1-2-3-4-5-6-
فإن اول من يحذف هو 6 لأن آخر ما تمت اضافته
إذا كان هذا خطأ فأتمنى الشرح لي ...
طبعا أنا كتبت برنامج
package Stack;
public class Node {
private School data;
private Node next;
public Node(){
data=null;
next=null;
}
public Node(School sc){
data=sc;
next=null;
}
public School getData() {
return data;
}
public void setData(School data) {
this.data = data;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
}package Stack;
public class School {
private String name;
private double id;
private int age;
public School()
{
this.name=null;
this.id=0.0;
this.age=0;
}
public School(String nam,double id,int age)
{
this.name=nam;
this.id=id;
this.age=age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getId() {
return id;
}
public void setId(double id) {
this.id = id;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void display(){
System.out.println("name :"+name+"\n"+"id"+id+"\n"+"age :"+age);
}
}package Stack;
public class LinkStack {
private Node top;
public LinkStack(){
top=null;
}
public boolean empty()
{
return top==null;
}
public boolean full()
{
return false;
}
public void push(School S){
Node N=new Node(S);
N.setNext(N);
top=N;
}
public School pop(){
School e=top.getData();
top=top.getNext();
return e;
}
public void display()
{
Node c = top;
c.getData().display();
}
}package Stack;
public class StackMain {
public static void main(String[]args)
{
School S=new School();
School S1=new School();
School S2=new School();
LinkStack L=new LinkStack();
S.setName("A");
S.setId(123456);
S.setAge(14);
L.push(S);
L.display();
System.out.println("--------");
S1.setName("B");
S1.setId(5677);
S1.setAge(17);
L.push(S1);
L.display();
System.out.println("--------");
S2.setName("C");
S2.setId(8888);
S2.setAge(90);
L.push(S2);
L.display();
System.out.println("------333----------------");
L.pop();
L.display();
}
}المخرجات
name :A id123456.0 age :14 -------- name :B id5677.0 age :17 -------- name :C id8888.0 age :90 ------333---------------- اعتقد ان الخطاء يبدأ من هنا name :C id8888.0 age :90
أنا اتوقع ان المفروض يكون :
\\ يـحذف c مع id , age و يطبع معلومات b و a . بناء على فكرة stack و إلا أنا غلطان !؟