# Data Structure – codewindow.in

## Related Topics ## Data Structure

#### A stack can be implemented using an array or a linked list. Here is an example implementation of a stack using an array:

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

def push(self, item):
self.stack.append(item)

def pop(self):
if not self.is_empty():
return self.stack.pop()

def peek(self):
if not self.is_empty():
return self.stack[-1]

def is_empty(self):
return len(self.stack) == 0

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

#### A queue can be implemented using an array or a linked list. Here is an example implementation of a queue using a linked list:

``````class Node:
def __init__(self, data):
self.data = data
self.next = None

class Queue:
def __init__(self):
self.front = None
self.rear = None

def enqueue(self, item):
new_node = Node(item)
if self.rear is None:
self.front = self.rear = new_node
return
self.rear.next = new_node
self.rear = new_node

def dequeue(self):
if self.front is None:
return
temp = self.front
self.front = temp.next
if self.front is None:
self.rear = None

def is_empty(self):
return self.front is None
``````

#### For example, consider the following binary tree and binary search tree:

``````     8                     5
/ \                   / \
3   10                3   7
/ \   \               / \   \
1   6   14            1   4   14
``````

#### The first tree is a regular binary tree, whereas the second tree is a binary search tree. Note that in the binary search tree, the left subtree of node 5 contains only values less than 5, and the right subtree contains only values greater than or equal to 5. This ordering property is not present in the regular binary tree. #### Top Company Questions  #### Questions on Chapter 1  ## We Loveto Support you

Go through our study material. Your Job is awaiting.