栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

基于单向链表实现栈

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

基于单向链表实现栈

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;
    }


    
    public static int ListLength(ListNode headNode){
        int length = 0;
        ListNode headNodeS = headNode;
        if (headNode != null){
            length++;
            headNodeS = headNode.getNext();
        }
        return length;
    }

    
    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;
    }


    
    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;
        
    }

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/301478.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号