/**
* 线性表
* @author wu_pc
*
*/
public class Xianxingbiao {
// 初始化一个数组
private Object[] biao = new Object[5];
// 定义长度
private int length = 0;
// 线性表添加方法
public void add(Object data) {
biao[length] = data;
length++;
return;
}
// 删除方法,当需要删除元素时,将数组后一位元素赋值到需要删除元素的位置
// 进行删除操作后数组长度减一
// 如果删除的是2,相当于从1,2,3,4中拿出2, 3和4各往前移动一位
public void delete(Object data) {
for (int j = 0; j < this.length; j++) {
if (this.biao[j] == data) {
if (j != this.length - 1) {
for (int o = 1; o < this.length - j; o++)
this.biao[j] = this.biao[j + o];
}
this.length--;
}
}
}
// 简单的遍历数组
public void foreach(Xianxingbiao a) {
for (int j = 0; j < a.length; j++) {
System.out.println(a.biao[j]);
}
}
}
/**
* 定义一个节点
* 头存数据
* 尾存下一个节点的地址信息
* @author wu_pc
*
*/
public class Node {
public int data;
public Node next;
public Node(int data) {
this.data = data;
}
}
/**
* 单链表
* @author wu_pc
*
*/
public class Lianbiao {
// 定义头节点
private Node head;
public Lianbiao(Node head) {
this.head = head;
}
public Lianbiao() {
}
//添加节点方法
public void add(int data) {
Node newNode = new Node(data);
// 如果第这是第一个节点
if (head == null) {
head = newNode;
return;
}
Node tmp = head;
//找到当前列表的最后一个节点
while (tmp.next != null) {
tmp = tmp.next;
}
//把添加的节点放到链表的最后
tmp.next = newNode;
}
//删除节点方法
public boolean delete(int data) {
//如果删除的是头节点
if (data == head.data) {
this.head = head.next;
return true;
}
//找到链表的最后一个节点和倒数第二个节点
Node c = head.next;
Node b = head;
while (data != c.data) {
c = c.next;
b = b.next;
}
// 当发现C就是要删除的元素时,将c前一个节点存储的节点,由c换成c里存储的节点
b.next = c.next;
return true;
}
}
/**
* 定义一个节点
* 头存数据
* 尾存下一个节点的地址信息
* @author wu_pc
*
*/
public class Node {
public int data;
public Node next;
public Node(int data) {
this.data = data;
}
}
/**
* 循环链表
* @author wu_pc
*
*/
public class xhlianbiao {
// 定义头节点
public Node head;
public xhlianbiao() {
}
public xhlianbiao(Node head) {
this.head = head;
head.next = head;
}
//循环链表添加方法
public void add(int data) {
Node newNode = new Node(data);
// 如果链表为空,就是没有头节点
if (head == null) {
head = newNode;
head.next = head;
return;
}
//找到链表的最后一个节点
Node tmp = head;
while (tmp.next != head) {
tmp = tmp.next;
}
//链表末尾节点的下一位为新插入的节点
tmp.next = newNode;
//新的链表末尾节点指向头节点,实现循环链表
newNode.next = head;
}
// 循环链表删除方法
public boolean delete(int data) {
//如果链表为空
if (this.head == null) {
System.out.println("链表已经没有节点");
return true;
}
//如果删除的是头节点
if (data == head.data) {
if(head.next == head) {
this.head = null;
}
this.head = head.next;
return true;
}
//找到链表的最后一个节点和倒数第二个节点
Node b = head;
Node c = head.next;
while (data != c.data) {
b = b.next;
c = c.next;
}
//如果要删除的正好是链表的最后一个节点
if (c.next == head) {
b.next = head;
} else {
当发现C就是要删除的元素时,将c前一个节点b存储的节点,由c 换成c的下一个节点
b.next = c.next;
}
return true;
}
}
/**
* 队列
* @author wu_pc
*
*/
public class Queue {
//定义存储数据的数组
int max = 10;
int[] data = new int[max];
//定义头指针,尾指针
int front = 0;
int rear = 0;
//定义插入操作
public void En(int a) {
//如果尾指针在头指针前一位加一,队列已满
if ((this.rear + 1) % max == this.front) {
System.out.println("队列已满");
return;
}
//插入数据,移动尾指针
this.data[rear] = a;
this.rear = (this.rear + 1) % max;
return;
}
//定义删除操作
public void De() {
//如果头尾指针在一个位置,空队列
if(this.front == this.rear) {
System.out.println("队列为空");
}
//头指针后移一位,队列先进先出的特性
this.front = (this.front + 1) % max;
return;
}
}
/**
* 定义一个节点
* 头存数据
* 尾存下一个节点的地址信息
* @author wu_pc
*
*/
public class Node {
public int data;
public Node next;
public Node(int data) {
this.data = data;
}
public Node() {
}
}
/**
* 链式队列
* @author wu_pc
*
*/
public class LinkedQueue {
//定义头尾节点
Node rear = new Node();
Node front = new Node();
//定义队列长度
int count = 0;
//定义进入队列操作
public void En(Node a) {
//如果是空栈
if (count == 0) {
front.next = a;
}
//移动尾指针
rear.next = a;
rear = a;
count++;
}
//定义出队列操作
public void De() {
//移动头指针
Node p = null;
p = front.next;
front.next = p.next;
count--;
}
//测试用
public void syso() {
for (int i = 0; i < count ; i++) {
System.out.println(front.next.data);
front.next = front.next.next;
}
}
}
package demo5;
/**
* 栈
* @author wu_pc
*
*/
public class Zhan {
//初始化存储数据的数组
int max = 10;
int[] data = new int[max];
//定义栈顶指针
int top = 0;
//定义入栈操作
public void push(int a, Zhan b) {
//判断栈是否已满
if (b.top == max - 1) {
System.out.println("已经到达最大容量");
return;
}
//将数据存到数组中
b.data[top] = a;
top++;
}
//定义出栈操作
public void pop(Zhan b) {
//判断栈是否为空,栈顶指针指向栈底
if(b.top == 0) {
System.out.println("数组里面已经没有元素了");
return;
}
//删除数组内的元素
b.top--;
}
//测试用
public void syso() {
for (int i = 0; i < top; i++) {
System.out.println(data[i]);
}
}
}
/**
* 定义一个节点
* 头存数据
* 尾存下一个节点的地址信息
* @author wu_pc
*
*/
public class LinkedStack {
//定义节点
Node top = null;
//定义栈的长度
int count = 0;
//定义入栈操作
public void push(Node a) {
//判断是否为空栈
if (count == 0) {
top = a;
} else {
a.next = top;
top = a;
}
//长度加一
count++;
}
//定义出栈操作
public void pop() {
//判断是否为空栈
if (count == 0) {
System.out.println("没有节点可以弹出");
}
else {
//删除
top=top.next;
//长度减一
count--;
}
}
//测试用
public void me() {
Node a = top ;
for( int i = 0 ; i < count ; i++ ) {
System.out.println(a.data);
a=a.next;
}
}
}
如履薄冰,诚惶诚恐