File tree Expand file tree Collapse file tree 3 files changed +242
-0
lines changed Expand file tree Collapse file tree 3 files changed +242
-0
lines changed Original file line number Diff line number Diff line change
1
+ # Copyright (C) Deepali Srivastava - All Rights Reserved
2
+ # This code is part of DSA course available on CourseGalaxy.com
3
+
4
+ class StackEmptyError (Exception ):
5
+ pass
6
+
7
+ class StackFullError (Exception ):
8
+ pass
9
+
10
+ class Stack :
11
+
12
+ def __init__ (self ,max_size = 10 ):
13
+ self .items = [None ] * max_size
14
+ self .count = 0
15
+
16
+ def size (self ):
17
+ return self .count
18
+
19
+ def is_empty (self ):
20
+ return self .count == 0
21
+
22
+ def is_full (self ):
23
+ return self .count == len (self .items )
24
+
25
+ def push (self ,x ):
26
+ if self .is_full ():
27
+ raise StackFullError ("Stack is full, can't push" )
28
+
29
+ self .items [self .count ] = x
30
+ self .count += 1
31
+
32
+ def pop (self ):
33
+ if self .is_empty ():
34
+ raise StackEmptyError ("Stack is empty, can't pop" )
35
+
36
+ x = self .items [self .count - 1 ]
37
+ self .items [self .count - 1 ] = None
38
+ self .count -= 1
39
+ return x
40
+
41
+ def peek (self ):
42
+ if self .is_empty ():
43
+ raise StackEmptyError ("Stack is empty, can't peek" )
44
+
45
+ return self .items [self .count - 1 ]
46
+
47
+ def display (self ):
48
+ print (self .items )
49
+
50
+ #########################################################
51
+
52
+ if __name__ == "__main__" :
53
+
54
+ st = Stack (8 )
55
+
56
+ while True :
57
+ print ("1.Push" )
58
+ print ("2.Pop" )
59
+ print ("3.Peek" )
60
+ print ("4.Size" )
61
+ print ("4.Display" )
62
+ print ("6.Quit" )
63
+
64
+ choice = int (input ("Enter your choice : " ))
65
+
66
+ if choice == 1 :
67
+ x = int (input ("Enter the element to be pushed : " ))
68
+ st .push (x )
69
+ elif choice == 2 :
70
+ x = st .pop ()
71
+ print ("Popped element is : " , x )
72
+ elif choice == 3 :
73
+ print ("Element at the top is : " , st .peek ())
74
+ elif choice == 4 :
75
+ print ("Size of stack " , st .size ())
76
+ elif choice == 5 :
77
+ st .display ()
78
+ elif choice == 6 :
79
+ break
80
+ else :
81
+ print ("Wrong choice" )
82
+ print ()
Original file line number Diff line number Diff line change
1
+ # Copyright (C) Deepali Srivastava - All Rights Reserved
2
+ # This code is part of DSA course available on CourseGalaxy.com
3
+
4
+ class EmptyStackError (Exception ):
5
+ pass
6
+
7
+ class Stack :
8
+
9
+ def __init__ (self ):
10
+ self .items = []
11
+
12
+ def is_empty (self ):
13
+ return self .items == []
14
+
15
+ def size (self ):
16
+ return len (self .items )
17
+
18
+ def push (self , item ):
19
+ self .items .append (item )
20
+
21
+ def pop (self ):
22
+ if self .is_empty ():
23
+ raise EmptyStackError ("Stack is empty" )
24
+ return self .items .pop ()
25
+
26
+ def peek (self ):
27
+ if self .is_empty ():
28
+ raise EmptyStackError ("Stack is empty" )
29
+ return self .items [len (self .items )- 1 ]
30
+
31
+ def display (self ):
32
+ print (self .items )
33
+
34
+ ###########################################################
35
+
36
+ if __name__ == "__main__" :
37
+ st = Stack ()
38
+
39
+ while True :
40
+ print ("1.Push" )
41
+ print ("2.Pop" )
42
+ print ("3.Peek" )
43
+ print ("4.Size" )
44
+ print ("5.Display" )
45
+ print ("6.Quit" )
46
+
47
+ choice = int (input ("Enter your choice : " ))
48
+
49
+ if choice == 1 :
50
+ x = int (input ("Enter the element to be pushed : " ))
51
+ st .push (x )
52
+ elif choice == 2 :
53
+ x = st .pop ()
54
+ print ("Popped element is : " , x )
55
+ elif choice == 3 :
56
+ print ("Element at the top is : " , st .peek ())
57
+ elif choice == 4 :
58
+ print ("Size of stack " , st .size ())
59
+ elif choice == 5 :
60
+ st .display ()
61
+ elif choice == 6 :
62
+ break ;
63
+ else :
64
+ print ("Wrong choice" )
65
+ print ()
66
+
Original file line number Diff line number Diff line change
1
+ # Copyright (C) Deepali Srivastava - All Rights Reserved
2
+ # This code is part of DSA course available on CourseGalaxy.com
3
+
4
+ class EmptyStackError (Exception ):
5
+ pass
6
+
7
+ class Node :
8
+
9
+ def __init__ (self ,value ):
10
+ self .info = value
11
+ self .link = None
12
+
13
+ class Stack :
14
+
15
+ def __init__ (self ):
16
+ self .top = None
17
+
18
+ def is_empty (self ):
19
+ return self .top == None
20
+
21
+ def size (self ):
22
+
23
+ if self .is_empty ():
24
+ retun 0
25
+
26
+ count = 0
27
+ p = self .top
28
+ while p is not None :
29
+ count += 1
30
+ p = p .link
31
+ return count
32
+
33
+ def push (self , data ):
34
+ temp = Node (data )
35
+ temp .link = self .top
36
+ self .top = temp
37
+
38
+ def peek (self ):
39
+ if self .is_empty ():
40
+ raise EmptyStackError ("Stack is empty" )
41
+ return self .top .info
42
+
43
+ def pop (self ):
44
+ if self .is_empty ():
45
+ raise EmptyStackError ("Stack is empty" )
46
+ popped = self .top .info
47
+ self .top = self .top .link
48
+ return popped
49
+
50
+ def display (self ):
51
+ if self .is_empty ():
52
+ print ("Stack is empty" )
53
+ return
54
+
55
+ print ("Stack is : " )
56
+ p = self .top
57
+ while p is not None :
58
+ print (p .info , " " )
59
+ p = p .link
60
+
61
+ #########################################################################################
62
+
63
+ if __name__ == "__main__" :
64
+ st = Stack ()
65
+
66
+ while True :
67
+ print ("1.Push" )
68
+ print ("2.Pop" )
69
+ print ("3.Peek" )
70
+ print ("5.Size" )
71
+ print ("4.Display" )
72
+ print ("6.Quit" )
73
+
74
+ choice = int (input ("Enter your choice : " ))
75
+
76
+ if choice == 1 :
77
+ x = int (input ("Enter the element to be pushed : " ))
78
+ st .push (x )
79
+ elif choice == 2 :
80
+ x = st .pop ()
81
+ print ("Popped element is : " , x )
82
+ elif choice == 3 :
83
+ print ("Element at the top is : " , st .peek ())
84
+ elif choice == 4 :
85
+ print ("Size of stack " , st .size ())
86
+ elif choice == 5 :
87
+ st .display ()
88
+ elif choice == 6 :
89
+ break ;
90
+ else :
91
+ print ("Wrong choice" )
92
+ print ()
93
+
94
+
You can’t perform that action at this time.
0 commit comments