1. What is a linear data structure?
Stacks, queues, deques, and lists are containers for a class of data. The order of their data items is determined by the order of addition or deletion.
Once a data item is added, it remains in that position relative to the previous and next elements.
Data structures like this are called linear data structures.
2. What is a stack?
A stack (sometimes called a "last in, first out") is an ordered collection of items, where additions and removals of new items always happen on the same end.
This end is often called the "top". The end corresponding to the top is called the "bottom".
Practical application:
Every web browser has a back button. When you browse web pages, these pages are placed in a stack (actually the web page URL).
The page you are viewing now is at the top, and the page you are viewing first is at the bottom. If you press the 'Back' button, the previous page will be browsed in reverse order.
Stack () creates an empty new stack. It takes no parameters and returns an empty stack.
push (item) adds a new item to the top of the stack. It takes item as a parameter and returns nothing.
pop () Removes the top item from the stack. It takes no parameters and returns item. The stack is modified.
peek () returns the top item from the stack, but does not delete it. No parameters required. Does not modify the stack.
isEmpty () Tests if the stack is empty. Takes no parameters and returns a Boolean value.
size () Returns the number of items in the stack. Takes no parameters and returns an integer.
Some related operations of the stack
3. Python implementation stack
class Stack:
def __init __ (self):
self.items = []
def isEmpty (self):
return self.items == []
def push (self, item):
self.items.append (item)
def pop (self):
return self.items.pop ()
def peek (self):
return self.items [-1]
def size (self):
return len (self.items)
s = Stack ()
print (s.isEmpty ()) # True
s.push (4)
s.push (‘dog’)
print (s.peek ()) # dog
s.push (True)
print (s.size ()) # 3
print (s.isEmpty ()) # False
s.push (8.4)
print (s.pop ()) # 8.4
s.pop ()
print (s.size ()) # 2
4. Simple bracket matching
class Stack:
def __init __ (self):
self.items = []
def isEmpty (self):
return self.items == []
def push (self, item):
self.items.append (item)
def pop (self):
return self.items.pop ()
def peek (self):
return self.items [-1]
def size (self):
return len (self.items)
def parChecker (symbolString):
s = Stack ()
balanced = True
index = 0
while index <len (symbolString) and balanced:
symbol = symbolString [index]
if symbol == ‘(‘:
s.push (symbol)
else:
if s.isEmpty ():
balanced = False
else:
s.pop ()
index + = 1
return balanced and s.isEmpty ()
print (parChecker (‘((()) () ()) ′))
print (parChecker (‘() (((() (() ()))) ′))
5. Symbol matching (), [], ()
class Stack:
def __init __ (self):
self.items = []
def isEmpty (self):
return self.items == []
def push (self, item):
self.items.append (item)
def pop (self):
return self.items.pop ()
def peek (self):
return self.items [-1]
def size (self):
return len (self.items)
def parChecker (symbolString):
s = Stack ()
balanced = True
index = 0
while index <len (symbolString) and balanced:
symbol = symbolString [index]
if symbol in ‘([{‘:
s.push (symbol)
else:
if s.isEmpty ():
balanced = False
else:
top = s.pop ()
if not ‘([{’ .index (top) == ‘)]}’. index (symbol):
balanced = False
index + = 1
return balanced and s.isEmpty ()
print (parChecker (‘{({}) {} ([] [])}’))
print (parChecker (‘[(()]‘))
6. Decimal to binary
class Stack:
def __init __ (self):
self.items = []
def isEmpty (self):
return self.items == []
def push (self, item):
self.items.append (item)
def pop (self):
return self.items.pop ()
def peek (self):
return self.items [-1]
def size (self):
return len (self.items)
def divideBy2 (decNumber):
remstack = Stack ()
while decNumber> 0:
rem = decNumber% 2
remstack.push (rem)
decNumber = decNumber // 2
binString = ‘‘
while not remstack.isEmpty ():
binString = binString + str (remstack.pop ())
return binString
print (divideBy2 (11))
print (divideBy2 (42))
7. Decimal to Arbitrary
class Stack:
def __init __ (self):
self.items = []
def isEmpty (self):
return self.items == []
def push (self, item):
self.items.append (item)
def pop (self):
return self.items.pop ()
def peek (self):
return self.items [-1]
def size (self):
return len (self.items)
def divideBy2 (decNumber, base):
‘‘ ‘
Decimal numbers are converted to arbitrary numbers (below 16)
: param decNumber: decimal number
: param base: how many bases to convert to
: return: result
‘‘ ‘
digits = ‘0123456789ABCDEF’ # If the remainder is 13, find D by the 13 index
remstack = Stack ()
while decNumber> 0:
rem = decNumber% base
remstack.push (rem)
decNumber = decNumber // base
newString = ‘‘
while not remstack.isEmpty ():
newString = newString + digits [remstack.pop ()]
return newString
print (divideBy2 (11,8))
print (divideBy2 (42,16))
problem-solving-with-algorithms-and-data-structure-usingpython (using python to solve algorithms and data structures)-basic data structures (1)