0% found this document useful (0 votes)
2 views3 pages

S

The document contains Java code for a simple stack implementation using a generic class. It includes three classes: Student, Stack, and Test, where Student represents a student with attributes like ID, name, and marks, Stack manages a collection of Student objects, and Test demonstrates the stack operations. The stack allows pushing, popping, peeking, and displaying student objects.

Uploaded by

ASK 011
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

S

The document contains Java code for a simple stack implementation using a generic class. It includes three classes: Student, Stack, and Test, where Student represents a student with attributes like ID, name, and marks, Stack manages a collection of Student objects, and Test demonstrates the stack operations. The stack allows pushing, popping, peeking, and displaying student objects.

Uploaded by

ASK 011
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

package Stack;

1 Student.java
public class Student {

private int student_id;


private String sname;
private int marks;
public Student(int student_id, String sname, int marks) {
super();
this.student_id = student_id;
this.sname = sname;
this.marks = marks;
}
@Override
public String toString() {
return "Student [student_id=" + student_id + ", sname=" + sname + ",
marks=" + marks + "]";
}

public int getStudent_id() {


return student_id;
}
public void setStudent_id(int student_id) {
this.student_id = student_id;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public int getMarks() {
return marks;
}
public void setMarks(int marks) {
this.marks = marks;
}
}

2.Test.java
package Stack;
public class Test {
public static void main(String[] args) {

Stack<Student> cst=new Stack<Student>(5);

cst.push(new Student(1, "kedar",14));


cst.push(new Student(1, "harsh",14));
cst.push(new Student(1, "xyz",14));
cst.push(new Student(1, "abc",14));
System.out.println("----------pop-----------");
System.out.println( cst.pop());
System.out.println("------------peek-------------");
System.out.println(cst.peek());
System.out.println("--------display-----------");
cst.display();
}
}

3. Stack.java
package Stack;
public class Stack<T> {
private Student[] sarr;
private int size;

private int top=-1;

public Stack(int size)


{
this.size=size;
this.sarr=new Student[size];
}

public boolean isEmpty()


{
return top == -1;
}

public boolean isFull()


{
return top==size-1;
}

public boolean push(Student s)


{
if(isFull())
{
return false;
}

sarr[++top]=s;
return true;
}

public Student pop()


{
if(isEmpty())
{
return null;
}

return sarr[top--];
}

public Student peek()


{
if(isEmpty())
{
return null;
}
return sarr[top];
}

public void display()


{
for(int i=0;i<=top;i++)
{
System.out.println(sarr[i]);
}
}

You might also like