Data Structure - Data structure is a mechanism to organize data or collection of data to achieve target result in minimal processing of data for given problem statement.
1. Time Complexity - It refers to amount of time an algorithm takes to solve a given problem.
2. Space Complexity - It refers to amount of ram/cache memory an algorithm takes to solve a given problem.
Algorithm - Algorithm is set of steps/instructions applied on data or organized data(Using some Data Structure) to produce target result. In other words it is logic or approach written in sequential steps to solve given problem based on data.
In daily life we encounter many problems which are based on some data. To tackle these problems we process data for solution. To process data in simple way for given problem many times we need suitable data structure for smooth processing. Below picture illustrates Data Structure and Algorithm.
Example 1. A document is collection of words which is sequence of characters. Once some wrong sentence/word/character is written, there should be provision to undo or redo operation.
Here undo and redo operation will happen on the content(character set) which is written at last. Means undo/redo will start from the last character added/modified to the document. So we need some mechanism to store characters of document, so that undo/redo operation could be performed on latest data(character added modified at last). Here last entered character is processed first. In computer science word to implement such mechanism Stack(Last In First Out LIFO) data structure is used. Below figure depicts the Undo and Redo operation mentioned in above example. Here File data is organized in two Stacks Undo Stack and Redo Stack.
1.a Example of Undo operation:
Algorithm for Undo operation is given below:
Undo(charCount)
{
For i=1 to charCount
{
If(Undo Stack is not empty)
{
Char c=removeItemFromTopOfUndoStack()
pushItemOnRedoStack(c)
}
}
formatAndShowContentOfUndoStackToFile()
}
1.b Example of Redo operation:
Algorithm for Redo operation is given below:
Redo(charCount)
{
For i=1 to charCount
{
If(Redo Stack is not empty)
{
Char c=removeItemFromTopOfRedoStack()
pushItemOnUndoStack(c)
}
}
formatAndShowContentOfUndoStackToFile()
}
Example 2. Issuing food coupons for game participants.
Here the participant which comes first gets first coupon. For second participant second coupon is issued and so on. So here first participant is processed first(By assigning first ticket). In computer science world this type of data structure is called Queue(First In First Out FIFO). Below figure depicts the coupon processing of mentioned example.
Selection of data structure differs according to data processing requirement based on problem statement. Any one can define its own data structure according to requirement.
Below are some pre defined data structures which can be utilized alone or with combination to solve real life problems.