基于单向链表实现栈


import com.demo.ListNode;

import java.util.EmptyStackException;

/**
 * 基于单向链表实现栈
 */
public class LLStack {
    private ListNode headNode;

    public LLStack(ListNode headNode) {
        this.headNode = new ListNode(null);
    }

    public void Push(int data){
        if (headNode == null){
            headNode = new ListNode(data);
        }else if (headNode.getNext() == null){
            headNode.setData(data);
        }else {
            ListNode llNode = new ListNode(data);
            llNode.setNext(headNode);
            headNode = llNode;
        }
    }

    public int top(){
        if (headNode == null){
            return -1;
        }else {
            return headNode.getData();
        }
    }

    public int pop() {
        if (headNode == null) {
            throw new EmptyStackException();
        }else{
            int data = headNode.getData();
            headNode = headNode.getNext();
            return data;
        }
    }

    public boolean isEmpty(){
        if (headNode == null) {
            return true;
        }else{
            return false;
        }
    }

    public void deleteStack(){
        headNode = null;
    }

}

单向链表


/**
 * 单向链表
 */
public class ListNode {
    private int data;
    private ListNode next;
    public ListNode(Integer data) {
        this.data = data;
    }
    public int getData() {
        return data;
    }

    public void setData(int data) {
        this.data = data;
    }

    public ListNode getNext() {
        return next;
    }

    public void setNext(ListNode next) {
        this.next = next;
    }


    /**
     * 计算链表中结点的个数
     * @param headNode
     * @return
     */
    public static int ListLength(ListNode headNode){
        int length = 0;
        ListNode headNodeS = headNode;
        if (headNode != null){
            length++;
            headNodeS = headNode.getNext();
        }
        return length;
    }

    /**
     * 单向链表的插入
     * @param headNode
     * @param nodeToInsert
     * @param position
     * @return
     */
    public static ListNode InsertInLinkedList(ListNode headNode,ListNode nodeToInsert,int position){
        if (headNode == null){ // 若链表为空,插入
            return nodeToInsert;
        }

        int size = ListLength(headNode); // 获取结点个数
        if (position > size-1 || position < 1){
            System.out.println(size+1);
            return headNode;
        }

        if (position == 1){ // 在链表开头插入
            nodeToInsert.setNext(headNode);
            return nodeToInsert;
        }else{
            //在中间或结尾插入
            ListNode previousNode = headNode;
            int count = 1;
            while (count < position - 1){
                previousNode = previousNode.getNext();
                count++;
            }
            ListNode currentNode = previousNode.getNext();
            nodeToInsert.setNext(currentNode);
            previousNode.setNext(nodeToInsert);
        }
        return headNode;
    }


    /**
     * 删除链表中的元素
     * @param headNode
     * @param position
     * @return
     */
    public static ListNode DeleteNodeFromLinkedList(ListNode headNode, int position){
        int size = ListLength(headNode);
        if (position > size || position < 1){
            System.out.println(size);
            return headNode;
        }

        if (position == 1){ 
            ListNode currentNode = headNode.getNext(); 
            headNode = null;
            return currentNode;
        }else { //删除中间或表尾结点
            ListNode previousNode = headNode;
            int count = 1;
            while(count < position){
                previousNode = previousNode.getNext();
                count++;
            }
            ListNode currentNode = previousNode.getNext();
            previousNode.setNext(currentNode.getNext());
            currentNode = null;
        }
        return headNode;
    }

    public static void DeleteLinkedList(ListNode headNode){
        ListNode auxilaryNode, iterator = headNode;
        while (iterator != null){
            auxilaryNode = iterator.getNext();
            iterator = null; //java垃圾回收器将自动处理
            iterator = auxilaryNode;
        }
        char[] c = new char[10];
        String s;
        
    }

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值