C language linked list complete operation demonstration
- 2020-06-07 04:55:12
- OfStack
This article has Shared the C linked list operation demonstration specific code for your reference, the specific content is as follows
Header file: link_0505.h
/*
Chain performance in
*/
#ifndef __LINK_0505
#define __LINK_0505
typedef struct node{
int num;
struct node* p_next;
}node;
typedef struct
{
node head,tail;
}link;
// Initializer for the linked list
void link_init(link *);
// List cleanup function
void link_deinit(link *);
// A function that determines if a linked list is empty
int link_empty(link *);
// A function that determines if a linked list is full
int link_full(link *);
// A function that counts the number of significant digits
int link_size(link *);
// A function that inserts a number at the front
int link_add_head(link *, int );
// A function that inserts a new number at the end
int link_append(link *, int );
// A function that inserts Numbers into a linked list in sequence
int link_insert(link *, int);
// Delete the function with the first digit
int link_remove_head(link *);
// Delete the last 1 Significant number
int link_remove_tail(link *);
// Deletes a function with a given number
int link_remove(link *, int );
// For the first 1 A function of a significant number
int link_get_head(link *, int *);
// To obtain the final 1 A function of a significant number
int link_get_tail(link *, int *);
// Gets the function with the specified number
int link_get(link *, int *, int );
#endif
Implementation code: link_0505.cpp
/*
Chain performance in
*/
#include "stdlib.h"
#include "link_0505.h"
// Initializer for the linked list
void link_init(link *p_link)
{
p_link->head.p_next = &(p_link->tail);
}
// List cleanup function
void link_deinit(link *p_link)
{
while(p_link->head.p_next != &(p_link->tail))
{
node *p_first = &(p_link->head);
node *p_mid = p_first->p_next;
node *p_last = p_mid->p_next;
p_first->p_next = p_last;
free(p_mid);
p_mid = NULL;
}
}
// A function that determines if a linked list is empty
int link_empty(link *p_link)
{
return p_link->head.p_next == &(p_link->tail);
}
// A function that determines if a linked list is full
int link_full(link *p_link)
{
return 0;
}
// A function that counts the number of significant digits
int link_size(link *p_link)
{
int cnt = 0;
node *p_node = NULL;
for (p_node = &(p_link->head);p_node != &(p_link->tail);p_node = p_node->p_next)
{
node *p_first = p_node;
node *p_mid = p_first->p_next;
node *p_last = p_mid->p_next;
if (p_mid != &(p_link->tail))
{
cnt++;
}
}
return cnt;
}
// A function that inserts a number at the front
int link_add_head(link *p_link, int num)
{
node *p_temp = (node *)malloc(sizeof(node));
if (!p_temp)
{
return 0;
}
p_temp->num = num;
node *p_first = &(p_link->head);
node *p_mid = p_first->p_next;
node *p_last = p_mid->p_next;
p_first->p_next = p_temp;
p_temp->p_next = p_mid;
return 1;
}
// A function that inserts a new number at the end
int link_append(link *p_link, int num)
{
node *p_tmp = (node *)malloc(sizeof(node));
node *p_node = NULL;
if (!p_tmp)
{
return 0;
}
p_tmp->num = num;
for (p_node = &(p_link->head);p_node != &(p_link->tail);p_node = p_node->p_next)
{
node *p_first = p_node;
node *p_mid = p_first->p_next;
node *p_last = p_mid->p_next;
if (p_mid == &(p_link->tail))
{
p_first->p_next = p_tmp;
p_tmp->p_next = p_mid;
break;
}
}
return 1;
}
// A function that inserts Numbers into a linked list in sequence
int link_insert(link *p_link, int num)
{
node* p_temp = (node *)malloc(sizeof(node));
node* p_node = NULL;
if (!p_temp)
{
return 0;
}
p_temp->num = num;
p_temp->p_next = NULL;
for (p_node = &(p_link->head);p_node != &(p_link->tail);p_node = p_node->p_next)
{
node *p_first = p_node;
node *p_mid = p_first->p_next;
node *p_last = p_mid->p_next;
if (p_mid == &(p_link->tail) || p_mid->num > p_temp->num)
{
p_first->p_next = p_temp;
p_temp->p_next = p_mid;
break;
}
}
return 0;
}
// Delete the function with the first digit
int link_remove_head(link *p_link)
{
node *p_first = &(p_link->head);
node *p_mid = p_first->p_next;
node *p_last = p_mid->p_next;
if (p_link->head.p_next == &(p_link->tail))
{
return 0;
}
p_first->p_next = p_last;
free(p_mid);
p_mid = NULL;
}
// Delete the last 1 Significant number
int link_remove_tail(link *p_link)
{
node *p_node = NULL;
for (p_node = &(p_link->head);p_node !=&(p_link->tail);p_node = p_node->p_next)
{
node *p_first = p_node;
node *p_mid = p_first->p_next;
node *p_last = p_mid->p_next;
if (p_last == &(p_link->tail))
{
p_first->p_next = p_last;
free(p_mid);
p_mid = NULL;
return 1;
}
}
return 0;
}
// Deletes a function with a given number
int link_remove(link *p_link, int num)
{
node *p_node = NULL;
for (p_node = &(p_link->head);p_node != &(p_link->tail);p_node = p_node->p_next)
{
node *p_first = p_node;
node *p_mid = p_first->p_next;
node *p_last = p_mid->p_next;
if (p_mid != &(p_link->tail) && p_mid->num == num)
{
p_first->p_next = p_last;
free(p_mid);
p_mid = NULL;
return 1;
}
}
return 0;
}
// For the first 1 A function of a significant number
int link_get_head(link *p_link, int *p_num)
{
if (p_link->head.p_next == &(p_link->tail))
{
return 0;
}
node *p_first = &(p_link->head);
node *p_mid = p_first->p_next;
node *p_last = p_mid->p_next;
p_first->p_next = p_last;
*p_num = p_mid->num;
return 1;
}
// To obtain the final 1 A function of a significant number
int link_get_tail(link *p_link, int *p_num)
{
node *p_node = NULL;
for (p_node = &(p_link->head);p_node != &(p_link->tail);p_node = p_node->p_next)
{
node *p_first = p_node;
node *p_mid = p_first->p_next;
node *p_last = p_mid->p_next;
if (p_last == &(p_link->tail))
{
*p_num = p_mid->num;
return 1;
}
}
return 0;
}
// Gets the function with the specified number
int link_get(link *p_link, int *p_num, int num)
{
int cnt = 0;
node *p_node = NULL;
for (p_node = &(p_link->head);p_node != &(p_link->tail);p_node = p_node->p_next)
{
node *p_first = p_node;
node *p_mid = p_first->p_next;
node *p_last = p_mid->p_next;
if (p_mid != &(p_link->tail) && cnt == num)
{
*p_num = p_mid->num;
return 1;
}
cnt++;
}
return 0;
}
Test function:
/*
* List the test
* */
#include <stdio.h>
#include "link_0505.h"
int main() {
int size = 0, num = 0, val = 0;
link lnk = {0};
link_init(&lnk);
link_add_head(&lnk, 30);
link_add_head(&lnk, 20);
link_append(&lnk, 90);
link_append(&lnk, 100);
link_insert(&lnk, 50);
link_insert(&lnk, 60);
link_insert(&lnk, 40);
link_insert(&lnk, 80);
link_insert(&lnk, 70);
size = link_size(&lnk);
for (num = 0;num <= size - 1;num++) {
link_get(&lnk, &val, num);
printf("%d ", val);
}
printf("\n");
printf("------------------");
link_remove_head(&lnk);
link_remove_tail(&lnk);
link_remove(&lnk, 70);
size = link_size(&lnk);
for (num = 0;num <= size - 1;num++) {
link_get(&lnk, &val, num);
printf("%d ", val);
}
printf("\n");
link_get_head(&lnk, &val);
printf(" The first number is %d\n", val);
link_get_tail(&lnk, &val);
printf(" The last number is %d\n", val);
link_deinit(&lnk);
return 0;
}