An warware: java stack char

tari char Stacks wani muhimmin tsarin bayanai ne a cikin kimiyyar kwamfuta, yana ba da damar yin aiki mai inganci da adanawa. A cikin Java, tarin haruffa na iya zama da amfani musamman wajen warware takamaiman matsalolin shirye-shirye kamar sarrafa kirtani, rarrabawa, da kuma nazarin ma'amala. A cikin wannan labarin, za mu ga yadda ake ฦ™irฦ™ira da aiki tare da tarin haruffa a cikin Java, yayin da kuma bincika dakunan karatu da ayyuka masu alaฦ™a waษ—anda ke sauฦ™aฦ™e warware matsala tare da tarin halaye.

Tari shine tsarin bayanan ฦ˜arshe, Na Farko (LIFO), wanda ke nufin cewa abu na ฦ™arshe da aka ฦ™ara a cikin tarin shine farkon wanda za'a cire. Wannan ษ—abi'a na da amfani a yawancin mahallin shirye-shirye, kamar daidaita bayanan baฦ™aฦ™e, tantance maganganu, ko ma gano tarin kira na shirin. Bari mu zurfafa cikin aiwatarwa da amfani da tarin haruffa a Java.

ฦ˜irฦ™irar Tarin Haruffa

A cikin Java, da tari ajin bayar da java.util ana iya amfani da fakitin don ฦ™irฦ™irar tarin haruffa. Ga misali mai sauฦ™i na yadda ake ayyana tarin haruffa da aiwatar da ayyuka na yau da kullun kamar turawa, fiษ—a, da leฦ™en asiri:

import java.util.Stack;

public class CharStack {
    public static void main(String[] args) {
        Stack<Character> stack = new Stack<>();

        // Push characters onto the stack
        stack.push('A');
        stack.push('B');
        stack.push('C');

        // Pop and peek characters from the stack
        System.out.println(stack.pop());
        System.out.println(stack.peek());
    }
}

Amfani da Tarin Hali don Magance Matsaloli

Tarin haruffa suna da amfani musamman don magance matsalolin da suka haษ—a da sarrafa kirtani ko buฦ™atar bin diddigin abubuwan gida. A matsayin misali, yi la'akari da matsalar bincika ko layin da aka bayar na baฦ™aฦ™e ya โ€‹โ€‹daidaita.

Ana ษ—aukar kirtani daidaitacce idan:

  • Kowane baka na buษ—ewa yana da daidaitaccen baka na rufewa
  • Biyu na baka suna gida sosai

Za mu iya amfani da tarin hali don magance wannan matsala yadda ya kamata tare da matakai masu zuwa:

1. ฦ˜addamar da fanko na haruffa
2. Maษ—aukaki ta kowane hali a cikin kirtan shigarwa
3. Idan harafin baฦ™ar magana ce mai buษ—ewa, tura shi kan tari
4. Idan harafin baฦ™aฦ™e ne na rufewa, duba idan tari ษ—in ba komai a ciki sannan a bugo babban kashi idan ya kasance daidaitaccen baka na buษ—ewa.
5. Idan tari ba fanko ba bayan sarrafa duk haruffa, kirtani ba ta da daidaituwa

Ga lambar Java don hanyar da ke sama:

public static boolean isBalanced(String input) {
    Stack<Character> stack = new Stack<>();

    for (char c : input.toCharArray()) {
        if (c == '(' || c == '{' || c == '[') {
            stack.push(c);
        } else if (c == ')' || c == '}' || c == ']') {
            if (stack.isEmpty()) {
                return false;
            }

            char top = stack.pop();
            if ((c == ')' && top != '(') || (c == '}' && top != '{') || (c == ']' && top != '[')) {
                return false;
            }
        }
    }

    return stack.isEmpty();
}

Ta hanyar fahimta da amfani da tsarin bayanan tarin, za mu iya magance matsalolin shirye-shirye yadda ya kamata kamar waษ—anda suka haษ—a da magudin kirtani, ษ“arna, da kuma bincike na haษ—in gwiwa. Haka kuma, tare da Stack class samuwa a cikin java.util kunshin, aiwatarwa da yin amfani da tarin haruffa a Java ya zama kyakkyawan aiki.

Shafi posts:

Leave a Comment