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.