当前位置:网站首页>Pointer learning diary (IV) use structure and pointer (linked list)
Pointer learning diary (IV) use structure and pointer (linked list)
2022-07-24 05:14:00 【Rain and cold at night in Xiaoxiang】
typdef struct node
{
struct node *link;
int value;
}Node;
Node* new;// Create a pointer to the structure new
new = (node*)malloc(sizeof(node));
I want to explain this sentence specifically .
First ,new Is a pointer variable to the structure .
secondly ,malloc yes void The pointer , This means that it can be converted to any type of pointer ,
and (node) Is the cast character . Force the return value to Pointer of structure ( And new The same type ), and sizeof(node) Is to allocate this size of memory space to new.
Linked list :
When the teacher taught the pointer in the first semester of freshman , Many students do not understand the use of pointers , Then I only know that pointer is learning C and C++ It's a headache , Of course, some students even talk loudly after class “C and C++ It's entirely possible not to learn , The pointer is so troublesome , Learn later JAVA Isn't the front end fragrant ”. This one , I am also a freshman , We don't know whether it's right or wrong , But as OLE told uncle , Meet with difficulties , Face it with a smile . If the pointer cannot be turned off ,c and c++ Basically, it came to an end , Less c and c++ The company of , I want to say that I must pay a certain price in the future ( Is it a little far away ?) All in all , The first nightmare of many students when they learn data structures as freshmen is linked lists , The linked list is probably the first program for all beginners to use pointers on a large scale .
typedef struct NODE
{
struct NODE *link;
int value;
}Node;//typedef Definer , It can be used directly in the future Node Define new values
#include<stdlib.h>
#include<stdio.h>
/* Let's use insert function to explain here */
/ The code here draws on some 《C And a pointer 》 If you are interested in the teaching code in, you can refer to the original book /
int insert(Node* current,int new_value)
{
Node* previous;
Node* new;
while(current->value < new_value)
{
previous = current;
current = current->link;
}
new = (Node*)malloc(sizeof(Node));
if(new = NULL)
{
return -1;
}
new->value = new_value;
new->link = current;
previous->link = new;
return 1;
}
Do you think this code is good ? wrong , This code has limitations , If the inserted value is smaller than the first value ,
Then the system will report an error ! Because there was no while loop ,previous It is not assigned at all , Indirect access to a NULL The value of is not allowed !
And if you enter a value that is larger than all the values, it is not possible , here current The value pointed to is also NULL, Not for one NULL Indirect access by pointer to !
So we can improve the cycle
while(current->value < new_value&¤t!=NULL)
{
previous = current;
current = current->link;
}
But if you think about it carefully, you will find , This can only solve the second problem , The first problem has not been solved .
We might as well conduct a thorough optimization , Whether it is inserted in the first place or at the end is a special case .
The key to eliminating special situations lies in : We have to realize that , Each node in the linked list has a pointer to it . For the first node , This pointer is the root pointer ; For other nodes , This pointer is the previous node link Field . The point is that each node has a pointer to it !
#include<stdlib.h>
#include<stdio.h>
int insert(Node** linkp,int new_value)//linik Is a pointer to the pointer
{
Node* current;
Node* new;
while((current = *linkp) != NULL && current->value < new_value)
{
linkp = ¤t->link;
// When we call a member in a class or a member in a struct We need to use it -> Operator
}
new = (Node*)malloc(sizeof(Node));
if(new == NULL)
{
return -1;
}
new->value = new_value;
new->link = current;
*linkp = new;
return 1;
}
I still admire this optimization , Assign the address that needs to be changed to linkp, then current Assign a value to new->link, because current->link No change , So nothing will happen to the linked list .
边栏推荐
- Introduction to MapReduce
- Zhaoyi innovation gd25wdxxk6 SPI nor flash product series comes out
- 13. Write a program, in which a user-defined function is used to judge whether an integer is a prime number. The main function inputs a number and outputs whether it is a prime number.
- Chapter VI more supervision training
- frp内网穿透服务使用
- )的低字节来反馈给应用层或者成多种格式文档:
- Several common sorts
- Drools 开发决策表
- Yum to see which installation package provides a command
- JDBC MySQL basic operations
猜你喜欢
![[advanced mathematics] the difference between differentiable and differentiable functions](/img/32/ead52f0d451e3c07a22c7a107fc8b9.jpg)
[advanced mathematics] the difference between differentiable and differentiable functions

This article takes you to understand C string functions and memory functions in simple terms

明星逆市入局的NFT,如何能走出独立行情?

熊市抄底指南

C primer plus learning notes - 6. Arrays and pointers

Quick reference manual for the strongest collation of common regular expressions (glory Collection Edition)

Beginners' preparation for the Blue Bridge Cup (University Programming learning history records, topic ideas for students who need to prepare for the Blue Bridge Cup)

Several common sorts

京东方高级副总裁姜幸群:AIoT技术赋能企业物联网转型

Chapter5 foundation of deep learning
随机推荐
【深度学习】(三)图像分类
MapReduce介绍
Some thoughts about blogger coach soserious
Introduction to 51 single chip microcomputer (dedicated to the most understandable article for beginners)
postgresql:在Docker中运行PostgreSQL + pgAdmin 4
Yum to see which installation package provides a command
编译型语言和解释型语言的区别
All sections need to be able to have a problem system port, the first subscript. Many machines become
Codeforce:d2. remove the substring (hard version) [greedy string + subsequence]
Chapter5 foundation of deep learning
Chapter 0 Introduction to encog
Hcde city closed door meeting successfully held in Nanjing station
MySQL transaction and its problems and isolation level
熊市抄底指南
[basic 7] - exceptions, capture and customize exceptions
Chiitoitsu (expected DP)
Learning pyramid context encoder network for high quality image painting paper notes
power. The operation is in the low peak period of business. Import call will help you prepare each word
Hcip-- review the homework for the next day
)To feed back to the application layer or into multiple format documents: