CS- Week 5

Apr 03, 2025 pm 11:06 PM
c language key value pair typedef

Detailed explanation of data structure: from array to tree, and then to hash table

This article discusses several common data structures in depth, including arrays, linked lists, binary search trees (BSTs) and hash tables, and explains their organization in memory and their advantages and disadvantages.

Information structure and abstract data structure

Information structure refers to the way information is organized in memory, while abstract data structures are our conceptual understanding of these structures. Understanding abstract data structures helps us better implement various data structures in practice.


Stack and Queue

Queues are an abstract data structure that follows the FIFO (first in, first out) principle, similar to waiting in line. Its main operations include enqueuing (adding elements to the tail of the queue) and dequeuing (removing the head elements of the queue).

The stack follows the LIFO (Last In First Out) principle, just like stacking a plate. Its operations include pushing (adding elements to the top of the stack) and popping (removing top elements of the stack).


Array

An array is a structure that continuously stores data in memory. As shown in the figure below, arrays occupy continuous storage space in memory.

CS- Week 5

Other programs, functions, and variables may exist in memory, as well as redundant data that has been used before. If you need to add new elements to the array, you need to reallocate memory and copy the entire array, which can be inefficient.

CS- Week 5CS- Week 5CS- Week 5

Although pre-allocating too much memory can reduce copy operations, it will waste system resources. Therefore, it is crucial to allocate memory according to actual needs.


Link List

Linked lists are powerful data structures that allow concatenation of values ​​located in different memory regions into a list and support dynamic expansion or reduction.

CS- Week 5

Each CS- Week 5 contains two values: the data value and a pointer to the next CS- Week 5. The pointer value of the last CS- Week 5 is NULL, indicating the end of the linked list.

CS- Week 5CS- Week 5

In C language, CS- Week 5s can be defined as follows:

 <code class="c">typedef struct CS- Week 5 { int number; struct CS- Week 5 *next; } CS- Week 5;</code>
Copy after login

The following example shows the process of creating a linked list:

CS- Week 5CS- Week 5CS- Week 5CS- Week 5CS- Week 5CS- Week 5CS- Week 5CS- Week 5

Disadvantages of linked lists include the need for additional memory storage pointers and the inability to directly access elements through indexes.


Binary Search Tree (BST)

A binary search tree is a tree structure that efficiently stores, searches and retrieves data.

CS- Week 5CS- Week 5CS- Week 5

The advantages of BST are dynamic and search efficiency (O(log n)), and the disadvantage is that the search efficiency drops to O(n) when the tree is unbalanced and requires additional memory storage pointers.


Hash table

A hash table is similar to a dictionary and contains key-value pairs. It uses a hash function to map keys to array indexes, thus achieving the average lookup time of O(1).

CS- Week 5

Hash conflicts (multiple keys mapped to the same index) can be resolved by linked lists or other methods. The design of hash functions is crucial to the performance of hash tables. An example of a simple hash function is as follows:

 <code class="c">#include <ctype.h> unsigned int hash(const char *word) { return toupper(word[0]) - 'A'; }</ctype.h></code>
Copy after login

This article is compiled based on cs50x 2024 source code.

The above is the detailed content of CS- Week 5. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the method of converting Vue.js strings into objects? What is the method of converting Vue.js strings into objects? Apr 07, 2025 pm 09:18 PM

Using JSON.parse() string to object is the safest and most efficient: make sure that strings comply with JSON specifications and avoid common errors. Use try...catch to handle exceptions to improve code robustness. Avoid using the eval() method, which has security risks. For huge JSON strings, chunked parsing or asynchronous parsing can be considered for optimizing performance.

C language data structure: data representation and operation of trees and graphs C language data structure: data representation and operation of trees and graphs Apr 04, 2025 am 11:18 AM

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 behind the C language file operation problem The truth behind the C language file operation problem Apr 04, 2025 am 11:24 AM

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.

How to distinguish between closing a browser tab and closing the entire browser using JavaScript? How to distinguish between closing a browser tab and closing the entire browser using JavaScript? Apr 04, 2025 pm 10:21 PM

How to distinguish between closing tabs and closing entire browser using JavaScript on your browser? During the daily use of the browser, users may...

How debian readdir integrates with other tools How debian readdir integrates with other tools Apr 13, 2025 am 09:42 AM

The readdir function in the Debian system is a system call used to read directory contents and is often used in C programming. This article will explain how to integrate readdir with other tools to enhance its functionality. Method 1: Combining C language program and pipeline First, write a C program to call the readdir function and output the result: #include#include#include#includeintmain(intargc,char*argv[]){DIR*dir;structdirent*entry;if(argc!=2){

HadiDB: A lightweight, horizontally scalable database in Python HadiDB: A lightweight, horizontally scalable database in Python Apr 08, 2025 pm 06:12 PM

HadiDB: A lightweight, high-level scalable Python database HadiDB (hadidb) is a lightweight database written in Python, with a high level of scalability. Install HadiDB using pip installation: pipinstallhadidb User Management Create user: createuser() method to create a new user. The authentication() method authenticates the user's identity. fromhadidb.operationimportuseruser_obj=user("admin","admin")user_obj.

Is the URL requested by Vue Axios correct? Is the URL requested by Vue Axios correct? Apr 07, 2025 pm 10:12 PM

Yes, the URL requested by Vue Axios must be correct for the request to succeed. The format of url is: protocol, host name, resource path, optional query string. Common errors include missing protocols, misspellings, duplicate slashes, missing port numbers, and incorrect query string format. How to verify the correctness of the URL: enter manually in the browser address bar, use the online verification tool, or use the validateStatus option of Vue Axios in the request.

How to use the redis command How to use the redis command Apr 10, 2025 pm 08:45 PM

Using the Redis directive requires the following steps: Open the Redis client. Enter the command (verb key value). Provides the required parameters (varies from instruction to instruction). Press Enter to execute the command. Redis returns a response indicating the result of the operation (usually OK or -ERR).

See all articles