/**
# encoding: utf-8
# 版权所有 2023 涂聚文有限公司
# 许可信息查看:https://www.learnc.net/c-data-structures/c-linked-list/
# 描述:https://blog.jetbrains.com/clion/2016/05/keep-your-code-documented/
# Author : geovindu,Geovin Du 涂聚文.
# IDE : CLion 2023.1.1 c17 windows 10
# Datetime : 2023/11/11 6:10
# User : geovindu
# Product : CLion
# Project : ctest
# File : geovindu.c
# explain : 学习
*/
//
// Created by geovindu on 2023/11/11.
//
#include "../includes/geovindu.h"
/**!
* @brief
* @param data
*
*/
typedef void (*callback)(node* data);
/**
* @brief create a new node
initialize the data and next field
return the newly created node
* @param data
* @param next
@return
*/
node* create(int data,node* next)
{
node* new_node = (node*)malloc(sizeof(node));
if(new_node == NULL)
{
printf("Error creating a new node.\n");
exit(0);
}
new_node->data = data;
new_node->next = next;
return new_node;
}
/**
@brief add a new node at the beginning of the list
@param head
@param data
*/
node* prepend(node* head,int data)
{
node* new_node = create(data,head);
head = new_node;
return head;
}
/**
*@brief add a new node at the end of the list
* @param head
* @param data
*/
node* append(node* head, int data)
{
if(head == NULL)
return NULL;
/* go to the last node */
node *cursor = head;
while(cursor->next != NULL)
cursor = cursor->next;
/* create a new node */
node* new_node = create(data,NULL);
cursor->next = new_node;
return head;
}
/**
@brief insert a new node after the prev node
@param head
@param data
@param prev
@return
*/
node* insert_after(node *head, int data, node* prev)
{
if(head == NULL || prev == NULL)
return NULL;
/* find the prev node, starting from the first node*/
node *cursor = head;
while(cursor != prev)
cursor = cursor->next;
if(cursor != NULL)
{
node* new_node = create(data,cursor->next);
cursor->next = new_node;
return head;
}
else
{
return NULL;
}
}
/**
*@brief insert a new node before the nxt node
*@param head
* @param data
* @param nxt
*/
node* insert_before(node *head, int data, node* nxt)
{
if(nxt == NULL || head == NULL)
return NULL;
if(head == nxt)
{
head = prepend(head,data);
return head;
}
/* find the prev node, starting from the first node*/
node *cursor = head;
while(cursor != NULL)
{
if(cursor->next == nxt)
break;
cursor = cursor->next;
}
if(cursor != NULL)
{
node* new_node = create(data,cursor->next);
cursor->next = new_node;
return head;
}
else
{
return NULL;
}
}
/**
*@brief traverse the linked list
* @param head
* @param f
*/
void traverse(node* head,callback f)
{
node* cursor = head;
while(cursor != NULL)
{
f(cursor);
cursor = cursor->next;
}
}
/**
* @brief remove node from the front of list
* @param head
*/
node* remove_front(node* head)
{
if(head == NULL)
return NULL;
node *front = head;
head = head->next;
front->next = NULL;
/* is this the last node in the list */
if(front == head)
head = NULL;
free(front);
return head;
}
/**
*@brief remove node from the back of the list
* @param head
*/
node* remove_back(node* head)
{
if(head == NULL)
return NULL;
node *cursor = head;
node *back = NULL;
while(cursor->next != NULL)
{
back = cursor;
cursor = cursor->next;
}
if(back != NULL)
back->next = NULL;
/* if this is the last node in the list*/
if(cursor == head)
head = NULL;
free(cursor);
return head;
}
/**
*@brief remove a node from the list
* @param head
* @param nd
*
*/
node* remove_any(node* head,node* nd)
{
if(nd == NULL)
return NULL;
/* if the node is the first node */
if(nd == head)
return remove_front(head);
/* if the node is the last node */
if(nd->next == NULL)
return remove_back(head);
/* if the node is in the middle */
node* cursor = head;
while(cursor != NULL)
{
if(cursor->next == nd)
break;
cursor = cursor->next;
}
if(cursor != NULL)
{
node* tmp = cursor->next;
cursor->next = tmp->next;
tmp->next = NULL;
free(tmp);
}
return head;
}
/**
*@brief display a node
* @param p
*
*/
void display(node* n)
{
if(n != NULL)
printf("%d ", n->data);
}
/**
*@brief Search for a specific node with input data
return the first matched node that stores the input data,
otherwise return NULL
*@param head
* @param data
*/
node* search(node* head,int data)
{
node *cursor = head;
while(cursor!=NULL)
{
if(cursor->data == data)
return cursor;
cursor = cursor->next;
}
return NULL;
}
/**
*@brief remove all element of the list
* @param head
*
*/
void dispose(node *head)
{
node *cursor, *tmp;
if(head != NULL)
{
cursor = head->next;
head->next = NULL;
while(cursor != NULL)
{
tmp = cursor->next;
free(cursor);
cursor = tmp;
}
}
}
/**
*@brief return the number of elements in the list
* @param head
*/
int count(node *head)
{
node *cursor = head;
int c = 0;
while(cursor != NULL)
{
c++;
cursor = cursor->next;
}
return c;
}
/**
*@brief sort the linked list using insertion sort
* @param head
*/
node* insertion_sort(node* head)
{
node *x, *y, *e;
x = head;
head = NULL;
while(x != NULL)
{
e = x;
x = x->next;
if (head != NULL)
{
if(e->data > head->data)
{
y = head;
while ((y->next != NULL) && (e->data> y->next->data))
{
y = y->next;
}
e->next = y->next;
y->next = e;
}
else
{
e->next = head;
head = e ;
}
}
else
{
e->next = NULL;
head = e ;
}
}
return head;
}
/**
*@brief reverse the linked list
* @param head
*/
node* reverse(node* head)
{
node* prev = NULL;
node* current = head;
node* next;
while (current != NULL)
{
next = current->next;
current->next = prev;
prev = current;
current = next;
}
head = prev;
return head;
}
/**
*@brief display the menu
*/
void menu()
{
printf("--- C Linked List Demonstration --- \n\n");
printf("0.menu\n");
printf("1.prepend an element\n");
printf("2.append an element\n");
printf("3.search for an element\n");
printf("4.insert after an element\n");
printf("5.insert before an element\n");
printf("6.remove front node\n");
printf("7.remove back node\n");
printf("8.remove any node\n");
printf("9.sort the list\n");
printf("10.Reverse the linked list\n");
printf("-1.quit\n");
}
/**
* @brief 显示
*/
void displaynode()
{
int command = 0;
int data;
node* head = NULL;
node* tmp = NULL;
callback disp = display;
menu();
while(1)
{
printf("\nEnter a command(0-10,-1 to quit):");
scanf("%d",&command);
if(command == -1)
break;
switch(command)
{
case 0:
menu();
break;
case 1:
printf("Please enter a number to prepend:");
scanf("%d",&data);
head = prepend(head,data);
traverse(head,disp);
break;
case 2:
printf("Please enter a number to append:");
scanf("%d",&data);
head = append(head,data);
traverse(head,disp);
break;
case 3:
printf("Please enter a number to search:");
scanf("%d",&data);
tmp = search(head,data);
if(tmp != NULL)
{
printf("Element with value %d found.",data);
}
else
{
printf("Element with value %d not found.",data);
}
break;
case 4:
printf("Enter the element value where you want to insert after:");
scanf("%d",&data);
tmp = search(head,data);
if(tmp != NULL)
{
printf("Enter the element value to insert after:");
scanf("%d",&data);
head = insert_after(head,data,tmp);
if(head != NULL)
traverse(head,disp);
}
else
{
printf("Element with value %d not found.",data);
}
break;
case 5:
printf("Enter the element value where you want to insert before:");
scanf("%d",&data);
tmp = search(head,data);
if(tmp != NULL)
{
printf("Enter the element value to insert before:");
scanf("%d",&data);
head = insert_before(head,data,tmp);
if(head != NULL)
traverse(head,disp);
}
else
{
printf("Element with value %d not found.",data);
}
break;
case 6:
head = remove_front(head);
if(head != NULL)
traverse(head,disp);
break;
case 7:
head = remove_back(head);
if(head != NULL)
traverse(head,disp);
break;
case 8:
printf("Enter the element value to remove:");
scanf("%d",&data);
tmp = search(head,data);
if(tmp != NULL)
{
remove_any(head,tmp);
if(head != NULL)
traverse(head,disp);
}
else
{
printf("Element with value %d not found.",data);
}
break;
case 9:
head = insertion_sort(head);
if(head != NULL)
traverse(head,disp);
break;
case 10:
head = reverse(head);
if(head != NULL)
traverse(head,disp);
break;
}
}
dispose(head);
}
/**
# encoding: utf-8
# 版权所有 2023 涂聚文有限公司
# 许可信息查看:
# 描述:
# Author : geovindu,Geovin Du 涂聚文.
# IDE : CLion 2023.1.1 c17 windows 10
# Datetime : 2023/11/11 6:09
# User : geovindu
# Product : CLion
# Project : ctest
# File : geovindu.c
# explain : 学习
*/
//
// Created by geovindu on 2023/11/11.
//
#ifndef CTEST_GEOVINDU_H
#define CTEST_GEOVINDU_H
#include <stdio.h>
#include <stdlib.h>
#include <sdpblb.h>
/**
* @brief
*
*/
typedef struct node
{
/**
* @brief
*/
int data;
/**
* @brief
*
*/
struct node* next;
} node;
/**
* @brief 显示
*/
void displaynode();
#endif //CTEST_GEOVINDU_H
调用:
/**
# encoding: utf-8
# 版权所有 2013 涂聚文有限公司
# 许可信息查看:linked list
# 描述:
# Author : geovindu,Geovin Du 涂聚文.
# IDE : CLion 2023.1.1 c17 window10
# Datetime : 2023 11.11
# User : geovindu
# Product : ctest
# Project : ctest
# File : main.c
# explain : 学习
*/
#include <stdio.h>
#include "includes/geovindu.h"
/**
* @brief
* @return
*/
int main() {
SetConsoleOutputCP(CP_UTF8);//终端中文乱码问题
printf("Hello, World!涂聚文!\n");
displaynode();
return 0;
}
输出:
结构化文件夹,调用头文件即可