Data Structure and Structured Type in Data Structure

 

             Data Structure and Structured Type

A Data Structure delivers a structured set of variables related to each other in various ways. It forms the basis of a programming tool that signifies the relationship between the data elements and allows programmers to process the data efficiently.

We can classify Data Structures into two categories:

  1. Primitive Data Structure
  2. Non-Primitive Data Structure

The following figure shows the different classifications of Data Structures.


Classifications of Data Structures

Primitive Data Structures

  1. Primitive Data Structures are the data structures consisting of the numbers and the characters that come in-built into programs.
  2. These data structures can be manipulated or operated directly by machine-level instructions.
  3. Basic data types like Integer, Float, Character, and Boolean come under the Primitive Data Structures.
  4. These data types are also called Simple data types, as they contain characters that can't be divided further

Non-Primitive Data Structures

  1. Non-Primitive Data Structures are those data structures derived from Primitive Data Structures.
  2. These data structures can't be manipulated or operated directly by machine-level instructions.
  3. The focus of these data structures is on forming a set of data elements that is either homogeneous (same data type) or heterogeneous (different data types).
  4. Based on the structure and arrangement of data, we can divide these data structures into two sub-categories -
    1. Linear Data Structures
    2. Non-Linear Data Structures

Linear Data Structures

A data structure that preserves a linear connection among its data elements is known as a Linear Data Structure. The arrangement of the data is done linearly, where each element consists of the successors and predecessors except the first and the last data element. However, it is not necessarily true in the case of memory, as the arrangement may not be sequential.

Based on memory allocation, the Linear Data Structures are further classified into two types:

  1. Static Data Structures: The data structures having a fixed size are known as Static Data Structures. The memory for these data structures is allocated at the compiler time, and their size cannot be changed by the user after being compiled; however, the data stored in them can be altered.
    The Array is the best example of the Static Data Structure as they have a fixed size, and its data can be modified later.
  2. Dynamic Data Structures: The data structures having a dynamic size are known as Dynamic Data Structures. The memory of these data structures is allocated at the run time, and their size varies during the run time of the code. Moreover, the user can change the size as well as the data elements stored in these data structures at the run time of the code.
    Linked Lists, Stacks, and Queues are common examples of dynamic data structures

Types of Linear Data Structures

The following is the list of Linear Data Structures that we generally use:

1. Arrays

An Array is a data structure used to collect multiple data elements of the same data type into one variable. Instead of storing multiple values of the same data types in separate variable names, we could store all of them together into one variable. This statement doesn't imply that we will have to unite all the values of the same data type in any program into one array of that data type. But there will often be times when some specific variables of the same data types are all related to one another in a way appropriate for an array.

An Array is a list of elements where each element has a unique place in the list. The data elements of the array share the same variable name; however, each carries a different index number called a subscript. We can access any data element from the list with the help of its location in the list. Thus, the key feature of the arrays to understand is that the data is stored in contiguous memory locations, making it possible for the users to traverse through the data elements of the array using their respective indexes.


An Array

Arrays can be classified into different types:

  1. One-Dimensional Array: An Array with only one row of data elements is known as a One-Dimensional Array. It is stored in ascending storage location.
  2. Two-Dimensional Array: An Array consisting of multiple rows and columns of data elements is called a Two-Dimensional Array. It is also known as a Matrix.
  3. Multidimensional Array: We can define Multidimensional Array as an Array of Arrays. Multidimensional Arrays are not bounded to two indices or two dimensions as they can include as many indices are per the need.
  1. We can store a list of data elements belonging to the same data type.
  2. Array acts as an auxiliary storage for other data structures.
  3. The array also helps store data elements of a binary tree of the fixed count.
  4. Array also acts as a storage of matrices.

2. Linked Lists

Linked List is another example of a linear data structure used to store a collection of data elements dynamically. Data elements in this data structure are represented by the Nodes, connected using links or pointers. Each node contains two fields, the information field consists of the actual data, and the pointer field consists of the address of the subsequent nodes in the list. The pointer of the last node of the linked list consists of a null pointer, as it points to nothing. Unlike the Arrays, the user can dynamically adjust the size of a Linked List as per the requirements.

An Introduction to Data Structures

A Linked List

Linked Lists can be classified into different types:

  1. Singly Linked List: A Singly Linked List is the most common type of Linked List. Each node has data and a pointer field containing an address to the next node.
  2. Doubly Linked List: A Doubly Linked List consists of an information field and two pointer fields. The information field contains the data. The first pointer field contains an address of the previous node, whereas another pointer field contains a reference to the next node. Thus, we can go in both directions (backward as well as forward).
  3. Circular Linked List: The Circular Linked List is similar to the Singly Linked List. The only key difference is that the last node contains the address of the first node, forming a circular loop in the Circular Linked List.
  1. The Linked Lists help us implement stacks, queues, binary trees, and graphs of predefined size.
  2. We can also implement Operating System's function for dynamic memory management.
  3. Linked Lists also allow polynomial implementation for mathematical operations.
  4. We can use Circular Linked List to implement Operating Systems or application functions that Round Robin execution of tasks.
  5. Circular Linked List is also helpful in a Slide Show where a user requires to go back to the first slide after the last slide is presented.
  6. Doubly Linked List is utilized to implement forward and backward buttons in a browser to move forward and backward in the opened pages of a website.

3. Stacks

Stack is a Linear Data Structure that follows the LIFO (Last In, First Out) principle that allows operations like insertion and deletion from one end of the Stack, i.e., Top. Stacks can be implemented with the help of contiguous memory, an Array, and non-contiguous memory, a Linked List. Real-life examples of Stacks are piles of books, a deck of cards, piles of money, and many more.

An Introduction to Data Structures

Figure 5. A Real-life Example of Stack

The above figure represents the real-life example of a Stack where the operations are performed from one end only, like the insertion and removal of new books from the top of the Stack. It implies that the insertion and deletion in the Stack can be done only from the top of the Stack. We can access only the Stack's tops at any given time.

The primary operations in the Stack are as follows:

  1. Push: Operation to insert a new element in the Stack is termed as Push Operation.
  2. Pop: Operation to remove or delete elements from the Stack is termed as Pop Operation.
An Introduction to Data Structures

Figure 6. A Stack

Some Applications of Stacks:

  1. The Stack is used as a Temporary Storage Structure for recursive operations.
  2. Stack is also utilized as Auxiliary Storage Structure for function calls, nested operations, and deferred/postponed functions.
  3. We can manage function calls using Stacks.
  4. Stacks are also utilized to evaluate the arithmetic expressions in different programming languages.
  5. Stacks are also helpful in converting infix expressions to postfix expressions.
  6. Stacks allow us to check the expression's syntax in the programming environment.
  7. We can match parenthesis using Stacks.
  8. Stacks can be used to reverse a String.
  9. Stacks are helpful in solving problems based on backtracking.
  10. We can use Stacks in depth-first search in graph and tree traversal.
  11. Stacks are also used in Operating System functions.
  12. Stacks are also used in UNDO and REDO functions in an edit.

4. Queues

Queue is a linear data structure similar to a Stack with some limitations on the insertion and deletion of the elements. The insertion of an element in a Queue is done at one end, and the removal is done at another or opposite end. Thus, we can conclude that the Queue data structure follows FIFO (First In, First Out) principle to manipulate the data elements. Implementation of Queues can be done using Arrays, Linked Lists, or Stacks. Some real-life examples of Queues are a line at the ticket counter, an escalator, a car wash, and many more.

No comments:

Post a Comment