We set the task to write a program in which, having a string, you need to form a queue with paired characters and a stack with every fifth character. I am not very strong in this, but I did such a program, but it doesn’t produce what I need (everything works out with an array of numbers). How to remake a line? Help, I will be very grateful.

public class Node { private int inf; private Node next; Node() { inf=0; next=null; } Node(int i) { inf=i; next=null; } public void setInf(int i) { inf=i; } public void setNext(Node n) { next=n; } public int getInf() { return inf; } public Node getNext() { return next; } } public class List { private Node first; List() { first=null; } public void addFirstNode(Node n) { n.setNext(first); first=n; } public void addLastNode(Node n) { if (first==null) first=n; else { Node tmp=first; while (tmp.getNext()!=null) { tmp=tmp.getNext(); } tmp.setNext(n); } } public void addFirst(int a) { Node newnode = new Node(a); addFirstNode(newnode); } public void addLast(int a) { Node newnode = new Node(a); addLastNode(newnode); } public void showList() { Node tmp=first; while (tmp!=null) { System.out.print(" "+tmp.getInf()); tmp=tmp.getNext(); } System.out.println(); } } package dom; //основная программа public class Dom { public static void main(String[] args) { List queue = new List(); List stack = new List(); String s = "125478965321547896521547889633215478965487512546985478521"; int l = s.length(); for (int i=0; i<l; i++) { if (i%2==0) { char k=s.charAt(i); queue.addLast(k); } if (i%5==0) { char k=s.charAt(i); stack.addFirst(k); } } queue.showList(); stack.showList(); } } 

    3 answers 3

    With the sample, everything seems to be in order ... Is there a problem in outputting codes instead of characters? Then store in Node#inf value of char , not an int , or showList() char in showList() :

     System.out.print(" " + (char)tmp.getInf()); 

    Only you should understand that the structure implemented by you is neither a queue nor a stack .

      The fact is that you keep the character code on the stack and the queue, since your structure stores an integer type (number). To make it work, you need to convert the number to a symbol:

       public void showList() { Node tmp=first; while (tmp!=null) { System.out.print(" "+ (char)tmp.getInf()); //здесь преобразование кода в символ tmp=tmp.getNext(); } System.out.println(); } 

        There is another option: change the type of the inf field in the Node class from int to Object. The getInf (), setInf (...) and constructor, as well as the addLast (...) and addFirst (...) methods in the List class will change accordingly. For integers and characters, this can be done, since automatic wrapping / unwrapping takes place in the corresponding Integer and Character types. The output code of the list in the console will not change: the objects are written using String.valueOf (...), for wrapper classes this will be the value of the number / symbol.

        Here are the code sections that need to be changed:

         //Node private Object inf; Node(Object inf){ this.inf = inf; next = null; } public void setInf(Object inf){ this.inf = inf; } public Object getInf(){ return inf; } //List public void addFirst(Object inf){ Node newnode = new Node(inf); addFirstNode(newnode); } public void addLast(Object inf){ Node newnode = new Node(inf); addLastNode(newnode); }