File tree Expand file tree Collapse file tree 6 files changed +214
-0
lines changed
Expand file tree Collapse file tree 6 files changed +214
-0
lines changed Original file line number Diff line number Diff line change 1+ <?xml version =" 1.0" encoding =" UTF-8" ?>
2+ <module type =" JAVA_MODULE" version =" 4" >
3+ <component name =" NewModuleRootManager" inherit-compiler-output =" true" >
4+ <exclude-output />
5+ <content url =" file://$MODULE_DIR$" >
6+ <sourceFolder url =" file://$MODULE_DIR$/src" isTestSource =" false" />
7+ </content >
8+ <orderEntry type =" inheritedJdk" />
9+ <orderEntry type =" sourceFolder" forTests =" false" />
10+ </component >
11+ </module >
Original file line number Diff line number Diff line change 1+ package zad1 ;
2+
3+ public interface InterStack <E > {
4+ boolean isEmpty ();
5+ E top ();
6+ E pop ();
7+ int size ();
8+ int deepLevel (E item );
9+ void push (E item );
10+
11+ }
Original file line number Diff line number Diff line change 1+ package zad1 ;
2+
3+ import java .util .Scanner ;
4+
5+ public class Main {
6+ Scanner sc = new Scanner (System .in );
7+
8+
9+ public void solution (){
10+
11+ public static void run (){
12+ TStack <String > stackOfBook =
13+ new TStack <>();
14+ try {
15+ loadData (stackOfBooks ,5 );
16+ }catch (Exception e ){
17+ System .out .println ("Wyjatek " +e .getMessage ());
18+ e .printStackTrace ();
19+ return ;
20+ }
21+
22+ System .out .println ("ksiazki od ostatniej " );
23+ while (!stackOfBook .isEmpty ()){
24+ System .out .println (stackOfBooks .pop ()); //zdejmuje elementy
25+ }
26+ }
27+ private static void loadData (TStack <String > stack ,int n ){
28+ if (stack == null ){
29+ throw new NullPointerException ();
30+ }
31+ Scanner sc = new Scanner (System .in );
32+ System .out .println (String .format ("Podaj %d ksiazek w kolej liniach" ,n ));
33+ String tmp ;
34+ for (int i =1 ;i <=n ;i ++){
35+ System .out .println ("podaj pozycje ksiazek" );
36+ tmp = sc .nextLine ();
37+ stack .push (tmp );
38+ }
39+
40+ }
41+
42+
43+ }
44+
45+ public static void partA (){
46+ TStack <Integer > st =
47+ new TStack <~>();
48+ int num =23 ;
49+ System .out .println ("spradzamy elementy " + num );
50+ System .out .println ("stos pusty" );
51+ System .out .println ("glebokosc " +num +st .deepLevel (num ));
52+ st .push (5 );
53+ st .push (10 );
54+ st .push (15 );
55+ System .out .println ("stos niepusty brak leementu " +num +st .deepLevel (num ));
56+ st .push (23 );
57+ st .push (100 );
58+ st .push (-1 );
59+ System .out .println ("stos niepusty szukany" );
60+
61+
62+ while (!st .isEmpty ()){
63+ System .out .println (st .pop ());
64+ }
65+ }
66+
67+ }
68+
69+
Original file line number Diff line number Diff line change 1+ package zad1 ;
2+
3+ import java .util .EmptyStackException ;
4+
5+ public class TStack <E > implements InterStack <E > {
6+ private TStackNode <E > top ; //uchwyt 1 elementu
7+
8+ public TStack () {
9+ top = null ;
10+ }
11+
12+ @ Override
13+ public boolean isEmpty () {
14+ if (top == null )
15+ return true ;
16+ else
17+ return false ;
18+ }
19+
20+ @ Override
21+ public E top () {
22+ if ( top == null ){
23+ throw new EmptyStackException ();
24+ } else {
25+ return top .getData ();
26+ }
27+ }
28+
29+ @ Override
30+ public E pop () {
31+ if ( top == null ){
32+ throw new EmptyStackException ();
33+ } else {
34+ E dataOut = top .getData ();
35+ top = top .getPrev ();
36+ return dataOut ;
37+ }
38+ }
39+
40+ @ Override
41+ public int size () {
42+ if ( top == null ){
43+ return 0 ;
44+ } else {
45+ TStackNode <E > refNode = top ;
46+ int counter = 0 ;
47+ while (refNode != null ) {
48+ counter ++;
49+ refNode = refNode .getPrev ();
50+ }
51+ return counter ;
52+ }
53+ }
54+
55+ @ Override
56+ public int deepLevel (E item ) {
57+ TStackNode <E > tmp = top ;
58+ int level =0 ;
59+ while (tmp !=null ){
60+ if (item ==tmp .getData ()){
61+ return level ;
62+ }
63+ tmp = tmp .getPrev ();
64+ level ++;
65+ }
66+ return -1 ;
67+ }
68+
69+ public void deleteBottom (){
70+ if (top == null ){
71+ throw new EmptyStackException ();
72+ }else {
73+ TStackNode <E > tmp = top ;
74+ if (tmp .getPrev ()==null ){
75+ tmp =null ;
76+ //tylko jeden elemrnt jest na stosie
77+ }
78+ else
79+ {
80+ while (tmp .getPrev ().getPrev ()!=null ){
81+ tmp = tmp .getPrev (); //jes twiecej ni z jeden element
82+ }
83+ tmp .setPrev (null );//pomijany ten na dole i sutawiamy na null
84+ }
85+ }
86+
87+ @ Override
88+ public void push (E item ) {
89+ if (top == null ) {
90+ top = new TStackNode <>(item , null );
91+ } else {
92+ TStackNode <E > refNode = new TStackNode <>(item , top );
93+ top = refNode ;
94+ }
95+ }
96+ }
Original file line number Diff line number Diff line change 1+ package zad1 ;
2+
3+ public class TStackNode <E > {
4+ private E data ;
5+ private TStackNode prev ;
6+
7+ public TStackNode (E data , TStackNode prev ) {
8+ this .data = data ;
9+ this .prev = prev ;
10+ }
11+
12+ public E getData () {
13+ return data ;
14+ }
15+
16+ public void setData (E data ) {
17+ this .data = data ;
18+ }
19+
20+ public TStackNode getPrev () {
21+ return prev ;
22+ }
23+
24+ public void setPrev (TStackNode prev ) {
25+ this .prev = prev ;
26+ }
27+ }
You can’t perform that action at this time.
0 commit comments