search
HomeDatabaseMysql Tutorial04.线性表(三)链式存储结构.单链表2

链式存储结构.单链表2 顺序存储结构的创建实质是一个数组的初始化,存储空间连续且其大小和类型已经固定;单链表存储空间不连续,是一种动态结构且它所占用空间的大小和位置是不需要预先分配划定的,可以根据系统的情况和实际的需求即时生成。 一.单链表的整

链式存储结构.单链表2
顺序存储结构的创建实质是一个数组的初始化,存储空间连续且其大小和类型已经固定;单链表存储空间不连续,是一种动态结构且它所占用空间的大小和位置是不需要预先分配划定的,可以根据系统的情况和实际的需求即时生成。 一.单链表的整表创建 创建单链表的过程就是一个动态生成链表的过程,即从“空表”的初始化起,依次建立各元素结点,并逐个插入链表。 1.算法思路 (1)声明一个结点p和计数器变量i; (2)初始化一空链表L (3)让链表L的头结点的指针指向NULL,即建立一个带头结点的单链表 *L=(LinkList)malloc(sizeof(Node)); (*L)->next=NULL; (4)循环: a.生成一个新结点赋值给p; b.随机生成一个数字赋值给p的数据域p->data; c.将p插入到头结点之前与一新结点之间 2.源码实现 (1)头插法:始终让新结点在第一的位置 /*随机产生n个元素的值,建立带头结点的单链线性表L(头插法)*/ typedef struct Node *LinkList; //定义LinkList void CreateListHead(LinkList *L,int n) { LinkList p; int i; srand(time(0)); //初始化随机数种子 /*1.建立一个带头结点的单链表*/ *L=(LinkList)malloc(sizeof(Node)); //初始化一空链表L (*L)->next=NULL; //让链表L的头结点的指针指向NULL /*2.循环插入新结点到单链表中*/ for(i=0;idata=rand()%100+1; //设置新结点的数据域 p->next=(*L)->next; //设置新结点的指针域:使p结点的后继结点为头指针之前指向的结点 (*L)->next=p; //更改头结点的后继结点为新结点p } } (2)后插法:把每次新结点都插在终端结点的后面 /*随机产生n个元素的值,建立带头结点的单链线性表L(尾插法)*/ typedef struct Node *LinkList; //定义LinkList void CreateListTail(LinkList *L,int n) { LinkList p,r; int i; srand(time(0)); //初始化随机数种子 /*1.建立一个带头结点的单链表并设置尾结点*/ *L=(LinkList)malloc(sizeof(Node)); //初始化一空链表L,L指整个单链表 r=*L; //r为指向尾部的结点 /*2.循环插入新结点到单链表中*/ for(i=0;idata=rand()%100+1; //设置新结点的数据域 r->next=p; //将表尾终端结点的指针指向新结点 r=p; //将当前的新结点定义为表尾终端结点 } r->next=NULL; //此时的尾结点为p,相当于p->next=null,,表示当前链表结束 } 注释:注意L与r的关系,L是指整个单链表,而r是指向尾结点的变量;r会随着循环不断地变化结点,而L则是随着循环增长为一个多结点的链表。

升华笔记: 1.如何创建一个带头结点的单链表? (1)初始化一空链表L (2)让链表L的头结点的指针指向NULL 源码实现: LinkList *L; *L=(LinkList)malloc(sizeof(Node)); (*L)->next=NULL; 2.头插法如何实现插入新结点? (1)生成一个新结点 (2)设置新结点的数据域 (3)设置新结点的指针域 (4)将新结点作为上一个结点的后继结点 源码实现:p=(LinkList)malloc(sizeof(Node)); p->data=e; //e为数据 p->next=(*L)->next; //设置新结点的指针域:使p结点的后继结点为头指针之前指向的结点 (*L)->next=p; //更改头结点的后继结点为新结点p 3.尾差法如何实现插入新结点? (1)声明一个结点r,并将其设置为链表L的尾部结点 (2)生成一个新结点p (3)设置新结点的数据域 (4)将表尾终端结点的指针指向新结点,并将新结点设置为尾部结点 (5)设置新的表位结点指针指向NULL,表示当前链表结束 源码实现:LinkList *L,p; *L=(LinkList)malloc(sizeof(Node)); r=*L; p=(LinkList)malloc(sizeof(N【本文来自鸿网互联 (http://www.68idc.cn)】ode)); p->data=rand()%100+1; r->next=p; r=p;
二.单链表的整表删除
1.算法思路
(1)声明一个结点p和q; (2)将单链表的第一个结点赋值给p (3)循环: a.将下一结点赋值给q b.释放p c.将q赋值给p 2.源码实现: /*初始化条件:单链式线性表L已存在,操作结果:将单链表L重置为空表*/ typedef struct Node *LinkList; //定义LinkList
typedef int Status;
Status ClearList(LinkList *L) { LinkList p,q; p=(*L)->next; //将单链表的第一个结点赋值给结点p while(p) { q=p->next; //将p的下一个结点设置为q free(p); //释放结点p p=q; //p指向下一结点q } (*L)->next=NULL; //最后,单链表头结点指针域为空 returm OK; } 三.单链表结构与顺序存储结构的优缺点 1.存储分配方式 (1)顺序存储结构用一段联系的存储单元依次存储线性表的数据元素 (2)单链表采用链式存储结构,用一组任意的存储单元存放线性表的元素 2.时间性能 (1)查找:顺序存储结构O(1);单链表O(n) (2)删除和插入:顺序存储结构需要平均移动表长一半的元素,时间为O(n);单链表在指出某位置的指针后,插入和删除时间仅为O(1) 3.空间性能 (1)顺序存储结构需要预分配存储空间,分大了,浪费,分小了易发生溢出; (2)单链表不需要分配存储空间,只要有就可以分配,元素个数不受限制

Statement
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
Explain the InnoDB Buffer Pool and its importance for performance.Explain the InnoDB Buffer Pool and its importance for performance.Apr 19, 2025 am 12:24 AM

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

MySQL vs. Other Programming Languages: A ComparisonMySQL vs. Other Programming Languages: A ComparisonApr 19, 2025 am 12:22 AM

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

Learning MySQL: A Step-by-Step Guide for New UsersLearning MySQL: A Step-by-Step Guide for New UsersApr 19, 2025 am 12:19 AM

MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

MySQL: Essential Skills for Beginners to MasterMySQL: Essential Skills for Beginners to MasterApr 18, 2025 am 12:24 AM

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL: Structured Data and Relational DatabasesMySQL: Structured Data and Relational DatabasesApr 18, 2025 am 12:22 AM

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL: Key Features and Capabilities ExplainedMySQL: Key Features and Capabilities ExplainedApr 18, 2025 am 12:17 AM

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

The Purpose of SQL: Interacting with MySQL DatabasesThe Purpose of SQL: Interacting with MySQL DatabasesApr 18, 2025 am 12:12 AM

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

MySQL for Beginners: Getting Started with Database ManagementMySQL for Beginners: Getting Started with Database ManagementApr 18, 2025 am 12:10 AM

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA

See all articles

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

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools