Data Structures Tutorial

Last Updated : 28 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow
Show Topics
Topics:
Solve Problem
Easy
91.84%
16.2K

Data structures are the fundamental building blocks of computer programming. They define how data is organized, stored, and manipulated within a program. Understanding data structures is very important for developing efficient and effective algorithms.

In this tutorial, we will explore the most commonly used data structures, including arrays, linked lists, stacks, queues, trees, and graphs.

Data Structures Tutorial

What is Data Structure?

A data structure is a storage that is used to store and organize data. It is a way of arranging data on a computer so that it can be accessed and updated efficiently.

A data structure is not only used for organizing the data. It is also used for processing, retrieving, and storing data. There are different basic and advanced types of data structures that are used in almost every program or software system that has been developed. So we must have good knowledge about data structures. 

Get Hands-on With Data Structures and Algorithms

Master fundamental computer science concepts to solve real-world problems and ace coding interview questions with Educative’s interactive course Data Structures and Algorithms in Python. Sign up at Educative.io with the code GEEKS10 to save 10% on your subscription.

Classification of Data Structure

Classification of Data Structure

  1. Linear Data Structure: Data structure in which data elements are arranged sequentially or linearly, where each element is attached to its previous and next adjacent elements, is called a linear data structure. 
    Example: Array, Stack, Queue, Linked List, etc.
  2. Static Data Structure: Static data structure has a fixed memory size. It is easier to access the elements in a static data structure. 
    Example: array.
  3. Dynamic Data Structure: In dynamic data structure, the size is not fixed. It can be randomly updated during the runtime which may be considered efficient concerning the memory (space) complexity of the code. 
    Example: Queue, Stack, etc.
  4. Non-Linear Data Structure: Data structures where data elements are not placed sequentially or linearly are called non-linear data structures. In a non-linear data structure, we can’t traverse all the elements in a single run only. 
    Examples: Trees and Graphs.

Data Structures Tutorial

Introduction to Data Structures

Array Data Structure

Linked List Data Structure

1. Singly Linked List:

2. Circular Linked List:

3. Doubly Linked List:

Matrix Data Structure

Stack Data Structure

Queue Data Structure

Binary Tree Data Structure

Binary Search Tree Data Structure

Heap Data Structure

Hashing Data Structure

Graph Data Structure

Advanced Data Structure

1. Advanced Lists:

2. Segment Tree Data Structure:

All Articles on Segment Tre

3. Trie Data Structure:

All Articles on Trie

4. Binary Indexed Tree Data Structure:

All Articles on Binary Indexed Tree

5. Suffix Array and Suffix Tree:

All Articles on Suffix Tree

6. AVL Tree:

7. Splay Tree:

8. B Tree:

9. Red-Black Tree:

All Articles on Self-Balancing BSTs

10. K Dimensional Tree:

Others Data Structures:

Misc:



Previous Article
Next Article

Similar Reads

Does a Data Scientist/Machine Learning Engineer require in depth knowledge of Data Structures and Algorithms?
In today's world, data scientists and machine learning engineers play a crucial role in analyzing data and building intelligent systems. As technology continues to advance, the demand for these experts is growing rapidly. Real-world data problems are complex, requiring strong skills in handling data and creating efficient algorithms. In this articl
10 min read
Data Structures | Linked List | Question 1
What does the following function do for a given Linked List with first node as head? void fun1(struct node* head) { if(head == NULL) return; fun1(head->next); printf(\"%d \", head->data); } (A) Prints all nodes of linked lists (B) Prints all nodes of linked list in reverse order (C) Prints alternate nodes of Linked List (D) Prints alternate n
2 min read
Data Structures | Stack | Question 1
Following is C like pseudo code of a function that takes a number as an argument, and uses a stack S to do processing. void fun(int n) { Stack S; // Say it creates an empty stack S while (n > 0) { // This line pushes the value of n%2 to stack S push(&S, n%2); n = n/2; } // Run while Stack S is not empty while (!isEmpty(&S)) printf("
1 min read
Data Structures | Queue | Question 1
Following is C like pseudo-code of a function that takes a Queue as an argument, and uses a stack S to do processing. C/C++ Code void fun(Queue *Q) { Stack S; // Say it creates an empty stack S // Run while Q is not empty while (!isEmpty(Q)) { // deQueue an item from Q and push the dequeued item to S push(&S, deQueue(Q)); } // Run while Stack S is
1 min read
Data Structures | Stack | Question 2
Which one of the following is an application of Stack Data Structure? (A) Managing function calls (B) The stock span problem (C) Arithmetic expression evaluation (D) All of the above Answer: (D) Explanation: See http://en.wikipedia.org/wiki/Stack_(abstract_data_type)#Applications
1 min read
Data Structures | Linked List | Question 2
Which of the following points is/are true about Linked List data structure when it is compared with array? (A) Arrays have better cache locality that can make them better in terms of performance. (B) It is easy to insert and delete elements in Linked List (C) Random access is not allowed in a typical implementation of Linked Lists (D) The size of a
2 min read
Data Structures | Linked List | Question 3
Consider the following function that takes reference to head of a Doubly Linked List as parameter. Assume that a node of doubly linked list has previous pointer as prev and next pointer as next. C/C++ Code void fun(struct node **head_ref) { struct node *temp = NULL; struct node *current = *head_ref; while (current != NULL) { temp = current->prev; c
2 min read
Data Structures | Queue | Question 2
Which one of the following is an application of Queue Data Structure? (A) When a resource is shared among multiple consumers. (B) When data is transferred asynchronously (data not necessarily received at same rate as sent) between two processes (C) Load Balancing (D) All of the above Answer: (D) Explanation: (A) When a resource is shared among mult
2 min read
Data Structures | Binary Trees | Question 1
Which of the following is true about Binary Trees? (A) Every binary tree is either complete or full. (B) Every complete binary tree is also a full binary tree. (C) Every full binary tree is also a complete binary tree. (D) No binary tree is both complete and full. (E) None of the above Answer: (E)Explanation: A full binary tree (sometimes proper bi
2 min read
Data Structures | Tree Traversals | Question 1
Following function is supposed to calculate the maximum depth or height of a Binary tree -- the number of nodes along the longest path from the root node down to the farthest leaf node. int maxDepth(struct node* node) { if (node==NULL) return 0; else { /* compute the depth of each subtree */ int lDepth = maxDepth(node->left); int rDepth = maxDep
1 min read
Data Structures | Binary Trees | Question 15
If arity of operators is fixed, then which of the following notations can be used to parse expressions without parentheses? a) Infix Notation (Inorder traversal of a expression tree) b) Postfix Notation (Postorder traversal of a expression tree) c) Prefix Notation (Preorder traversal of a expression tree) (A) b and c (B) Only b (C) a, b and c (D) N
1 min read
Data Structures | Tree Traversals | Question 2
What is common in three different types of traversals (Inorder, Preorder and Postorder)? (A) Root is visited before right subtree (B) Left subtree is always visited before right subtree (C) Root is visited after left subtree (D) All of the above (E) None of the above Answer: (B) Explanation: The order of inorder traversal is LEFT ROOT RIGHT The ord
1 min read
Data Structures | Tree Traversals | Question 3
The inorder and preorder traversal of a binary tree are d b e a f c g and a b d e c f g, respectively. The postorder traversal of the binary tree is: (A) d e b f g c a (B) e d b g f c a (C) e d b f g c a (D) d e f g b c a Answer: (A) Explanation: Below is the given tree. a / \ / \ b c / \ / \ / \ / \ d e f g Quiz of this Question
1 min read
Data Structures | Binary Trees | Question 3
What are the main applications of tree data structure? Manipulate hierarchical data Make information easy to search Manipulate sorted lists of data Router algorithms Form of a multi-stage decision-making, like Chess Game. As a workflow for compositing digital images for visual effects (A) 1, 2, 3, 4 and 6 (B) 1, 2, 3, 4 and 5 (C) 1, 3, 4, 5 and 6 (
1 min read
Data Structures | Binary Trees | Question 4
Level of a node is distance from root to that node. For example, level of root is 1 and levels of left and right children of root is 2. The maximum number of nodes on level i of a binary tree is In the following answers, the operator '^' indicates power. (A) 2^(i-1) (B) 2^i (C) 2^(i+1) (D) 2^[(i+1)/2] Answer: (A) Explanation: Number of nodes of bin
1 min read
Data Structures | Binary Trees | Question 15
In a complete k-ary tree, every internal node has exactly k children or no child. The number of leaves in such a tree with n internal nodes is: (A) nk (B) (n – 1) k+ 1 (C) n( k – 1) + 1 (D) n(k – 1) Answer: (C) Explanation: For an k-ary tree where each node has k children or no children, following relation holds L = (k-1)*n + 1 Where L is the numbe
1 min read
Data Structures | Tree Traversals | Question 4
What does the following function do for a given binary tree? int fun(struct node *root) { if (root == NULL) return 0; if (root->left == NULL && root->right == NULL) return 0; return 1 + fun(root->left) + fun(root->right); } (A) Counts leaf nodes (B) Counts internal nodes (C) Returns height where height is defined as number of ed
2 min read
Data Structures | Linked List | Question 4
Which of the following sorting algorithms can be used to sort a random linked list with minimum time complexity? (A) Insertion Sort (B) Quick Sort (C) Heap Sort (D) Merge Sort Answer: (D)Explanation: Both Merge sort and Insertion sort can be used for linked lists. The slow random-access performance of a linked list makes other algorithms (such as q
1 min read
Data Structures | Queue | Question 3
How many stacks are needed to implement a queue. Consider the situation where no other data structure like arrays, linked list is available to you. (A) 1 (B) 2 (C) 3 (D) 4 Answer: (B) Explanation: A queue can be implemented using two stacks. Refer this for more reference:https://www.geeksforgeeks.org/queue-using-stacks/ Hence Option(B) is the corre
1 min read
Data Structures | Linked List | Question 5
The following function reverse() is supposed to reverse a singly linked list. There is one line missing at the end of the function. C/C++ Code /* Link list node */ struct node { int data; struct node* next; }; /* head_ref is a double pointer which points to head (or start) pointer of linked list */ static void reverse(struct node** head_ref) { stru
1 min read
Data Structures | Binary Search Trees | Question 1
What is the worst case time complexity for search, insert and delete operations in a general Binary Search Tree for a skewed tree ? (A) O(n) for all (B) O(Logn) for all (C) O(Logn) for search and insert, and O(n) for delete (D) O(Logn) for search, and O(n) for insert and delete Answer: (A)Explanation: In skewed Binary Search Tree (BST), all three o
1 min read
Data Structures | Binary Search Trees | Question 2
In delete operation of BST, we need inorder successor (or predecessor) of a node when the node to be deleted has both left and right child as non-empty. Which of the following is true about inorder successor needed in delete operation? (A) Inorder Successor is always a leaf node (B) Inorder successor is always either a leaf node or a node with empt
2 min read
Data Structures | Tree Traversals | Question 5
Which of the following pairs of traversals is not sufficient to build a binary tree from the given traversals? (A) Preorder and Inorder (B) Preorder and Postorder (C) Inorder and Postorder (D) None of the Above Answer: (B) Explanation: See https://www.geeksforgeeks.org/if-you-are-given-two-traversal-sequences-can-you-construct-the-binary-tree/ for
1 min read
Data Structures | Linked List | Question 6
What is the output of following function in which start is pointing to the first node of the following linked list 1->2->3->4->5->6 ? C/C++ Code void fun(struct node* start) { if(start == NULL) return; printf(\"%d \", start->data); if(start->next != NULL ) fun(start->next->next); printf(\"%d \", start->data); } (A) 1 4 6 6 4 1 (B) 1
1 min read
Data Structures | Queue | Question 5
A priority queue can efficiently implemented using which of the following data structures? Assume that the number of insert and peek (operation to see the current highest priority item) and extraction (remove the highest priority item) operations are almost same. (A) Array (B) Linked List (C) Heap Data Structures like Binary Heap, Fibonacci Heap (D
1 min read
Data Structures | Stack | Question 3
Which of the following is true about linked list implementation of stack? (A) In push operation, if new nodes are inserted at the beginning of linked list, then in pop operation, nodes must be removed from end. (B) In push operation, if new nodes are inserted at the end, then in pop operation, nodes must be removed from the beginning. (C) Both of t
1 min read
Data Structures | Queue | Question 6
Which of the following is true about linked list implementation of queue? (A) In push operation, if new nodes are inserted at the beginning of linked list, then in pop operation, nodes must be removed from end. (B) In push operation, if new nodes are inserted at the end, then in pop operation, nodes must be removed from the beginning. (C) Both of t
1 min read
Data Structures | Graph | Question 1
Which of the following is an advantage of adjacency list representation over adjacency matrix representation of a graph? (A) In adjacency list representation, space is saved for sparse graphs. (B) DFS and BSF can be done in O(V + E) time for adjacency list representation. These operations take O(V^2) time in adjacency matrix representation. Here is
1 min read
Data Structures | Queue | Question 11
Suppose a circular queue of capacity (n – 1) elements is implemented with an array of n elements. Assume that the insertion and deletion operation are carried out using REAR and FRONT as array index variables, respectively. Initially, REAR = FRONT = 0. The conditions to detect queue full and queue empty are (A) Full: (REAR+1) mod n == FRONT, empty:
2 min read
Data Structures | Balanced Binary Search Trees | Question 2
The worst case running time to search for an element in a balanced in a binary search tree with n2^n elements is (A) [Tex]\\Theta(n log n) [/Tex](B) [Tex]\\Theta (n2^n) [/Tex](C) [Tex]\\Theta (n) [/Tex](D) [Tex]\\Theta (log n) [/Tex] (A) A (B) B (C) C (D) D Answer: (C)Explanation: Time taken to search an element is [Tex]\\Theta (h) [/Tex]where h is
1 min read