A data structure organizes related data in a computer so that it can be used efficiently.
Different kinds of data structures are suited to different kinds of applications, and some are highly specialized to specific tasks. For example, graphs are used to represent connections on social websites (such as Twitter, Facebook), trees are used to represent hierarchical data, such as files and folders in a filesystem, queues can be used to create task schedulers, compiler implementations usually use hash tables to look up identifiers, binary trees are used in searching algorithms, etc.
Usually, choosing the correct data structure is key to designing an efficient algorithm.
Some of the most common data structures are:
- Linked List
- Stack, Queue
- Graph, Tree
- Hash Table
Some data structures can be used to create others, for example, linked lists can be used to create stacks and queues.
"Each data structure has a set of operations that can be performed on the data it stores. We select a data structure based on the operations required to implement an algorithm."
ArrayArray, is a data structure consisting of a collection of elements (values or variables), each identified by at least one array index or key. An array is stored so that the position of each element can be computed from its index tuple by a mathematical formula. The simplest type of data structure is a linear array, also called one-dimensional array.
For example, an array of 10 32-bit integer variables, with indices 0 through 9, may be stored as 10 words at memory addresses 2000, 2004, 2008, ... 2036, so that the element with index i has the address 2000 + 4 × i.
Linked ListThe linked list data structure is often used to implement other data structures.
A linked list is a sequence of nodes where each node stores its own data and a pointer (address) to the location of the next node.
One node links to another forming what can be thought of as a linked chain:
The last item in the list has a link to NULL, indicating the end of the chain.
Although a linked list is similar to an array, it is not restricted to a declared number of elements. Additionally, unlike an array which stores data contiguously in memory or on disk, a linked list can easily insert or remove elements without reallocation or reorganization of the entire structure because the data items need not be stored contiguously.
Linked List Drawbacks:
1) Random access is not allowed. We must access nodes sequentially starting from the first one. Therefore, we cannot do a binary search on a linked list.
2) Extra memory space for a link is required for each element of the list.
"Linked lists are generally used as the fundamental building block for implementing data structures such as stacks, queues, trees, and graphs."
A stack is a simple data structure that adds and removes elements in a particular order.
Every time an element is added, it goes on the "top" of the stack. Only an element at the top of the stack can be removed, just like a stack of plates. This behavior is called LIFO (Last In, First Out).
Adding a new element onto the stack is called push.
Removing an element from the stack is called pop.
Stacks can be used to create undo-redo functionalities, parsing expressions (infix to postfix/prefix conversion), and much more.
"A stack can be implemented using an array or a linked list."
QueueA queue is a simple data structure that allows elements to be inserted from one end, called the rear (also called tail), and deleted from the other end, called the front (also called head).
This behavior is called FIFO (First in First Out).
The process of adding new elements into the queue is called enqueue.
The process of removal of an element from the queue is called dequeue.
Queues are used whenever we need to manage objects in order starting with the first one in.
Scenarios include printing documents on a printer, call center systems answering people on hold people, and so on.
"A queue can be implemented using an array or a linked list."
GraphGraphs are used to represent many real-life applications like networks, transportation paths of a city, and social networks such as Facebook where each person represents a single node. Graphs are useful in many applications of computer science as well.
A graph is a set of connected nodes where each node is called a Vertex and the connection between two of them is called an Edge. We define a Graph as a data structure that consists of finite vertices and edges.
There are two types of graphs, undirected (A) and directed (B):
Image A is an example of an undirected graph. Here, the edges do not have directions, meaning that if the vertex u is connected to v, then v is connected to u.
Undirected graphs usually are drawn with straight lines between the vertices (no arrows).
Example: On Facebook, if user A is a friend of user B, that automatically means that user B is a friend of user A.
Image B is an example of a directed graph. Here, each edge has a defined direction, which is usually represented with arrows. Having u connected to v does not necessarily mean that v is connected to u.
Example: On Twitter, if user A follows user B, then user B does not necessarily follow user A back.
Two of the most commonly used implementations of graphs are:
1. Adjacency Matrix
2. Adjacency List
Check out the implementations below!
Masukan artikel kamu 4