鱼C论坛

 找回密码
 立即注册
查看: 1104|回复: 1

好人一生平安,求助各位大佬

[复制链接]
发表于 2024-3-17 18:26:40 From FishC Mobile | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回文是指正读反读均相同的字符序列,如“abba”和“abdba”均是回文,但“good”不是回文。试写一个算法判定给定的字符向量是否为回文。(提示:将一半字符入栈)

函数的原型为:bool isPalindrome(string s);是回文返回true,不是返回flase。

文件源码:
StackLInk.java:
class Node {
    int data;
    Node next;

    public Node(int data) {
        this.data = data;
        this.next = null;
    }
}

public class StackLink {

    private Node top;

    public static void main(String[] args) {
        StackLink stack = new StackLink();
        stack.push(10);
        stack.push(20);
        stack.push(30);
        stack.display();
        System.out.println("寮瑰嚭鍏冪礌涓猴細" + stack.pop());
        System.out.println("鏍堥《鍏冪礌涓猴細" + stack.peek());
        stack.display();
    }

    /**
     * 鍒涘缓鏍
     */
    public StackLink() {
        top = null;
    }

    /**
     * 鍏冪礌鍏ユ爤
     * @param x
     */
    public void push(int x) {
        // 鍒涘缓鏂拌妭鐐
        Node newNode = new Node(x);
        // 鏂拌妭鐐圭殑next鎸囧悜鏍堥《鍏冪礌
        newNode.next = top;
        // 鏍堥《鎸囧悜鏂拌妭鐐
        top = newNode;
    }

    /**
     * 鍏冪礌鍑烘爤
     * @return
     */
    public int pop() {
        if (top == null) {
            System.out.println("Stack is empty");
            return -1;
        }
        // 鏍堥《鍏冪礌鍑烘爤
        int x = top.data;
        // 鏍堥《鎸囧悜涓嬩竴涓厓绱
        top = top.next;
        return x;
    }

    /**
     * 杩斿洖鏍堥《鍏冪礌
     * @return
     */
    public int peek() {
        if (top == null) {
            System.out.println("Stack is empty");
            return -1;
        }
        return top.data;
    }

    /**
     * 鍒ゆ柇鏍堟槸鍚︿负绌
     * @return
     */
    public boolean isEmpty() {
        return top == null;
    }

    /**
     * 鏄剧ず鏍堟墍鏈夊厓绱
     */
    public void display() {
        Node temp = top;
        while (temp != null) {
            System.out.print(temp.data + " ");
            temp = temp.next;
        }
        System.out.println();
    }
}




Stack.java:

import java.util.Arrays;

public class Stack{

    private int[] stack;
    private int size;

    public static void main(String[] args) {
        Stack stack = new Stack();
        stack.push(1);
        stack.push(2);
        stack.push(3);
        stack.printAllElement();
        System.out.println("鏍堥《鍏冪礌锛" + stack.peek() + " 澶у皬锛" + stack.size);
        System.out.println("鍑烘爤锛" + stack.pop());
        stack.printAllElement();
    }

    /**
     * 鍒涘缓鏍
     */
    public Stack() {
        stack = new int[10];
        size = 0;
    }

    /**
     * 鍒ゆ柇鏍堟槸鍚︿负绌
     * @return
     */
    public boolean isEmpty() {
        return size == 0;
    }

    /**
     * 娓呯┖鏍
     */
    public void clear() {
        stack = new int[10];
        size = 0;
    }

    /**
     * 杩斿洖鏍堥《鍏冪礌
     * @return 鏍堥《鍏冪礌
     */
    public int peek() {
        if (size > 0) {
            return stack[size - 1];
        } else {
            return -1;
        }
    }

    /**
     * 鍏冪礌鍏ユ爤
     * @param e
     */
    public void push(int e) {
        judgeStackSize(size + 1);
        stack[size] = e;
        size += 1;
    }

    /**
     * 鍏冪礌鍑烘爤
     * @return
     */
    public int pop() {
        int e = this.peek();
        stack[size - 1] = 0;
        size -= 1;
        return e;
    }

    /**
     * 鎵撳嵃鏍堜腑鎵鏈夊厓绱狅紝浠庢爤椤跺埌鏍堝簳
     */
    public void printAllElement() {
        for (int i = size - 1; i >= 0; i--) {
            System.out.println(stack[i]);
        }
    }

    /**
     * 鍒ゆ柇鏍堢殑绌洪棿鏄惁瓒冲锛屼笉瓒冲垯鎵╁
     * @param size
     */
    private void judgeStackSize(int size) {
        int stackLength = stack.length;
        if (size > stackLength) {
            int expaned_size = size + 10;
            stack = Arrays.copyOf(stack, expaned_size);
        }
    }

}
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-10-4 04:08

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表