Skip to content

codebinge/Data-Structures

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

96 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Data-Structures Build Status Coverage Status

Implementation of simple data structures in Python.


Linked List

A singly linked list is made of nodes which contain a reference (or pointer) to the next node in the list and data. They are one of the simpliest data structures and can be used to implement other abstract data types including lists, stacks, queues etc.

linked list

Advatange of a Linked list is a dynamic data structure which can grow while program is running (unlike arrays). Insertion and deletion methods are easy to implement, and it is a simple building block for other more complex data structures.

The disadvantages of using a linked list is they use more memory than an array. Nodes must be read in order from the head to the tail (sequential access). Hard to reverse traverse a single linked list.

The list implementation supports the following methods:

Method Description Time Complexity
push(val) insert the value ‘val’ at the head of the list. O(1)
pop() removes the first value off the head of the list and return it O(1)
size() return the length of the list O(1)
search(val) return the node containing ‘val’ in the list, if present, else None O(n)
remove(node) remove the given node from the list, wherever it might be (node must be an item in the list) O(n)
display() return a unicode string representing the list as if it were a Python tuple literal: “(12, ‘sam’, 37, ‘tango’)” O(n)

Stack

A stack is a collection of nodes with operations occuring at one end only. It behaves like a real-world stack or pile of books - books can only be taken from or placed on the top of the pile in a First In Last Out (LIFO) operations.

stack

Stacks are handy for remembering state eg undo and redo

The Stack implementation supports the following methods:

Method Description Time Complexity
push(val) insert the value ‘val’ at the head of the stack. O(1)
pop() removes the first value off the head of the stack and return it O(1)

Double Linked List

A doubly linked list is made of nodes which contain a reference (or pointer) to the next node in the list, and the previous node in the list plus data.

doubly linked list

Advatange of a doubly linked list is two directional pointers allow traversal of the list in either direction.

The disadvantages of using a doubly linked list is they use more memory than a singly linked list and adding or removing a node requires changing more pointers.

The list implementation supports the following methods:

Method Description Time Complexity
push(val) will insert the value ‘val’ at the head of the list. O(1)
pop() will pop the first value off the head of the list and return it O(1)
append(val) will insert the value ‘val’ at the tail of the list. O(1)
shift() will pop the first value off the tail of the list and return it O(1)
remove(val) will remove the first instance of ‘val’ found in the list, starting from the head. If ‘val’ is not present, it will raise an appropriate Python exception. O(n)

Queue

A queue is an ordered collection of nodes with operations only allowing the addition of new nodes to the tail and removing nodes from the head of the collection. The Queue is called a First In First Out (FIFO) data structure for this reason.

Queue

Queues are used in computer science exactly like they are in the physical world, that is, they are a buffer and store data until it will later get processed. They have limited access (only insert at the tail) and they can always be added to (the length is unlimited).

The Queue implementation supports the following methods:

Method Description Time Complexity
enqueue(value) adds value to the queue O(1)
dequeue() removes the correct item from the queue and returns its value (should raise an error if the queue is empty) O(1)
peek() returns the next value in the queue without dequeueing it. If the queue is empty, returns None O(1)
size() return the size of the queue. Should return 0 if the queue is empty O(1)

Deque

Deque is a queue that can be accessed from both ends.

Deque

Deques are useful when modeling any kind of real-world waiting line. This is where entities (bits, people, cars, words, particles etc) arrive with a certain frequency and the front of the line is serviced at a different frequency.

The Deque implementation supports the following methods:

Method Description Time Complexity
append(val) adds value to the end of the deque O(1)
appendleft(val) adds a value to the front of the deque O(1)
pop() removes a value from the end of the deque and returns it (raises an exception if the deque is empty) O(1)
popleft() removes a value from the front of the deque and returns it (raises an exception if the deque is empty) O(1)
peek() returns the next value that would be returned by pop but leaves the value in the deque (returns None if the deque is empty) O(1)
peekleft() returns the next value that would be returned by popleft but leaves the value in the deque (returns None if the deque is empty) O(1)
size() returns the count of items in the queue (returns 0 if the queue is empty)
 |   O(1)        |

About

Implementation of simple data structures in Python

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 100.0%