Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.Iterator;
- import java.util.NoSuchElementException;
- class SLLNode<E> {
- protected E naslov;
- protected E tekst;
- protected SLLNode<E> succ;
- public SLLNode(E naslov,E tekst, SLLNode<E> succ) {
- this.naslov = naslov;
- this.tekst = tekst;
- this.succ = succ;
- }
- @Override
- public String toString() {
- return naslov.toString() + " " + tekst.toString();
- }
- }
- class SLL<E> {
- private SLLNode<E> first;
- public SLL() {
- // Construct an empty SLL
- this.first = null;
- }
- public void deleteList() {
- first = null;
- }
- public int length() {
- int ret;
- if (first != null) {
- SLLNode<E> tmp = first;
- ret = 1;
- while (tmp.succ != null) {
- tmp = tmp.succ;
- ret++;
- }
- return ret;
- } else
- return 0;
- }
- @Override
- public String toString() {
- String ret = new String();
- if (first != null) {
- SLLNode<E> tmp = first;
- ret += tmp + "\n";
- while (tmp.succ != null) {
- tmp = tmp.succ;
- ret += tmp + "\n";
- }
- } else
- ret = "Prazna lista!!!";
- return ret;
- }
- public void insertFirst(E o,E k) {
- SLLNode<E> ins = new SLLNode<E>(o,k, first);
- first = ins;
- }
- public void insertAfter(E o,E k, SLLNode<E> node) {
- if (node != null) {
- SLLNode<E> ins = new SLLNode<E>(o,k, node.succ);
- node.succ = ins;
- } else {
- System.out.println("Dadenot jazol e null");
- }
- }
- public void insertBefore(E o, E k, SLLNode<E> before) {
- if (first != null) {
- SLLNode<E> tmp = first;
- if(first==before){
- this.insertFirst(o,k);
- return;
- }
- //ako first!=before
- while (tmp.succ != before)
- tmp = tmp.succ;
- if (tmp.succ == before) {
- SLLNode<E> ins = new SLLNode<E>(o, k, before);
- tmp.succ = ins;
- } else {
- System.out.println("Elementot ne postoi vo listata");
- }
- } else {
- System.out.println("Listata e prazna");
- }
- }
- public void insertLast(E o, E k) {
- if (first != null) {
- SLLNode<E> tmp = first;
- while (tmp.succ != null)
- tmp = tmp.succ;
- SLLNode<E> ins = new SLLNode<E>(o, k, null);
- tmp.succ = ins;
- } else {
- insertFirst(o,k);
- }
- }
- public E deleteFirst() {
- if (first != null) {
- SLLNode<E> tmp = first;
- first = first.succ;
- return tmp.naslov;
- } else {
- System.out.println("Listata e prazna");
- return null;
- }
- }
- public E delete(SLLNode<E> node) {
- if (first != null) {
- SLLNode<E> tmp = first;
- if(first ==node){
- return this.deleteFirst();
- }
- while (tmp.succ != node && tmp.succ.succ != null)
- tmp = tmp.succ;
- if (tmp.succ == node) {
- tmp.succ = tmp.succ.succ;
- return node.naslov;
- } else {
- System.out.println("Elementot ne postoi vo listata");
- return null;
- }
- } else {
- System.out.println("Listata e prazna");
- return null;
- }
- }
- public SLLNode<E> getFirst() {
- return first;
- }
- public SLLNode<E> find(E o, E k) {
- if (first != null) {
- SLLNode<E> tmp = first;
- while (tmp.naslov != o && tmp.succ != null)
- tmp = tmp.succ;
- if (tmp.naslov == o) {
- return tmp;
- } else {
- System.out.println("Elementot ne postoi vo listata");
- }
- } else {
- System.out.println("Listata e prazna");
- }
- return first;
- }
- public Iterator<E> iterator () {
- // Return an iterator that visits all elements of this list, in left-to-right order.
- return new LRIterator<E>();
- }
- // //////////Inner class ////////////
- private class LRIterator<E> implements Iterator<E> {
- private SLLNode<E> place, curr;
- private LRIterator() {
- place = (SLLNode<E>) first;
- curr = null;
- }
- public boolean hasNext() {
- return (place != null);
- }
- public E next() {
- if (place == null)
- throw new NoSuchElementException();
- E nextElem = place.naslov;
- curr = place;
- place = place.succ;
- return nextElem;
- }
- public void remove() {
- //Not implemented
- }
- }
- public void mirror(){
- if (first != null) {
- //m=nextsucc, p=tmp,q=next
- SLLNode<E> tmp = first;
- SLLNode<E> newsucc = null;
- SLLNode<E> next;
- while(tmp != null){
- next = tmp.succ;
- tmp.succ = newsucc;
- newsucc = tmp;
- tmp = next;
- }
- first = newsucc;
- }
- }
- public void merge (SLL<E> in){
- if (first != null) {
- SLLNode<E> tmp = first;
- while(tmp.succ != null)
- tmp = tmp.succ;
- tmp.succ = in.getFirst();
- }
- else{
- first = in.getFirst();
- }
- }
- }
- public class EmailPoraki {
- public static void main(String [] args) throws IOException {
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- int n = Integer.parseInt(br.readLine());
- SLL<String> lista = new SLL<String>();
- SLLNode<String> tmp = null;
- String str = "";
- for(int i=0;i<n;i++){
- tmp = lista.getFirst();
- str = "";
- String [] line = br.readLine().split(" ");
- String operacija = line[0]; /// zimanje info
- String naslov = "[" +line[1] + "]:";///
- int k = 2;
- for(k=2;k<line.length-1;k++){
- str += line[k] + " ";
- }
- str+=line[k];
- str = str.trim();
- if(operacija.equals("DEL")&&tmp==null)
- break;
- else
- if(operacija.equals("DEL")){
- while(true){
- String m = tmp.naslov.split(" ")[0].trim();
- if(m.equals(naslov)){
- lista.delete(tmp);
- break;
- }
- tmp = tmp.succ;
- }
- }
- if(tmp==null){
- if(operacija.equals("SND")){
- lista.insertLast(naslov + "", str);
- }
- else if(operacija.equals("RCV")){
- lista.insertLast(naslov + " >", str);
- }
- }
- else{
- while(tmp!=null){
- String [] split = tmp.naslov.split(" ");
- String gettmpnaslov = split[0].trim();
- if(gettmpnaslov.equals(naslov.trim())){
- if(operacija.equals("SND")){
- tmp.naslov = naslov + " >";
- tmp.tekst = str;
- break;
- }
- else if(operacija.equals("RCV")){
- tmp.naslov = naslov + "";
- tmp.tekst = str;
- break;
- }
- }
- tmp = tmp.succ;
- }
- if(tmp==null){
- if(operacija.equals("SND")){
- lista.insertLast(naslov + " >",str);
- }
- else if(operacija.equals("RCV")){
- lista.insertLast(naslov + " ",str);
- }
- }
- }
- }
- SLLNode<String> tmp2;
- tmp = lista.getFirst().succ;
- str = "";
- while(tmp!=null){
- tmp2 = lista.getFirst();
- while(tmp2!=tmp){
- String str1 = "";
- String str2 = "";
- if(tmp.naslov.contains(">"))
- str1 = tmp.naslov.split(" ")[0].trim();
- else
- str1 = tmp.naslov.trim();
- if(tmp2.naslov.contains(">"))
- str2 = tmp2.naslov.split(" ")[0].trim();
- else
- str2 = tmp2.naslov.trim();
- if(str1.length()>str2.length()){
- }
- if(str1.length()==str2.length()){
- if(str1.compareTo(str2)<0){
- // System.out.println(str1 + " " + str2);
- // System.out.println(lista + " opdasaspdsodpkdopas");
- SLLNode<String> help = tmp;
- SLLNode<String> help2 = tmp2;
- lista.insertAfter(tmp2.naslov,tmp2.tekst,tmp);
- lista.insertBefore(tmp.naslov,tmp.tekst,tmp2);
- lista.delete(help);
- lista.delete(help2);
- tmp2 = tmp2.succ;
- break;
- }
- }
- tmp2 = tmp2.succ;
- }
- tmp = tmp.succ;
- }
- System.out.println(lista);
- }
- }
Add Comment
Please, Sign In to add comment