# Stacks and Queues

Stacks and Queues are two fundamental data structures often used in Computer Science.

Stacks, like the name suggests, follow Last-In-First-Out (LIFO) principle while Queues work with first come first served approach, in other words with First-In-First-Out (FIFO) principle.

## Stacks Let’s imagine a stack of cargo containers where new containers are added to the top of the stack. Since they are very heavy, only the topmost cargo container can be moved (LIFO). The topmost containers must be removed one by one, to be able to reach the other cargo containers down in the stack.

To implement a stack, we need two basic operations: `push()` and `pop()`

`push()` operation appends an element on top of the stack and `pop()` operation removes an element from the top of the stack. Unlike lists or arrays, stacks do not allow random access to the objects they contain but insert (`push`) and delete (`pop`) operations are very fast with O(1) time complexity.

There are two ways to implement stacks in Python; with using lists1 and with using collections.deque2 class.

### Implementation of Stacks with Using Lists

``````class Stack:
def __init__(self):
self.stack = []

def push(self, val):
return self.stack.append(val)

def pop(self):
if self.is_empty():
return None
else:
return self.stack.pop()

def size(self):
return len(self.stack)

def is_empty(self):
return self.size() == 0
``````

### Implementation of Stacks with Using collections.deque class

``````from collections import deque

class Stack:
def __init__(self):
self.stack = deque()

def push(self, val):
return self.stack.append(val)

def pop(self):
if self.is_empty():
return None
else:
return self.stack.pop()

def size(self):
return len(self.stack)

def is_empty(self):
return self.size() == 0
``````

## Queues

The simplest analogy of a queue data structure is, the line of people that forms in front of a supermarket checkout counter. The first person to arrive at that queue will be the first person to be done with it (FIFO). New additions to the line are made to the back of the queue while removal happens in the front of the queue when the first person pays for her groceries and take them to her home. To implement a queue, we again need two basic operations: `enqueue()` and `dequeue()`

`enqueue()` operation appends an element to the end of the queue and `dequeue()` operation removes an element from the beginning of the queue.

Unlike lists or arrays, queues do not allow random access to the objects they contain but insert (`enqueue`) and delete (`dequeue`) operations are very fast with O(1) time complexity.

### Implementation of Queues

``````from collections import deque

class Queue:
def __init__(self):
self.queue = deque()

def enqueue(self, val):
return self.queue.append(val)

def dequeue(self):
if self.is_empty():
return None
else:
return self.queue.popleft()

def size(self):
return len(self.queue)

def is_empty(self):
return self.size() == 0
``````
1. Python Official Documentation, Using Lists as Stacks
2. Python Official Documentation, collections.deque class
{"ft_lang_label":"__label__en","ft_lang_prob":0.76428217,"math_prob":0.8136867,"size":2986,"snap":"2020-34-2020-40","text_gpt3_token_len":668,"char_repetition_ratio":0.15023474,"word_repetition_ratio":0.18262807,"special_character_ratio":0.2273945,"punctuation_ratio":0.14113598,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9636597,"pos_list":[0,1,2,3,4,5,6],"im_url_duplicate_count":[null,3,null,3,null,3,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-09-24T02:41:24Z\",\"WARC-Record-ID\":\"<urn:uuid:9aa69207-a310-43bb-a2db-10619d2f9c21>\",\"Content-Length\":\"30904\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:cd041f67-2b11-41a2-b14b-e349265091b2>\",\"WARC-Concurrent-To\":\"<urn:uuid:e2612b8f-f1ca-4c96-a35a-2758f942f7cc>\",\"WARC-IP-Address\":\"185.199.110.153\",\"WARC-Target-URI\":\"https://emre.me/data-structures/stacks-and-queues/\",\"WARC-Payload-Digest\":\"sha1:ZPLKQ3A65APXKVZPXQLUYFRGT3IM4F43\",\"WARC-Block-Digest\":\"sha1:U6SVQOH7OZDETHHWANH6P2WAX2O5ESQ6\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-40/CC-MAIN-2020-40_segments_1600400213006.47_warc_CC-MAIN-20200924002749-20200924032749-00725.warc.gz\"}"}