Abstract data structure doesn't access computer memory diretely but access internal data structure so it's less prone to errors. We can say stack
,queue
, and linked list
are abstract data structure. Since almost all the programming language doesn't have these structures user has to implement it when needed.
Stack
of meal tray. Using a tray you can use one on the top, then the one right under it and so on. At the very end the last one to use is on the bottom of the tray stack
. The first data of the stack
is bottom and the last one is called top.
Insert(push)
Insert a data at the end of the structure.
Delete(pop)
Delete top(the last data of the structure)
Read
You can read top(the last data of the structure) only.
class Stack:
def __init__(self):
self.stack = []
def push(self, data):
self.stack.append(data)
print(stack)
def pop(self):
last = self.read()
self.stack = self.stack[:-1]
return last
def read(self):
return self.stack[-1]
This is queue
. You have to stand in line to buy movie tickets, the turn goes from the front to the back. When your turn comes, you can buy your tickets and some snacks. The first data of the queue
is front and the last one is back.
Insert(enqueue)
Insert a data at the end of the structure.
Delete(dequeue)
Delete front(the first data of the structure)
read
You can read front(the first data of the structure) only
class Queue:
def __init__(self):
self.queue = []
def enqueue(self, data):
self.queue.append(data)
print(self.queue)
def dequeue(self):
first = self.read()
self.queue = self.queue[1:]
return first
def read(self):
if len(self.queue) == 0:
return None
return self.queue[0]
I think it's more of rule for accessing internal data structure than a data structure.
class Brace_Linter(Stack):
def __init__(self):
super().__init__()
self.inputs = None
self.no_input_flag = False
def get_inputs(self):
self.inputs = input().split()
if len(self.inputs) == 0:
self.no_input_flag = True
else:
self.inputs = self.inputs[0]
def check(self):
if self.no_input_flag is False:
for i in self.inputs:
if (i == "(" or i == "["or i== "{"):
self.push(i)
if (i == ")" or i == "]" or i== "}"):
if ord(self.read()) == ord(i)-1:
self.pop()
else:
raise ValueError('Braces are not consistent')
if len(self.stack) == 0:
return True
else:
raise ValueError('Brace has been opend but not closed properly')
else:
raise ValueError('No input has been given')
class Request_Dispatcher(Queue):
def __init__(self):
super().__init__()
self.patcher = []
def hold_request(self, document):
self.enqueue(document)
def dispatch(self):
while self.read():
print(self.dequeue())