Nadesłany przez Kamil Dworak, 25 lipca 2010 14:06
Kod przedstawiony poniżej przedstawia główną część rozwiązania problemu.Pobierz pełne rozwiązanie.
Jeżeli nie odpowiada Ci sposób formatowania kodu przez autora skorzystaj z pretty printer'a i dostosuj go automatycznie do siebie.
stos_1_cs.cs:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Main { public class Elem { /* pola */ private int wartosc; private Elem next; /* konstruktor */ public Elem (int x, Elem n){ next = n; wartosc = x; } /* zwraca wsk na nastepny obiwkt */ public Elem getNext(){ return next; } /*zwraca przechowywana wartosc */ public int getWartosc(){ return wartosc; } } class Stos { /* wierzcholek stosu */ Elem start; public Stos() { start = null; } /*zwracam wartocs wierzcholka */ public Elem getStart() { return start; } /* dodaje element do stosu */ public void push(int x) { Elem temp = new Elem(x, start); start = temp; } /* sciagam element ze stosu */ public void pop() { if (start != null) { start = start.getNext(); } else { Console.WriteLine("Stos jest pusty"); } } /* wyswietlam zawartosc stosu */ public void show() { if (start != null) { Elem temp = start; while (temp != null) { Console.Write(temp.getWartosc() + " "); temp = temp.getNext(); } Console.WriteLine(); } else { Console.WriteLine("Stos jest pusty"); } } /* zwracam ile elementow jest na stosie */ public int zlicz(){ if(start!=null){ Elem temp = start; int licznik=0; while(temp!=null){ licznik++; temp = temp.getNext(); } return licznik; } else return 0; } /* * odwracam kolejnosc elemetow na stosie * wspomagajac sie dodatkowymi dwoma stosami */ public void reverse(Stos stos2, Stos stos3){ if(start!=null){ Elem temp = start; while(temp!=null){ stos2.push(temp.getWartosc()); this.pop(); temp=temp.getNext(); } while(stos2.getStart()!=null){ stos3.push(stos2.getStart().getWartosc()); stos2.pop(); } while(stos3.getStart()!=null){ this.push(stos3.getStart().getWartosc()); stos3.pop(); } } else Console.WriteLine("Stos jest pusty"); } /* * porzadkuje kolejnosc elementow na * stosie (od min do max) * */ public void sort(Stos stos2){ if(start!=null){ int licznik = this.zlicz(); int max, licznik2=0; while(licznik!=0){ Elem temp = start; max = temp.getWartosc(); while(temp!=null){ if(max>temp.getWartosc()){ max = temp.getWartosc(); } temp = temp.getNext(); } while(start!=null){ if(start.getWartosc()!=max){ stos2.push(start.getWartosc()); this.pop(); licznik2++; } else { this.pop(); while(licznik2!=0){ this.push(stos2.getStart().getWartosc()); stos2.pop(); licznik2--; } stos2.push(max); break; } } licznik--; } while(stos2.getStart()!=null){ this.push(stos2.getStart().getWartosc()); stos2.pop(); } } else { Console.WriteLine("Stos jest pusty"); } } public static void menu(){ Console.WriteLine("(0) Menu\n(1) PUSH\n(2) POP\n(3) SHOW"); Console.WriteLine("(4) ZLICZ\n(5) REVERSE\n(6) SORT\n(7) EXIT"); } } class Program { static void Main(string[] args) { Stos stos1 = new Stos(); Stos stos2 = new Stos(); Stos stos3 = new Stos(); int t; String s; while ((s = Console.ReadLine()) != null) { t = int.Parse(s); switch (t) { case 0: Stos.menu();break; case 1: Console.Write("co chcesz dodac?"); s = Console.ReadLine(); t = int.Parse(s); stos1.push(t); break; case 2: stos1.pop(); break; case 3: stos1.show(); break; case 4: Console.WriteLine(stos1.zlicz()); break; case 5: Console.WriteLine("odwracam"); stos1.reverse(stos2, stos3); break; case 6: Console.WriteLine("Sortuje"); stos1.sort(stos2); break; case 7: return; //default:; } } } } }