当前位置: 首页> 娱乐> 影视 > 企业老总电话名录_移动app开发外包公司_优帮云排名自动扣费_关键词搜索挖掘爱网站

企业老总电话名录_移动app开发外包公司_优帮云排名自动扣费_关键词搜索挖掘爱网站

时间:2025/7/13 8:19:26来源:https://blog.csdn.net/2301_78566776/article/details/144003703 浏览次数:0次
企业老总电话名录_移动app开发外包公司_优帮云排名自动扣费_关键词搜索挖掘爱网站

目录

用链表实现栈和队列

一、链表实现栈

1.ListNodeStack类

2.测试结果:

一、链表实现队列

1.ListNodeQueue类

2.测试结果:


用链表实现栈和队列

首先我们需要定义一个ListNode节点的泛型类,其中 T 是一个类型参数,意味着这个节点可以存储任何类型的数据。

package 数据结构;public class ListNode<T> {T data;ListNode<T> next;
}
一、链表实现栈
1.ListNodeStack<T>类
package 数据结构;
/*** 用链表实现一个栈结构*/
public class ListNodeStack<T> {//定义一个头节点private ListNode<T> pHead;public ListNodeStack(ListNode<T> pHead) {this.pHead = pHead;}//链表初始化public ListNodeStack() {pHead = new ListNode<T>();pHead.data = null;pHead.next = null;}//判断栈是否为空boolean isEmpty(){return pHead == null;}//入栈public void add(T e){ListNode<T> p = new ListNode<>();p.data = e;p.next = pHead.next;pHead.next = p;System.out.println(e+"push in stack");}//出栈public T pop(){ListNode<T> tmp = pHead.next;if(tmp != null){pHead.next = tmp.next;return tmp.data;}System.out.println("stack empty!");return null;}public static void main(String[] args) {ListNodeStack<Integer> stack = new ListNodeStack<Integer>();stack.add(1);stack.add(2);stack.add(3);System.out.println(stack.pop()+"out of stack");System.out.println(stack.pop()+"out of stack");System.out.println(stack.pop()+"out of stack");System.out.println(stack.pop()+" out of stack");}
}
2.测试结果:

一、链表实现队列
1.ListNodeQueue<T>类
package 数据结构;/*** 链表实现队列*/
public class ListNodeQueue<T> {//队列首元素private ListNode<T> pHead;//队列尾元素private ListNode<T> pEnd;//分配头结点public ListNodeQueue(){pEnd = pHead = null;}//判断队列是否为空boolean isEmpty(){if(pHead == null){return true;}else {return false;}}//获取栈中元素个数int size(){int size = 0;ListNode<T> p = pHead;while(p != null){p = p.next;size++;}return size;}//入队列void inQueue(T e){ListNode<T> p = new ListNode<>();p.data = e;p.next = null;if(pHead == null){pHead = pEnd = p;}else {pEnd.next = p;pEnd = p;}}//出队列public void outQueue(){if(pHead == null){System.out.println("queue empty!");return;}System.out.println(pHead.data+" out of queue");pHead = pHead.next;if(pHead == null){pEnd = null;}}public static void main(String[] args) {ListNodeQueue<Integer> queue = new ListNodeQueue<>();queue.inQueue(1);queue.inQueue(2);queue.inQueue(3);queue.inQueue(4);queue.outQueue();queue.outQueue();queue.outQueue();queue.outQueue();queue.outQueue();}
}
2.测试结果:

关键字:企业老总电话名录_移动app开发外包公司_优帮云排名自动扣费_关键词搜索挖掘爱网站

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: