Note: After creating a node, be sure to destroy the node.
Reference address:Singly linked list application function
#include
#include
typedef struct node{
dated;
structnode *next;
} node_t;
// Create node function
void *create(int size);
// Initialize the linked list
int init(node_t *head);
// Header insertion method
int insert_head(node_t *head,node_t *pn);
// Tail insertion method
int insert_end(node_t *head,node_t *pn);
// Print all node contents
void print(node_t *head);
// Destroy all nodes
void destroy(node_t *head);
// Application function
// Create a linked list of length len and enter the content
int create_link(node_t *head,int len);
int main()
{
node_t*head = NULL;
intlen = 0;
if(init(head= create(sizeof(node_t))) != 0){
printf("failed to initialize linked list");
exit(0);
}
printf("Length: ");
scanf("%d",&len);
create_link(head,len);
print(head);
destroy(head);
free(head);
head= NULL;
return0;
}
// Create node function
// Return the first address of the new node if successful, or NULL if failed
void *create(int size)
{
returncalloc(1,size);
}
// Initialize the linked list
// 0-success 1-failure
int init(node_t *head)
{
if(NULL== head)
return1;
head->next= NULL;
return0;
}
// Header insertion method
// 0-success 1-failure
int insert_head(node_t *head,node_t *pn)
{
if(NULL== pn)
return1;
pn->next= head->next;
head->next= pn;
return0;
}
// Tail insertion method
// 0-success 1-failure
int insert_end(node_t *head,node_t *pn)
{
node_t*tail = NULL;
if(NULL== pn)
return1;
tail= head;
while(tail->next!= NULL)
tail= tail->next;
tail->next= pn;
pn->next= NULL;
return0;
}
// Print all node contents
void print(node_t *head)
{
node_t*cur = NULL;
cur= head->next;
while(cur!= NULL){
printf("%d",cur->data);
cur= cur->next;
}
printf("
");
}
// Destroy all nodes
void destroy(node_t *head)
{
node_t*del =NULL,*n_node = NULL;
del = head->next;
while(del != NULL){
n_node= del->next;
free(of);
del = n_node;
}
init(head);
}
// Application function
// Create a linked list of length len and enter the content
// Return the number of nodes created
int create_link(node_t *head,int len)
{
you = 0;
node_t*n_node = NULL;
printf("Input %d number: ",len);
for(i= 0;i < len;i++){
n_node = create(sizeof(node_t)); //Create a new node
if(NULL== n_node)
turn back
scanf("%d",&n_node->data); // Input data
insert_end(head,n_node); // insert linked list
}
turn back;
}
Previous article:C language random function
Next article:Build a complete life structure program framework
- Popular Resources
- Popular amplifiers
Recommended Content
Latest Microcontroller Articles
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
He Limin Column
Microcontroller and Embedded Systems Bible
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
MoreSelected Circuit Diagrams
MorePopular Articles
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
MoreDaily News
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- Do you know all the various motors commonly used in automotive electronics?
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- Power Inverter - A critical safety system for electric vehicles
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
Guess you like
- Questions about the two array forms of LSM6DSO FSM gesture recognition?
- What are the advantages and disadvantages of complementary push-pull H-bridge (2 NMOS + 2 PMOS) and totem pole H-bridge (4 NMOS), and their applications...
- The problem of turning frequency in LC filter circuit design
- Live broadcast at 10 am today [The rapid growth of Renesas RA MCU family members helps build safe and stable industrial control systems]
- Evaluation Weekly Report 20220411: GigaDevice's value-for-money MCU F310 is here, TI's heterogeneous multi-core AM5708 thousand yuan industrial board
- Program space issues
- Buy more time with the Raspberry Pi Pico
- Is this seller a scam?
- [Raspberry Pi Pico Review] 4. Connect Pico to the temperature sensor DS18B20 to read the temperature
- Qorvo's leading RF solutions