栈的两种表示方法
public class MyStack {
public int[] arrays = new int[100];
public int userSize = 0;
public MyStack() {
}
public MyStack(int[] arrays) {
this.arrays = arrays;
}
//进栈
public void push(int data) {
this.arrays[this.userSize] = data;
this.userSize++;
}
public Integer pop() {
if(this.userSize <= 0) {
return null;
}
int ret = this.arrays[this.userSize - 1];
this.userSize--;
return ret;
}
public Integer peek() {
if(this.userSize <= 0) {
return null;
}
int ret = arrays[this.userSize - 1];
return ret;
}
public boolean empty() {
if(this.userSize == 0) {
return true;
}
return false;
}
public static void main(String[] args) {
MyStack stack = new MyStack();
stack.push(1);
stack.push(2);
stack.push(3);
stack.push(4);
while(true) {
Integer cur = stack.pop() ;
if(cur == null) {
break;
}
System.out.println(cur);
}
}
}
public class MyStackTwo {
static class Node{
public int val;
public Node next;
public Node(int val) {
this.val = val;
}
public Node() {
}
}
Node head = new Node();
public void push(int data) {
Node cur = new Node(data);
if(this.head == null) {
this.head = cur;
return;
}
cur.next = this.head;
this.head = cur;
}
public Integer pop() {
Node node = new Node();
node = this.head;
if (node == null) {
return null;
}
this.head = this.head.next;
return node.val;
}
public Integer peek() {
Node node = new Node();
if (this.head == null) {
return null;
}
node = this.head;
return node.val;
}
public boolean empty() {
if(this.head == null) {
return true;
}
return false;
}
public static void main(String[] args) {
MyStackTwo stackTwo = new MyStackTwo();
stackTwo.push(1);
stackTwo.push(2);
stackTwo.push(3);
stackTwo.push(4);
stackTwo.push(5);
while(true) {
Integer cur = stackTwo.pop();
if(cur == null){
break;
}
System.out.println(cur);
}
}
}
队列的两种表达方式
public class MyQueue {
static class Node {
public int val;
public Node next;
public Node(int val) {
this.val = val;
}
public Node() {
}
}
Node head = new Node(-1);
Node tail = head;
public void offer(int val) {
Node node = new Node(val);
this.tail.next = node;
tail = tail.next;
}
public Integer poll() {
if(this.head.next == null) {
return null;
}
Node node = new Node();
node = this.head.next;
this.head.next = node.next;
if(this.head.next == null) {
this.tail = this.head;
}
return node.val;
}
public Integer peek() {
if(this.head.next == null) {
return null;
}
return this.head.next.val;
}
public static void main(String[] args) {
MyQueue queue = new MyQueue();
queue.offer(1);
queue.offer(2);
queue.offer(3);
queue.offer(4);
while(true) {
Integer cur = queue.poll();
if(cur == null) {
break;
}
System.out.println(cur);
}
}
}
public class MyQueueTwo {
int[] arrays = new int[100];
int userSize = 0;
int head = 0;
int tail = 0;
public boolean offer(int data) {
if(this.userSize == this.arrays.length) {
return false;
}
this.arrays[this.tail] = data;
this.tail++;
this.tail = this.tail % this.arrays.length;
this.userSize++;
return true;
}
public Integer poll() {
if (this.userSize == 0) {
return null;
}
Integer num = this.arrays[this.head];
this.head++;
this.head = this.head % this.arrays.length;
this.userSize--;
return num;
}
public Integer peek() {
if (this.userSize == 0) {
return null;
}
return this.arrays[this.head];
}
public boolean empty() {
if(this.userSize == 0) {
return true;
}
return false;
}
public static void main(String[] args) {
MyQueueTwo queueTwo = new MyQueueTwo();
queueTwo.offer(1);
queueTwo.offer(2);
queueTwo.offer(3);
queueTwo.offer(4);
queueTwo.offer(5);
while (true) {
Integer num = queueTwo.poll();
if(num == null) {
break;
}
System.out.println(num);
}
}
}
本文介绍了两种数据结构——栈和队列的实现方法。通过使用数组和链表,展示了栈的进栈、出栈、查看栈顶元素及判断是否为空的操作;并通过数组和链表实现了队列的入队、出队、查看队头元素及判断是否为空的功能。

被折叠的 条评论
为什么被折叠?



