


What is the data structure in c language? What are the common data structures?
In the C language, a data structure refers to a collection of data elements that have one or more specific relationships with each other. It is a way for computers to store and organize data; common data structures include: linear data structures ( Array, linked list, stack, queue and linear list), tree structure (binary tree, complete binary tree, binary search tree, heap), graph structure (directed graph and undirected graph).
The operating environment of this tutorial: windows7 system, c99 version, Dell G3 computer.
What is a data structure?
Data structure is the way computers store and organize data. A data structure refers to a collection of data elements that have one or more specific relationships with each other
The implementation of most data structures requires the help of pointers and structure types in the C language
Next, let’s get to today’s focus. O(∩_∩)O Several common data structures
(1) Linear data structure: There is generally a one-to-one relationship between elements, which is the most commonly used A type of data structure, typically: arrays, stacks, queues and linear tables
(2) Tree structure: There is a hierarchical relationship between nodes, and a node in each layer can and can only be combined with A node on the upper layer is related, but it can be related to multiple nodes on the next layer at the same time, which is called a "one-to-many" relationship. Common types are: tree, heap
(3) Graphic structure : In the graph structure, multiple nodes are allowed to be related, which is called a "many-to-many" relationship
The following is a brief introduction to each of these data structures:
1. Linear data structures: Typical ones are: arrays, stacks, queues and linear tables
(1) Arrays and linked lists
a. Array: stores a group of data of the same type. The length of the array needs to be specified in advance, such as one-dimensional array, two-dimensional array, multi-dimensional array, etc.
b. Linked list: Linked list is a type of C language A widely used structure, it is implemented in the form of dynamically allocated memory, using a set of arbitrary storage units to store a linked list of data elements. Generally, a pointer field is added for each element to point to subsequent elements
c, arrays The difference from a linked list:
From a logical structure perspective: arrays must have a fixed length defined in advance and cannot adapt to the dynamic increase or decrease of data; linked lists dynamically allocate storage and can adapt to the dynamic increase or decrease of data. situation, and can easily insert and delete data items (when inserting or deleting data items in the array, other data items need to be moved)
From the memory storage point of view: (static) the array allocates space from the stack (using NEW is created in the heap), which is convenient and fast for programmers, but has a small degree of freedom; linked lists allocate space from the heap, which has a large degree of freedom but is troublesome to apply for and manage.
From the perspective of access methods: the array is in memory It is stored continuously, so you can use subscript indexes for random access; the linked list is a linked storage structure, and elements can only be accessed sequentially from front to back in a linear manner, so the access efficiency is lower than that of arrays
(2) Stack, queue and linear table: sequential storage and chain storage methods can be used for storage
Sequential storage: with the help of the relative position of data elements in the storage space Position to represent the logical relationship between elements
Chain storage: Use pointers representing the storage addresses of data elements to represent the logical relationship between elements
a. Stack: only allowed at the end of the sequence Operations, stack operations can only be performed on the top of the stack. Generally, the stack is also called a last-in-first-out or first-in-last-out linear structure
Sequential stack: A stack using a sequential storage structure is called a sequential stack, that is, it needs A space with consecutive addresses is used to store the elements of the stack. The type of the sequential stack is defined as follows:
Chain stack: A stack using a chain storage structure is called a chain stack:
b. Queue: Only operations are allowed at both ends of the sequence. Generally, the queue is also called a first-in, first-out linear structure.
Circular queue: A sequential storage structure is used. Queue needs to allocate storage space according to the maximum possible length of the queue. Its type is defined as follows:
Chain queue: A queue using a chain storage structure is called a chain queue. Generally, it needs Setting the head and tail pointers is just the head and tail nodes of the linked list:
c. Linear table: allows operations at any position in the sequence. The operation position of the linear table is not restricted. Table operations are very flexible. Common operations include inserting and deleting at any location, and querying and modifying elements at any location
Sequential table: A linear table represented by a sequential storage structure is called a sequential table. A set of storage units with consecutive addresses is used to store the data elements of the linear table at one time, that is, the adjacent storage locations represent the sequence between two elements. In order to avoid moving elements, in order to avoid moving elements, generally only inserting and deleting elements at the end of the table is considered in the interface definition of the sequence table. The sequence table implemented in this way can also be called a stack table:
Linear lists: generally include singly linked lists, doubly linked lists, circular linked lists and two-way circular linked lists
Singly linked lists:
Double linked lists :
Comparison of two storage structures of linear tables:
Sequential tables:
Advantages: In sequential tables, logic is relatively Two adjacent elements are also physically adjacent, making it easier to search. The time complexity of accessing any element is O(1)
. Disadvantages: Not suitable for inserting or deleting elements at any position. Because elements need to be moved, the average time complexity is O(n)
Linked list:
Advantages: To insert or delete an element at any position of the link, you only need to modify the corresponding pointer, and there is no need to move the element; Dynamic allocation on demand, no need to pre-allocate a continuous empty space according to the maximum demand
Disadvantages: It is inconvenient to search. To find an element, you need to start from the head pointer and search along the pointer field, so the average time complexity is O(n)
2. Tree structure: There is a hierarchical relationship between nodes. A node in each layer can and can only be related to one node in the previous layer, but it can also be related to multiple nodes in the next layer. Nodes are related, which is called a "one-to-many" relationship. Common types are: tree, heap
(1) Binary tree: A binary tree is a recursive data structure that contains n (n>=0) nodes. A finite set of points. A binary tree has the following characteristics:
A binary tree can be an empty tree; each node of a binary tree has exactly two subtrees, one or two of which may be empty; each node in a binary tree The positions of the left and right subtrees cannot be reversed. If the positions of the two are changed, it will become another binary tree
(2) Complete binary tree: starting from the root, top to bottom, left to right, Each node of a full binary tree is numbered consecutively from 1 to n. If each node corresponds one-to-one with the nodes numbered from 1 to n in the full binary tree of depth k, it is called a complete binary tree
a. Use a sequential storage structure: use a one-dimensional array to store a complete binary tree. The number of the node is related to the subscript of the node (for example, the root is 1, then the left child of the root is 2i=21=2, and the right child is 2i 1=21 1=2)
(6) Heap: A heap is a complete binary tree with the following characteristics: all non-leaf nodes are no larger than (or no smaller than) its left and right child nodes. If all non-leaf nodes in the heap are not larger than their left and right child nodes, it is called a small top heap (small root heap). If all non-leaf nodes in the heap are not smaller than their left and right child nodes, it is called a large heap. Top heap (big root heap)
(7) Union-find: Union-find refers to a set composed of a set of disjoint subsets, denoted as: S= {S1,S2,S3,…,Sn}
(8) B-tree
3. Graph structure: In the graph structure, correlation between multiple nodes is allowed. It is called a "many-to-many" relationship and can be divided into directed graphs and undirected graphs
Tutorial recommendation: "c language tutorial video"
The above is the detailed content of What is the data structure in c language? What are the common data structures?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



C language data structure: The data representation of the tree and graph is a hierarchical data structure consisting of nodes. Each node contains a data element and a pointer to its child nodes. The binary tree is a special type of tree. Each node has at most two child nodes. The data represents structTreeNode{intdata;structTreeNode*left;structTreeNode*right;}; Operation creates a tree traversal tree (predecision, in-order, and later order) search tree insertion node deletes node graph is a collection of data structures, where elements are vertices, and they can be connected together through edges with right or unrighted data representing neighbors.

The truth about file operation problems: file opening failed: insufficient permissions, wrong paths, and file occupied. Data writing failed: the buffer is full, the file is not writable, and the disk space is insufficient. Other FAQs: slow file traversal, incorrect text file encoding, and binary file reading errors.

C language multithreading programming guide: Creating threads: Use the pthread_create() function to specify thread ID, properties, and thread functions. Thread synchronization: Prevent data competition through mutexes, semaphores, and conditional variables. Practical case: Use multi-threading to calculate the Fibonacci number, assign tasks to multiple threads and synchronize the results. Troubleshooting: Solve problems such as program crashes, thread stop responses, and performance bottlenecks.

How to output a countdown in C? Answer: Use loop statements. Steps: 1. Define the variable n and store the countdown number to output; 2. Use the while loop to continuously print n until n is less than 1; 3. In the loop body, print out the value of n; 4. At the end of the loop, subtract n by 1 to output the next smaller reciprocal.

Algorithms are the set of instructions to solve problems, and their execution speed and memory usage vary. In programming, many algorithms are based on data search and sorting. This article will introduce several data retrieval and sorting algorithms. Linear search assumes that there is an array [20,500,10,5,100,1,50] and needs to find the number 50. The linear search algorithm checks each element in the array one by one until the target value is found or the complete array is traversed. The algorithm flowchart is as follows: The pseudo-code for linear search is as follows: Check each element: If the target value is found: Return true Return false C language implementation: #include#includeintmain(void){i

C Language Data Structure: Overview of the Key Role of Data Structure in Artificial Intelligence In the field of artificial intelligence, data structures are crucial to processing large amounts of data. Data structures provide an effective way to organize and manage data, optimize algorithms and improve program efficiency. Common data structures Commonly used data structures in C language include: arrays: a set of consecutively stored data items with the same type. Structure: A data type that organizes different types of data together and gives them a name. Linked List: A linear data structure in which data items are connected together by pointers. Stack: Data structure that follows the last-in first-out (LIFO) principle. Queue: Data structure that follows the first-in first-out (FIFO) principle. Practical case: Adjacent table in graph theory is artificial intelligence

Troubleshooting Tips for C language processing files When processing files in C language, you may encounter various problems. The following are common problems and corresponding solutions: Problem 1: Cannot open the file code: FILE*fp=fopen("myfile.txt","r");if(fp==NULL){//File opening failed} Reason: File path error File does not exist without file read permission Solution: Check the file path to ensure that the file has check file permission problem 2: File reading failed code: charbuffer[100];size_tread_bytes=fread(buffer,1,siz

C language functions are reusable code blocks, receive parameters for processing, and return results. It is similar to the Swiss Army Knife, powerful and requires careful use. Functions include elements such as defining formats, parameters, return values, and function bodies. Advanced usage includes function pointers, recursive functions, and callback functions. Common errors are type mismatch and forgetting to declare prototypes. Debugging skills include printing variables and using a debugger. Performance optimization uses inline functions. Function design should follow the principle of single responsibility. Proficiency in C language functions can significantly improve programming efficiency and code quality.
