数据结构基础1
Java实现一个栈
栈是一种简单的数据结构,我们都知道栈的特点是后进先出(LIFO),这里我要用不同的方式去实现一个顺序栈。
用数组实现一个简单的顺序栈
存放数据时从前往后存放,拿出数据时便是从后往前取出。依照这种思路很容易写出push和pop操作。
//用数组实现一个栈
public class Stack {
//栈的大小
int size;
//栈顶指针
int top;
//栈的大小
int[] data;
/**
*构造方法初始化栈大小
* @param size
*/
public Stack(int size) {
this.size = size;
data = new int[size];
top = -1;
}
/**
* 获取栈的大小
* @return
*/
public int getSize() {
return size;
}
/**
* 获取栈顶的索引
* @return
*/
public int getTop() {
return top;
}
/**
* 判断是否为空栈
* 方法是栈顶指针是否为-1
* @return
*/
public boolean isEmpty() {
return top == -1;
}
/**
* 判断是否为满栈
* 方法是栈顶指针是与最大索引相同
* @return
*/
public boolean isFull() {
return (top+1) == size;
}
/**
* 压栈操作
* @param d
* @return
*/
public boolean push(int d) {
if(isFull()){
System.out.println("栈已满,无法加入新元素");
return false;
}else{
top++;
this.data[top] = d;
return true;
}
}
/**
* 弹出栈操作
* @return
* @throws Exception
*/
public int pop() throws Exception {
if(isEmpty()){
throw new Exception("空栈没有数据可以弹出");
}else{
return data[top--];
}
}
/**
* 获取栈顶元素
* @return
*/
public int peek() {
return this.data[getTop()];
}
public static void main(String[] args){
Stack stack = new Stack(20);
stack.push(0);
stack.push(1);
stack.push(2);
stack.push(3);
System.out.println("Now the top_num is:" + stack.peek());
while(! stack.isEmpty()) {
try {
System.out.println(stack.pop());
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
用连表实现一个链式栈
用链表实现一个栈时用头部拔插法,只在头部进行操作。
public class MyStack2 {
Node top;
//链表实现栈用头插法
//每次往头部插入节点
//构造函数,初始化栈
public MyStack2(){
top = new Node();
top.next = null;
top.value = -1;
}
/**
* 采用头部拔插的方式加入节点
* @param input
*/
public void push(int input){
Node e = new Node(input);
if(top.next == null){
top.next = e;
}else {
e.next = top.next;
top.next = e;
}
}
public int pop(){
int out = -1;
if(top.next == null){
System.out.println("栈为空");
}else {
out = top.next.value;
top.next = top.next.next;
}
return out;
}
public int peek(){
int out = -1;
if(top.next == null){
System.out.println("栈为空");
}else {
out = top.next.value;
}
return out;
}
public boolean isEmpty(){
if(top.next == null){
return true;
}else {
return false;
}
}
}
编程模拟一个网页进退
如何实现浏览器的前进、后退功能?其实,用两个栈就可以非常完美地解决这个问题。
我们使用两个栈,X 和 Y,我们把首次浏览的页面依次压入栈 X,当点击后退按钮时,再依次从栈 X 中出栈,并将出栈的数据依次放入栈 Y。当我们点击前进按钮时,我们依次从栈 Y 中取出数据,放入栈 X 中。当栈 X 中没有数据时,那就说明没有页面可以继续后退浏览了。当栈 Y 中没有数据,那就说明没有页面可以点击前进按钮浏览了。
- 依次查看了A、B、C三个页面
Stack<string> x = new Stack<string>();
Stack<string> y = new Stack<string>();
//浏览A
x.push("A");
//浏览B
x.push("B");
//浏览C
x.push("C");
- 后退查看B再后退到A页面进行查询,同时需要将C和B页面插入到Y中;
//后退C 这时候C页面放入y栈中,同时移除掉x中的C
y.push("C");
x.pop();
//后退B 这时候B页面放入y栈中
y.push("B");
x.pop();
- 当再B页面查看D页面的时候,C页面就不能通过前进后退的功能查看了,所以需要清空Y;
//然后前进到B页面
x.push("B");
y.pop();
//此时x中只有AB 两个页面,y中C两个页面
//打开新的页面D
x.push("D");
//页面 c 就无法再通过前进、后退按钮重复查看了,所以需要清空
y.pop();
Java 实现一个队列
数组实现一个队列
与之前实现栈类似,只不过要循环引用其head和tail指针。
public class MyList1 {
private int[] queue;//数组队列
private int head;//头指针
private int tail;//尾指针
private int count;//队列所含元素
public MyList1() {//初始化队列无参默认为10
queue = new int[10];
head = 0;
tail = 0;
count = 0;
}
public MyList1(int size) {//初始化队列
queue = new int[size];
head = 0;
tail = 0;
count = 0;
}
//入队操作
public boolean offer(int input) {
if (count == queue.length) {
System.out.println("The queue is full");
return false;
} else {
queue[tail % queue.length] = input;
tail++;
count++;
return true;
}
}
public int poll() {
if (count == 0) {
return -1;
} else {
count--;
return queue[head--];
}
}
public int peek() {
if (count == 0) {
return -1;
} else {
return queue[head];
}
}
}
链表实现一个队列
用两个指针指向头尾两个部位。
进队时从尾部进入,出队时从头部进入。
public class MyQueue2 {
Node head;
Node tail;
public MyQueue2() {
Node head = new Node();
Node tail = head;
head.next = null;
head.value = -1;
}
public boolean offer(int input){
Node e = new Node(input);
if(tail == null){
head = tail = e;
}else {
tail.next = e;
tail = tail.next;
}
return true;
}
public int poll(){
if(tail == head){
int out = head.value;
head = tail =null;
return out;
}else if (head == null){
System.out.println("Empty stack");
return -1;
}else {
int out = head.value;
head = head.next;
return out;
}
}
public int peek(){
if(head == null){
System.out.println("Empty stack");
return -1;
}
return head.value;
}
}
链表
实现单链表、循环链表、双向链表,支持增删操作
单链表
public class SinglyLinkedList {
public class Node {
protected Node next; //指针域
public int data;//数据域
public Node( int data) {
this. data = data;
}
//显示此节点
public void display() {
System. out.print( data + " ");
}
}
public class LinkList {
public Node first; // 定义一个头结点
private int pos = 0;// 节点的位置
public LinkList() {
this.first = null;
}
// 插入一个头节点
public void addFirstNode(int data) {
Node node = new Node(data);
node.next = first;
first = node;
}
// 删除一个头结点,并返回头结点
public Node deleteFirstNode() {
Node tempNode = first;
first = tempNode.next;
return tempNode;
}
// 在任意位置插入节点 在index的后面插入
public void add(int index, int data) {
Node node = new Node(data);
Node current = first;
Node previous = first;
while (pos != index) {
previous = current;
current = current.next;
pos++;
}
node.next = current;
previous.next = node;
pos = 0;
}
// 删除任意位置的节点
public Node deleteByPos(int index) {
Node current = first;
Node previous = first;
while (pos != index) {
pos++;
previous = current;
current = current.next;
}
if (current == first) {
first = first.next;
} else {
pos = 0;
previous.next = current.next;
}
return current;
}
// 根据节点的data删除节点(仅仅删除第一个)
public Node deleteByData(int data) {
Node current = first;
Node previous = first; // 记住上一个节点
while (current.data != data) {
if (current.next == null) {
return null;
}
previous = current;
current = current.next;
}
if (current == first) {
first = first.next;
} else {
previous.next = current.next;
}
return current;
}
// 显示出所有的节点信息
public void displayAllNodes() {
Node current = first;
while (current != null) {
current.display();
current = current.next;
}
System.out.println();
}
// 根据位置查找节点信息
public Node findByPos(int index) {
Node current = first;
if (pos != index) {
current = current.next;
pos++;
}
return current;
}
// 根据数据查找节点信息
public Node findByData(int data) {
Node current = first;
while (current.data != data) {
if (current.next == null)
return null;
current = current.next;
}
return current;
}
}
}
双链表
public class DoublyLinkList<T>{
private Link<T> frist;
private Link<T> last;
public DoublyLinkList(){//初始化首尾指针
frist = null;
last = null;
}
public boolean isEmpty(){
return frist == null;
}
public void addFrist(T value){
Link<T> newLink= new Link(value);
if(isEmpty()){ // 如果链表为空
last = newLink; //last -> newLink
}else {
frist.pre = newLink; // frist.pre -> newLink
}
newLink.next = frist; // newLink -> frist
frist = newLink; // frist -> newLink
}
public void addLast(T value){
Link<T> newLink= new Link(value);
if(isEmpty()){ // 如果链表为空
frist = newLink; // 表头指针直接指向新节点
}else {
last.next = newLink; //last指向的节点指向新节点
newLink.pre = last; //新节点的前驱指向last指针
}
last = newLink; // last指向新节点
}
public boolean addBefore(T key,T value){
Link<T> cur = frist;
if(frist.next.val == key){
addFrist(value);
return true;
}else {
while (cur.next.val != key) {
cur = cur.next;
if(cur == null){
return false;
}
}
Link<T> newLink= new Link(value);
newLink.next = cur.next;
cur.next.pre = newLink;
newLink.pre = cur;
cur.next = newLink;
return true;
}
}
public void addAfter(T key,T value)throws RuntimeException{
Link<T> cur = frist;
while(cur.val!=key){ //经过循环,cur指针指向指定节点
cur = cur.next;
if(cur == null){ // 找不到该节点
throw new RuntimeException("Node is not exists");
}
}
Link<T> newLink = new Link<>(value);
if (cur == last){ // 如果当前结点是尾节点
newLink.next = null; // 新节点指向null
last =newLink; // last指针指向新节点
}else {
newLink.next = cur.next; //新节点next指针,指向当前结点的next
cur.next.pre = newLink; //当前结点的前驱指向新节点
}
newLink.pre = cur;//当前结点的前驱指向当前结点
cur.next = newLink; //当前结点的后继指向新节点
}
public void deleteFrist(){
if(frist.next == null){
last = null;
}else {
frist.next.pre = null;
}
frist = frist.next;
}
public void deleteLast(T key){
if(frist.next == null){
frist = null;
}else {
last.pre.next = null;
}
last = last.pre;
}
public void deleteKey(T key)throws RuntimeException{
Link<T> cur = frist;
while(cur.val!= key){
cur = cur.next;
if(cur == null){ //不存在该节点
throw new RuntimeException("Node is not exists");
}
}
if(cur == frist){ // 如果frist指向的节点
frist = cur.next; //frist指针后移
}else {
cur.pre.next = cur.next;//前面节点的后继指向当前节点的后一个节点
}
if(cur == last){ // 如果当前节点是尾节点
last = cur.pre; // 尾节点的前驱前移
}else {
cur.next.pre = cur.pre; //后面节点的前驱指向当前节点的前一个节点
}
}
public void displayForward(){
Link<T> cur = frist;
while(cur!=null){
cur.displayCurrentNode();
cur = cur.next;
}
System.out.println();
}
public void displayBackward(){
Link<T> cur = last;
while(cur!=null){
cur.displayCurrentNode();
cur = cur.pre;
}
System.out.println();
}
循环链表实现
public class CircleLinkedList <T>{
public class Node<T> {
T data;
Node<T> next;
public Node(T data){
this.data = data;
}
}
Node<T> head,tail;
Node<T> p;
int size = 0;
public CircleLinkedList(){
this.head = null;
tail = head;
p = head;
}
public int length(){
return size;
}
/**
* 添加节点
* @param data
*/
public void add(T data){
Node<T> node;
node = new Node(data);
if(head==null){
head = node;
tail = head;
p = head;
size++;
}
else{
node.next = head;
head = node;
tail.next = head;
p = head;
size++;
}
}
/**
* 得到数据
* @param index
* @return
*/
public T get(int index){
int i = 0;
p = head;
while(i!=index&&p!=tail){
i++;
p = p.next;
}
return (T) p.data;
}
/**
* 不带头结点的头插法,所谓不带头结点是指不带为空的头结点。
* 所以判断链表为空的条件不一样
* @return
*/
public boolean isEmpty(){
if(head!=null)
return false;
else
return true;
}
}
单链表的反转
//反转链表
public Node reverse(Node head) {
if (head == null || head.next == null)
return head;
Node temp = head.next;
Node newHead = reverse(head.next);
temp.next = head;
head.next = null;
return newHead;
}
两个有序的链表合并为一个有序链表
//将两个有序单链表合成一个有序单链表
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode result = null;
if (l1 == null){
return l2;
}
if (l2 == null){
return l1;
}
if (l1.val <= l2.val){
result = l1;
l1.next = mergeTwoLists(l1.next, l2); #递归
} else{
result = l2;
l2.next = mergeTwoLists(l1, l2.next);
}
return result;
}
求链表中间节点
用快慢两个指针当快指针到达末尾时慢指针为中点
public static ListNode middleNode(ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode slow = head;
ListNode fast = head.next;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
}
return fast == null ? slow : slow.next;
}