Summary


Linked List

Linked lists can be thought of as dynamic arrays which means that the size does not need to be specified and can keep growing as long as we still have memory in our RAM.

A linked list consist of a node, which is just like a single block of memory in an array, and in the node we can store data. The node also consist of a 'next' pointer which points to the next node in the linked list. The next pointers are essentially what connects the linked list together. In a doubly linked list, we have an extra 'prev' pointer which points to the previous node in the linked list. We also need a head pointer which points to the first node of the linked list. We can also use a tail pointer which points to the last node of the linked list, but this is optional.

A linked list contains of several operations such as, push head, push tail, insert after, delete, print and many more. Here is a program which contains several of those operations:

Stack

A stack is the a way of storing data with the principle of First In Last Out. This means that the last data which gets in the stack will be the first one the get removed. Stacks have 3 main operations which are: push, pop, and peek. Push is the same as inserting data while pop is removing data. Peek is return the top data in the stack. We can implement the principle of stacks both using arrays and linked list. If we use arrays, our stack size is limited. If we use linked list, we do not have a limit in the size of the stack we will use memory due to the presence of pointers.

A program that implement stacks using Arrays:



A program that implement stacks using Linked List:



We can use stacks for converting infix to postfix notation or prefix notation, and also evaluating prefix and postfix notations. Stacks are also used for Depth First Search algorithms.

Queue

Queue is similar to stacks but only with a different principle: First In First Out. So the first data that enters the queue will be the first one to be removed. Instead of push and pop, queues' operations is called enqueue and dequeue. We can also implement queues in arrays and linked list.

A program that implement queues using Arrays:



A program that implement queues using Linked List:



Queues can be used for Breadth First Search algorithms.

Hash Tables

Hash tables is a way to store and search data with a very fast time complexity. If we have a very good hash function, we can get a search time complexity close to O(1). A hash function processes the key of the data from the input and turn it into a hashed key which points to the index in the hash table array. This way, if we want to search for the data, we use the same hash function to output the same hashed key index and immediately find the data in O(1). There are many hash functions such as: mid-square, division, folding, digit extraction and rotating hash. However, a problem arises when there is collision or when 2 different data has the same hashed key from the hash function. There are 2 ways to handle collision: linear probing and chaining. Linear probing will basically place the item with the same hashed key in the next empty slot in the hash table array. Chaining is a much better collision handler which uses linked list. If there an item with the same hash key, just point the old item to the new item with the same hashed key, creating a linked list. Therefore, chaining will require us to use an array of linked list.

Here is a program which implements hash table, a simple hash function, and chaining:



Binary Search Tree

Binary Search Tree is a data structure which has an average search complexity of O(log n). This average time complexity is usually very accurate unlike hash tables which can diverge greatly from O(1) to O(n). The principle of binary tree is: every element smaller than the current node of the tree goes to the left subtree, whereas elements greater will go the right subtree. BST uses the concept of linked list but instead of a 'next' pointer, we have a 'left' and right' pointer.  BST has 3 main operations which are: insert, delete, search. Printing a binary search tree can be done in preorder, postorder, or in order. Operations of BST is mostly done recursively, that is why most functions have a struct Node* return type.

This is a program which implements the primary functions of BST:





Comments

Popular posts from this blog

Hash Tables and Trees

AVL TREES & B-TREES

Binary Search Tree