Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9fb7c83

Browse files
Added Stack using Linked Lists.
1 parent 6dd5007 commit 9fb7c83

File tree

3 files changed

+160
-4
lines changed

3 files changed

+160
-4
lines changed

‎README.md‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,29 @@
22

33
## Algorithms
44

5-
### Searching Algorithms
5+
### 1. Searching Algorithms
66

77
- [Binary Search - both Recursive and Iterative Approach](Searching%20Algorithm/binarysearch.py)
88

9-
### Sorting Algorithms
9+
### 2. Sorting Algorithms
1010

11-
#### [Index](Sorting%20Algorithms/README.md)
11+
Find the detailed description on [Index](Sorting%20Algorithms/) page.
1212

1313
- [Sorting Algorithms Code](Sorting%20Algorithms/sortingAlgo.py)
1414
- [Random Numbers for Input](Sorting%20Algorithms/num.txt)
1515

1616
## Data Structures
1717

18-
### Linked Lists
18+
### 1. Linked Lists
1919

2020
Find the detailed description on operations performed [here.](Linked%20List/)
2121

2222
1. [Linear Linked List](Linked%20List/linkedlist.py)
2323
2. [Circular Linked List](Linked%20List/circularLL.py)
2424
3. [Doubly Linked List](Linked%20List/doublyLL.py)
25+
26+
### 2. Stack
27+
28+
1. [Stack using inbuilt Python List](Stacks/stack.py)
29+
2. [Stack using Linked List](Stacks/stackLL.py)
30+
3.
File renamed without changes.

‎Stacks/stackLL.py‎

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
import os
2+
3+
4+
class _Node:
5+
'''
6+
Creates a Node with two fields:
7+
1. element (accesed using ._element)
8+
2. link (accesed using ._link)
9+
'''
10+
__slots__ = '_element', '_link'
11+
12+
def __init__(self, element, link):
13+
'''
14+
Initialses _element and _link with element and link respectively.
15+
'''
16+
self._element = element
17+
self._link = link
18+
19+
20+
class StackLL:
21+
'''
22+
Consists of member funtions to perform different
23+
operations on the linked list.
24+
'''
25+
26+
def __init__(self):
27+
'''
28+
Initialses head, tail and size with None, None and 0 respectively.
29+
'''
30+
self._head = None
31+
self._tail = None
32+
self._size = 0
33+
34+
def __len__(self):
35+
'''
36+
Returns length of stack.
37+
'''
38+
return self._size
39+
40+
def isempty(self):
41+
'''
42+
Returns True if stack is empty, otherwise False.
43+
'''
44+
return self._size == 0
45+
46+
def push(self, e):
47+
'''
48+
Pushes the passed element at the beginning of the linked list.
49+
That means, on the top of our stack.
50+
'''
51+
newest = _Node(e, None)
52+
53+
if self.isempty():
54+
self._head = newest
55+
self._tail = newest
56+
else:
57+
newest._link = self._head
58+
self._head = newest
59+
self._size += 1
60+
61+
def pop(self):
62+
'''
63+
Removes element from the beginning of the linked list.
64+
That means, pops element that is on the top.
65+
Returns the removed element.
66+
'''
67+
if self.isempty():
68+
print("Stack is Empty. Cannot perform Pop operation.")
69+
return
70+
71+
e = self._head._element
72+
self._head = self._head._link
73+
self._size = self._size - 1
74+
75+
if self.isempty():
76+
self._tail = None
77+
78+
return e
79+
80+
def top(self):
81+
'''
82+
Peeks at the element on the top of the stack.
83+
'''
84+
if self.isempty():
85+
print("Stack is Empty. Cannot perform Pop operation.")
86+
return
87+
88+
e = self._head._element
89+
return e
90+
91+
def display(self):
92+
'''
93+
Utility function to display the Stack.
94+
'''
95+
if self.isempty() == 0:
96+
p = self._head
97+
while p:
98+
print(p._element)
99+
p = p._link
100+
else:
101+
print("Empty")
102+
103+
###############################################################################
104+
105+
def options():
106+
'''
107+
Prints Menu for operations
108+
'''
109+
options_list = ['Push', 'Pop', 'Top',
110+
'Display Stack', 'Exit']
111+
112+
print("MENU")
113+
for i, option in enumerate(options_list):
114+
print(f'{i + 1}. {option}')
115+
116+
choice = int(input("Enter choice: "))
117+
return choice
118+
119+
120+
def switch_case(choice):
121+
'''
122+
Switch Case for operations
123+
'''
124+
os.system('cls')
125+
if choice == 1:
126+
elem = int(input("Enter Item: "))
127+
S.push(elem)
128+
129+
elif choice == 2:
130+
print('Popped item is: ', S.pop())
131+
132+
elif choice == 3:
133+
print("Item on top is: ", S.top())
134+
135+
elif choice == 4:
136+
print("Stack:")
137+
S.display()
138+
print("\n")
139+
140+
elif choice == 5:
141+
import sys
142+
sys.exit()
143+
144+
###############################################################################
145+
146+
147+
S = StackLL()
148+
while True:
149+
choice = options()
150+
switch_case(choice)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /