Data structures and algorithms with python.

Data structures are an integral part of any program that deals with data especially because they deal with the storage of data while the program is in execution. Algorithms on the other hand are a set of step by step instructions that help with processing of data sequentially. Data in programming comes in a variety of ways. Integers for instance are numerical values that do not have a fractional part. Boolean is a set of true or false values whereas strings are the set of all finite sequence of characters. Data structures can be classified into two major categories that is linear or non-linear data structures. Examples of linear data structures include arrays, queues, stacks and linked lists. Non-linear data structures on the other hand include trees.

Lists, tuples and dictionaries are similar in that they are used to store variables. However, as much as the three perform almost the same functions, they are very different from each other in their operations. Lists for instance, are used to store many variables in a single variables. [] are used in their implementation. They are mutable data in that the values in particular positions can be changed even after being keyed in. Tuples on the other hand are very similar to lists but the values cannot be changes hence are immutable. () are used. Dictionaries on the hand, are very different from lists and tuples in that they are organized and accessed using keys and values unlike tuples and lists that are accessed based on the location. This is because they are not ordered items. {} are used. Sets are unordered and unique items therefore they cannot be indexed.{} are used here

```
_Lists_
Mylist = [“car”, [2, 7, 6], [“n”]
print( Mylist)
_Tuples _
mytuple = (“car”, “school”, “book”)
print(mytuple)
```

Stacks are an example of linear data structures that use the LIFO technique that is, last in first out. This means that they only allow addition and deletion from one end. Several computations can be done on stacks such as pop, push, peek, is full and is empty. The peek operation provides the top value of the stack without necessarily removing it. The push operation allows addition of an element to the top of the stack. Pop operation allows the top most data element to be decremented to a lower position hence point to the next value.

```
_Is full ()_
begin procedure is full
if top equals to MAXSIZE
return true
else
return false
end if
end procedure
_Is empty ()_
begin procedure is empty
if top less than 1
return true
else
return false
end if
end procedure
```

Queues on the other hand are a FIFO that is first in first out. Essentially, queues allow insertion of elements from one end and deletion from the other end. Deletion occurs from the front end while insertion can occur at the rear end only and not the vice versa. For an empty queue, the index of both the rear and the front will be -1. Every time an element is inserted, rear is incremented by one. Basic operations in a queue are enqueue and dequeue. Enqueue is adding an element from a queue while dequeue is removing an element from the queue.

```
_Enqueue algorithm_
Procedure enqueue data
if queue is full
return overflow error
end if
rear rear +1
queue[rear] data
return true
end procedure
_Dequeue algorithm_
Procedure dequeuer
If queue is empty
Return underflow error
End if data = queue [front]
front front +1
return true
```

Linked lists are another example of linear data structures. Linked lists re a non-sequential collection of data items. They use pointers to allocate memory. Linked lists are a series of nodes linked together whereby each node has two parts. The first part of the node is the info part and has the element whereas the second part is the link which is a pointer used to link to the next node. Each node is allocated in the list by use of the malloc function. The start of the link which is the first node lacks an element as it only has the pointer. The last node of the list has an element but lacks a link therefore is mostly denoted by X to show that it is null. Basic operations in a linked list are creation, insertion, deletion and traversal. Creation is the basically creating a linked list. Insertion is inserting a new node at a particular position in a linked list. Deletion is to delete a node. It can occur at the beginning, end or any position in the linked list. Traversal is the process of going through nodes in the list. It can be either forward traversal that is traversing from the first node to the last node, or backward traversal that is traversing from the last node to the first node.

## Top comments (0)