本文为转载--原文链接:https://blog.csdn.net/qq_38605328/article/details/101081328
目录
一、自定义类实现链表
1.定义节点的数据类型
-
public
class NodeClass<T> {
-
-
private T Date;
//数据
-
private NodeClass<T> Next;
//指针
-
-
public T getDate() {
-
return Date;
-
}
-
public void setDate(T date) {
-
Date = date;
-
}
-
public NodeClass<T> getNext() {
-
return Next;
-
}
-
public void setNext(NodeClass<T> next) {
-
Next = next;
-
}
-
-
@Override
-
public String toString()
-
{
-
return
"NodeClass{" +
-
"data=" + Date +
-
", next=" + Next +
-
'}';
-
}
-
}
2.定义一个学生类
-
public
class Student {
-
-
private String stuName;
// 学生姓名
-
private String stuNum;
// 学号
-
private String stuInfo;
// 学生信息
-
-
public String getStuName() {
-
return StuName;
-
}
-
public void setStuName(String stuName) {
-
this.stuName = stuName;
-
}
-
public String getStuNum() {
-
return stuNum;
-
}
-
public void setStuNum(String stuNum) {
-
this.stuNum = stuNum;
-
}
-
public String getStuInfo() {
-
return stuInfo;
-
}
-
public void setStuInfo(String stuInfo) {
-
this.stuInfo = stuInfo;
-
}
-
-
@Override
-
public String toString() {
-
return
"Student{" +
-
"stuNum=" + stuNum +
-
", name='" + stuName +
'\'' +
-
", otherInfo='" + stuInfo +
'\'' +
-
'}';
-
}
-
-
}
3.测试类
-
public
class
StudentLink {
-
-
public static void main(String[] args) {
-
String[] student =
new String[] {
"阿里巴巴",
"腾讯",
"百度",
"字节跳动"};
-
NodeClass<Student> head =
new NodeClass<Student>();
-
NodeClass<Student> flag = head;
-
for(
int i =
0; i < student.length; i++) {
-
Student stu =
new Student();
-
flag.setDate(stu);
-
stu.setStuName(student[i]);
-
stu.setStuNum(String.valueOf(i));
-
stu.setStuInfo(
"this is " + student[i]);
-
if( i < student.length
-1) {
-
flag.setNext(
new NodeClass<Student>());
-
flag = flag.getNext();
-
}
-
}
-
System.
out.println(head);
-
}
-
}
4.输出
NodeClass{data=Student{stuNum=0, name='阿里巴巴', otherInfo='this is 阿里巴巴'}, next=NodeClass{data=Student{stuNum=1, name='腾讯', otherInfo='this is 腾讯'}, next=NodeClass{data=Student{stuNum=2, name='百度', otherInfo='this is 百度'}, next=NodeClass{data=Student{stuNum=3, name='字节跳动', otherInfo='this is 字节跳动'}, next=null}}}}
5.手绘:
二、自定义类实现栈
1.定义栈类
-
public
class StackClass<T> {
-
-
//栈顶元素
-
private NodeClass<T> head;
-
-
public NodeClass<T> gethead() {
-
return head;
-
}
-
-
public void sethead(NodeClass<T> head) {
-
this.head = head;
-
}
-
-
/**
-
* <p>Description:入栈 </p>
-
* @return
-
*/
-
public void push(NodeClass<T> push){
-
if( head ==
null ){
-
head = push;
-
}
else{
-
push.setNext(head);
-
head = push;
-
}
-
}
-
-
/**
-
* <p>Description:出栈 </p>
-
* @return 栈顶元素
-
*/
-
public NodeClass<T> pop(){
-
NodeClass<T> result = head;
-
if( head !=
null ){
-
head = head.getNext();
-
}
-
return result;
-
}
-
-
public void read(){
-
System.out.println(head.toString());
-
}
-
}
2.测试类
-
public static void main(String[] args) {
-
//入栈出栈
-
String[] studentNames =
new String[] {
-
"百度",
"阿里巴巴",
"腾讯",
"字节跳动",
"美团",
"滴滴",
"网易",
-
"58同城",
"携程",
"牛客网"
-
};
-
StackClass<Student> stack =
new StackClass<Student>();
-
-
for(
int i =
0; i < studentNames.length; i++) {
-
NodeClass<Student> flag1 =
new NodeClass<Student>();
-
Student stu =
new Student();
-
flag1.setData(stu);
-
stu.setStuNum(String.valueOf(i));
-
stu.setStuName(studentNames[i]);
-
stu.setStuInfo(
"I am the best!");
-
stack.push(flag1);
-
if(i ==
3 || i ==
6) {
-
NodeClass<Student> result = stack.pop();
-
if(result ==
null) {
-
System.
out.println(
"空了");
-
}
else {
-
System.
out.println(result.getData().getStuName());
-
}
-
}
-
}
-
while(
true) {
-
NodeClass<Student> result = stack.pop();
-
if(result ==
null) {
-
System.
out.println(
"空了");
-
break;
-
}
else {
-
System.
out.println(result.getData().getStuName());
-
}
-
}
-
}
3.输出:
腾讯 滴滴 牛客网 携程 58同城 网易 美团 字节跳动 阿里巴巴 百度 空了
三、自定义类实现队列
1.定义队列类
-
public
class QueClass<T> {
-
-
private NodeClass<T> start;
//队首
-
private NodeClass<T> end;
//队尾
-
-
public NodeClass<T> getStart() {
-
return start;
-
}
-
-
public void setStart(NodeClass<T> start) {
-
this.start = start;
-
}
-
-
public NodeClass<T> getEnd() {
-
return end;
-
}
-
-
public void setEnd(NodeClass<T> end) {
-
this.end = end;
-
}
-
-
public void push( NodeClass<T> add ){
-
if( start ==
null ){
-
start = add;
-
end = start;
-
}
else{
-
end.setNext(add);
-
end = end.getNext();
-
}
-
}
-
-
public NodeClass<T> pop(){
-
NodeClass<T> result = start;
-
if( start !=
null ){
-
start = start.getNext();
-
}
-
return result;
-
}
-
-
public void read(){
-
System.out.println(start.toString());
-
}
-
-
-
}
2.测试类
-
-
public static void main(String[] args) {
-
//入队出队
-
String[] studentNames =
new String[] {
-
"百度",
"阿里巴巴",
"腾讯",
"字节跳动",
"美团",
"滴滴",
"网易",
-
"58同城",
"携程",
"牛客网"
-
};
-
QueClass<Student> que =
new QueClass<Student>();
-
-
for(
int i =
0; i < studentNames.length; i++) {
-
NodeClass<Student> flag1 =
new NodeClass<Student>();
-
Student stu =
new Student();
-
flag1.setData(stu);
-
stu.setStuNum(String.valueOf(i));
-
stu.setStuName(studentNames[i]);
-
stu.setStuInfo(
"I am the best!");
-
que.push(flag1);
-
-
}
-
while(
true) {
-
NodeClass<Student> result = que.pop();
-
if(result ==
null) {
-
System.
out.println(
"空了");
-
break;
-
}
else {
-
System.
out.println(result.getData().getStuName());
-
}
-
}
-
}
-
3.输出
百度 阿里巴巴 腾讯 字节跳动 美团 滴滴 网易 58同城 携程 牛客网 空了
4.手绘: