File tree 5 files changed +311
-0
lines changed
5 files changed +311
-0
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+ Copyright (C) Deepali Srivastava - All Rights Reserved
3
+ This code is part of DSA course available on CourseGalaxy.com
4
+ */
5
+
6
+ package stackArray ;
7
+
8
+ import java .util .Scanner ;
9
+
10
+ public class Demo
11
+ {
12
+ public static void main (String [] args )
13
+ {
14
+ int choice ,x ;
15
+ Scanner scan = new Scanner (System .in );
16
+
17
+ StackA st = new StackA (8 );
18
+
19
+ while (true )
20
+ {
21
+ System .out .println ("1.Push an element on the stack" );
22
+ System .out .println ("2.Pop an element from the stack" );
23
+ System .out .println ("3.Display the top element" );
24
+ System .out .println ("4.Display all stack elements" );
25
+ System .out .println ("5.Display size of the stack" );
26
+ System .out .println ("6.Quit" );
27
+ System .out .println ("Enter your choice : " );
28
+ choice = scan .nextInt ();
29
+
30
+ if (choice ==6 )
31
+ break ;
32
+
33
+ switch (choice )
34
+ {
35
+ case 1 :
36
+ System .out .println ("Enter the element to be pushed : " );
37
+ x =scan .nextInt ();
38
+ st .push (x );
39
+ break ;
40
+ case 2 :
41
+ x =st .pop ();
42
+ System .out .println ("Popped element is : " + x );
43
+ break ;
44
+ case 3 :
45
+ System .out .println ("Element at the top is : " + st .peek ());
46
+ break ;
47
+ case 4 :
48
+ st .display ();
49
+ break ;
50
+ case 5 :
51
+ System .out .println ("Size of stack " + st .size ());
52
+ break ;
53
+ default :
54
+ System .out .println ("Wrong choice" );
55
+ }
56
+ System .out .println ("" );
57
+ }
58
+ scan .close ();
59
+ }
60
+ }
Original file line number Diff line number Diff line change
1
+ /*
2
+ Copyright (C) Deepali Srivastava - All Rights Reserved
3
+ This code is part of DSA course available on CourseGalaxy.com
4
+ */
5
+
6
+ package stackArray ;
7
+
8
+ import java .util .EmptyStackException ;
9
+
10
+ public class StackA
11
+ {
12
+ private int [] stackArray ;
13
+ private int top ;
14
+
15
+ public StackA ()
16
+ {
17
+ stackArray = new int [10 ];
18
+ top = -1 ;
19
+ }
20
+
21
+ public StackA (int maxSize )
22
+ {
23
+ stackArray = new int [maxSize ];
24
+ top = -1 ;
25
+ }
26
+
27
+ public int size ()
28
+ {
29
+ return top +1 ;
30
+ }
31
+
32
+ public boolean isEmpty ()
33
+ {
34
+ return (top ==-1 );
35
+ }
36
+
37
+ public boolean isFull ()
38
+ {
39
+ return (top ==stackArray .length -1 );
40
+ }
41
+
42
+ public void push (int x )
43
+ {
44
+ if (isFull ())
45
+ {
46
+ System .out .println ("Stack Overflow" );
47
+ return ;
48
+ }
49
+ top =top +1 ;
50
+ stackArray [top ]=x ;
51
+ }
52
+
53
+ public int pop ()
54
+ {
55
+ int x ;
56
+ if (isEmpty ())
57
+ {
58
+ System .out .println ("Stack Underflow" );
59
+ throw new EmptyStackException ();
60
+ }
61
+ x =stackArray [top ];
62
+ top =top -1 ;
63
+ return x ;
64
+ }
65
+
66
+ public int peek ()
67
+ {
68
+ if (isEmpty ())
69
+ {
70
+ System .out .println ("Stack Underflow" );
71
+ throw new EmptyStackException ();
72
+ }
73
+ return stackArray [top ];
74
+ }
75
+
76
+ public void display ()
77
+ {
78
+ int i ;
79
+
80
+ if (isEmpty ())
81
+ {
82
+ System .out .println ("Stack is empty" );
83
+ return ;
84
+ }
85
+ System .out .println ("Stack is : " );
86
+ for (i =top ; i >=0 ; i --)
87
+ System .out .println (stackArray [i ] + " " );
88
+ System .out .println ();
89
+ }
90
+ }
Original file line number Diff line number Diff line change
1
+ /*
2
+ Copyright (C) Deepali Srivastava - All Rights Reserved
3
+ This code is part of DSA course available on CourseGalaxy.com
4
+ */
5
+
6
+ package stackLinked ;
7
+
8
+ import java .util .Scanner ;
9
+
10
+
11
+ public class Demo
12
+ {
13
+ public static void main (String [] args )
14
+ {
15
+ int choice ,x ;
16
+ Scanner scan = new Scanner (System .in );
17
+
18
+ StackL st = new StackL ();
19
+
20
+ while (true )
21
+ {
22
+ System .out .println ("1.Push an element on the stack" );
23
+ System .out .println ("2.Pop an element from the stack" );
24
+ System .out .println ("3.Display the top element" );
25
+ System .out .println ("4.Display all stack elements" );
26
+ System .out .println ("5.Display size of the stack" );
27
+ System .out .println ("6.Quit" );
28
+ System .out .print ("Enter your choice : " );
29
+ choice = scan .nextInt ();
30
+
31
+ if (choice ==6 )
32
+ break ;
33
+ switch (choice )
34
+ {
35
+ case 1 :
36
+ System .out .print ("Enter the element to be pushed : " );
37
+ x =scan .nextInt ();
38
+ st .push (x );
39
+ break ;
40
+ case 2 :
41
+ x =st .pop ();
42
+ System .out .println ("Popped element is : " + x );
43
+ break ;
44
+ case 3 :
45
+ System .out .println ("Element at the top is : " + st .peek ());
46
+ break ;
47
+ case 4 :
48
+ st .display ();
49
+ break ;
50
+ case 5 :
51
+ System .out .println ("Size of stack " + st .size ());
52
+ break ;
53
+ default :
54
+ System .out .println ("Wrong choice" );
55
+ }
56
+ System .out .println ("" );
57
+ }
58
+ scan .close ();
59
+ }
60
+ }
Original file line number Diff line number Diff line change
1
+ /*
2
+ Copyright (C) Deepali Srivastava - All Rights Reserved
3
+ This code is part of DSA course available on CourseGalaxy.com
4
+ */
5
+
6
+ package stackLinked ;
7
+
8
+ public class Node
9
+ {
10
+ public int info ;
11
+ public Node link ;
12
+
13
+ public Node (int i )
14
+ {
15
+ info =i ;
16
+ link =null ;
17
+ }
18
+ }
Original file line number Diff line number Diff line change
1
+ /*
2
+ Copyright (C) Deepali Srivastava - All Rights Reserved
3
+ This code is part of DSA course available on CourseGalaxy.com
4
+ */
5
+
6
+ package stackLinked ;
7
+
8
+ import java .util .EmptyStackException ;
9
+
10
+ public class StackL
11
+ {
12
+ private Node top ;
13
+
14
+ public StackL ()
15
+ {
16
+ top =null ;
17
+ }
18
+
19
+ public int size ()
20
+ {
21
+ int s =0 ;
22
+ Node p =top ;
23
+ while (p !=null )
24
+ {
25
+ p =p .link ;
26
+ s ++;
27
+ }
28
+ return s ;
29
+ }
30
+
31
+ public void push (int x )
32
+ {
33
+ Node temp = new Node (x );
34
+ temp .link =top ;
35
+ top =temp ;
36
+ }
37
+
38
+ public int pop ()
39
+ {
40
+ int x ;
41
+ if (isEmpty ())
42
+ {
43
+ System .out .println ("Stack Underflow\n " );
44
+ throw new EmptyStackException ();
45
+ }
46
+ x =top .info ;
47
+ top =top .link ;
48
+ return x ;
49
+ }
50
+
51
+ public int peek ()
52
+ {
53
+ if (isEmpty ())
54
+ {
55
+ System .out .println ("Stack Underflow\n " );
56
+ throw new EmptyStackException ();
57
+ }
58
+ return top .info ;
59
+ }
60
+
61
+ public boolean isEmpty ()
62
+ {
63
+ return (top ==null );
64
+ }
65
+
66
+ public void display ()
67
+ {
68
+ Node p =top ;
69
+ if (isEmpty ())
70
+ {
71
+ System .out .println ("Stack is empty" );
72
+ return ;
73
+ }
74
+
75
+ System .out .println ("Stack is : " );
76
+ while (p !=null )
77
+ {
78
+ System .out .println (p .info + " " );
79
+ p =p .link ;
80
+ }
81
+ System .out .println ();
82
+ }
83
+ }
You can’t perform that action at this time.
0 commit comments