Data structure is a fundamental concept of any programming language, essential for algorithmic design.
It is used for the efficient organization and modification of data.
DS is how data and the relationship amongst different data is represented, that aids in how efficiently various functions or operations or algorithms can be applied.
什么是数据结构?
数据结构是任何编程语言的基本概念,对算法设计至关重要。
它用于有效地组织和修改数据。
DS 是如何表示数据和不同数据之间的关系的,它有助于提高各种功能或操作或算法的应用效率。
Types
There are two types of data structures:
Linear data structure: If the elements of a data structure result in a sequence or a linear list then it is called a linear data structure. Example: Arrays, Linked List, Stacks, Queues etc.
Non-linear data structure: If the elements of data structure results in a way that traversal of nodes is not done in a sequential manner, then it is a non linear data structure. Example: Trees, Graphs etc.
Data structures form the core foundation of software programming as any efficient algorithm to a given problem is dependent on how effectively a data is structured.
Identifiers look ups in compiler implementations are built using hash tables.
The B-trees data structures are suitable for the databases implementation.
Some of the most important areas where data structures are used are as follows:
Artificial intelligence
Compiler design
Machine learning
Database design and management
Blockchain
Numerical and Statistical analysis
Operating system development
Image & Speech Processing
Cryptography
应用
数据结构构成了软件编程的核心基础,因为针对给定问题的任何有效算法都取决于数据结构的有效性。
编译器实现中的标识符查找是使用哈希表构建的。
B 树数据结构适用于数据库实现。
使用数据结构的一些最重要的领域如下:
人工智能
编译器设计
机器学习
数据库设计与管理
区块链
数值和统计分析
操作系统开发
图像和语音处理
密码学
Benefits of Learning Data Structures
Any given problem has constraints on how fast the problem should be solved (time) and how much less resources the problem consumes(space). That is, a problem is constrained by the space and time complexity within which it has to be solved efficiently.
In order to do this, it is very much essential for the given problem to be represented in a proper structured format upon which efficient algorithms could be applied.
Selection of proper data structure becomes the most important step before applying algorithm to any problem.
Having knowledge of different kinds of data structures available helps the programmer in choosing which data structure suits the best for solving a problem efficiently. It is not just important to make a problem work, it is important how efficiently you make it work.
The core concepts of data structures remains the same across all the programming languages. Only the implementation differs based on the syntax or the structure of the programming language.
The implementation in procedural languages like C is done with the help of structures, pointers, etc.
In an objected oriented language like Java, data structures are implemented by using classes and objects.
Having sound knowledge of the concepts of each and every data structures helps you to stand apart in any interviews as selecting right data structure is the first step towards solving problem efficiently.
1. Can you explain the difference between file structure and storage structure?
File Structure: Representation of data into secondary or auxiliary memory say any device such as hard disk or pen drives that stores data which remains intact until manually deleted is known as a file structure representation.
Storage Structure: In this type, data is stored in the main memory i.e RAM, and is deleted once the function that uses this data gets completely executed.
The difference is that storage structure has data stored in the memory of the computer system, whereas file structure has the data stored in the auxiliary memory.
2. Can you tell how linear data structures differ from non-linear data structures?
If the elements of a data structure result in a sequence or a linear list then it is called a linear data structure. Whereas, traversal of nodes happens in a non-linear fashion in non-linear data structures.
Lists, stacks, and queues are examples of linear data structures whereas graphs and trees are the examples of non-linear data structures.
Arrays are the collection of similar types of data stored at contiguous memory locations.
It is the simplest data structure where the data element can be accessed randomly just by using its index number.
3. 什么是数组?
数组是存储在连续内存位置的类似类型数据的集合。
它是最简单的数据结构,只需使用索引号就可以随机访问数据元素。
4. What is a multidimensional array?
Multi-dimensional arrays are those data structures that span across more than one dimension.
This indicates that there will be more than one index variable for every point of storage. This type of data structure is primarily used in cases where data cannot be represented or stored using only one dimension. Most commonly used multidimensional arrays are 2D arrays.
2D arrays emulates the tabular form structure which provides ease of holding the bulk of data that are accessed using row and column pointers.
A linked list is a data structure that has sequence of nodes where every node is connected to the next node by means of a reference pointer. The elements are not stored in adjacent memory locations. They are linked using pointers to form a chain. This forms a chain-like link for data storage.
Each node element has two parts:
a data field
a reference (or pointer) to the next node.
The first node in a linked list is called the head and the last node in the list has the pointer to NULL. Null in the reference field indicates that the node is the last node. When the list is empty, the head is a null reference.
Linked lists can be considered both linear and non-linear data structures. This depends upon the application that they are used for.
When linked list is used for access strategies, it is considered as a linear data-structure. When they are used for data storage, it can be considered as a non-linear data structure.
7. How are linked lists more efficient than arrays?
Insertion and Deletion
Insertion and deletion process is expensive in an array as the room has to be created for the new elements and existing elements must be shifted.
But in a linked list, the same operation is an easier process, as we only update the address present in the next pointer of a node.
Dynamic Data Structure
Linked list is a dynamic data structure that means there is no need to give an initial size at the time of creation as it can grow and shrink at runtime by allocating and deallocating memory.
Whereas, the size of an array is limited as the number of items is statically stored in the main memory.
No wastage of memory
As the size of a linked list can grow or shrink based on the needs of the program, there is no memory wasted because it is allocated in runtime.
In arrays, if we declare an array of size 10 and store only 3 elements in it, then the space for 3 elements is wasted. Hence, chances of memory wastage is more in arrays.
8. Explain the scenarios where you can use linked lists and arrays.
Following are the scenarios where we use linked list over array:
When we do not know the exact number of elements beforehand.
When we know that there would be large number of add or remove operations.
Less number of random access operations.
When we want to insert items anywhere in the middle of the list, such as when implementing a priority queue, linked list is more suitable.
Below are the cases where we use arrays over the linked list:
When we need to index or randomly access elements more frequently.
When we know the number of elements in the array beforehand in order to allocate the right amount of memory.
When we need speed while iterating over the elements in the sequence.
When memory is a concern:
Due to the nature of arrays and linked list, it is safe to say that filled arrays use less memory than linked lists.
Each element in the array indicates just the data whereas each linked list node represents the data as well as one or more pointers or references to the other elements in the linked list.
To summarize, requirements of space, time, and ease of implementation are considered while deciding which data structure has to be used over what.
8. 解释可以使用链表和数组的场景。
以下是我们在数组上使用链表的场景:
当我们事先不知道元素的确切数量时。
当我们知道会有大量的添加或删除操作时。
更少的随机访问操作。
当我们想在链表中间的任意位置插入项目时,比如实现优先级队列时,链表更适合。
以下是我们在链表上使用数组的情况:
当我们需要更频繁地索引或随机访问元素时。
当我们事先知道数组中的元素数量以便分配适量的内存时。
当我们在迭代序列中的元素时需要速度时。
当内存是一个问题时:
由于数组和链表的性质,可以肯定地说填充数组比链表使用更少的内存。
数组中的每个元素仅表示数据,而每个链表节点表示数据以及一个或多个指针或对链表中其他元素的引用。
总而言之,在决定必须使用哪种数据结构时,需要考虑空间、时间和易于实施的要求。
9. What is a doubly-linked list (DLL)? What are its applications.
This is a complex type of a linked list wherein a node has two references:
One that connects to the next node in the sequence
Another that connects to the previous node.
This structure allows traversal of the data elements in both directions (left to right and vice versa).
Applications of DLL are:
A music playlist with next song and previous song navigation options.
The browser cache with BACK-FORWARD visited pages
The undo and redo functionality on platforms such as word, paint etc, where you can reverse the node to get to the previous page.
9. 什么是双向链表(DLL)?它的应用是什么。
这是一种复杂类型的链表,其中一个节点有两个引用:
一个连接到序列中的下一个节点
另一个连接到前一个节点。
这种结构允许在两个方向(从左到右,反之亦然)遍历数据元素。
DLL 的应用有:
带有下一首歌曲和上一首歌曲导航选项的音乐播放列表。
具有 BACK-FORWARD 访问页面的浏览器缓存
word,paint 等平台上的撤消和重做功能,您可以在其中反转节点以到达上一页。
10. What is a stack? What are the applications of stack?
Stack is a linear data structure that follows LIFO (Last In First Out) approach for accessing elements.
Push, pop, and top (or peek) are the basic operations of a stack.
Following are some of the applications of a stack:
Check for balanced parentheses in an expression
Evaluation of a postfix expression
Problem of Infix to postfix conversion
Reverse a string
10. 什么是堆栈?堆栈的应用有哪些?
堆栈是一种线性数据结构,遵循 LIFO(后进先出)方法来访问元素。
Push、pop 和 top(或 peek)是堆栈的基本操作。
以下是堆栈的一些应用:
检查表达式中的平衡括号
后缀表达式的评估
中缀到后缀转换的问题
反转字符串
11. What is a queue? What are the applications of queue?
A queue is a linear data structure that follows the FIFO (First In First Out) approach for accessing elements.
Dequeue from the queue, enqueue element to the queue, get front element of queue, and get rear element of queue are basic operations that can be performed.
Some of the applications of queue are:
CPU Task scheduling
BFS algorithm to find shortest distance between two nodes in a graph.
Website request processing
Used as buffers in applications like MP3 media player, CD player, etc.