编辑代码

#include <stdio.h>
#include<stdlib.h>
#include<stdbool.h>
// 定义链表节点结构体
typedef struct Node {
    int data;           // 数据域
    struct Node *next; // 指针域
} LNode;

// 尾插法创建带头结点的单链表
LNode* createListByTailInsert(int data[], int n) {
    LNode *head = (LNode *)malloc(sizeof(LNode)); // 创建头结点
    if (head == NULL) {
        printf("内存分配失败\n");
        exit(1);
    }
    head->next = NULL; // 初始化头结点指针域为空

    LNode *tail = head; // 尾指针初始指向头结点

    for (int i = 0; i < n; i++) {
        LNode *newNode = (LNode *)malloc(sizeof(LNode));
        if (newNode == NULL) {
            printf("内存分配失败\n");
            exit(1);
        }
        newNode->data = data[i]; // 设置数据
        newNode->next = NULL;    // 新节点作为最后一个节点

        tail->next = newNode;    // 插入到尾部
        tail = newNode;          // 更新尾指针
    }

    return head; // 返回头指针
}
// 打印链表函数
void printList(LNode *head) {
    LNode *current = head->next; // 跳过头结点
    while (current != NULL) {
        printf("%d -> ", current->data);
        current = current->next;
    }
    printf("NULL\n");
}
//反转链表,使用头插法
LNode  *Reverse(LNode *head){
    LNode *p=head->next;
    head->next=NULL;
    while(p!=NULL){
        LNode *q=p->next;
        p->next=head;
        head=p;
        p=q;
    }
    return head;
}
//--------------
//最小值函数
int min(int a,int b){
    return a<b?a:b;
}
//交换函数
void swap(int *x, int *y) {
    int temp = *x;
    *x = *y;
    *y = temp;
}
//快速排序
void QuickSort(int array[], int low, int high) {
    int i = low; 
    int j = high;
    if(i >= j) {
        return;
    }
 
    int temp = array[low];
    while(i != j) {
        while(array[j] >= temp && i < j) {
            j--;
        }
	while(array[i] <= temp && i < j) {
            i++;
        }
	if(i < j) {
            swap(&array[i], &array[j]);
        }
    }
 
    //将基准temp放于自己的位置,(第i个位置)
    swap(&array[low], &array[i]);
    QuickSort(array, low, i - 1);
    QuickSort(array, i + 1, high);
}
//--------------------
// int MinDistance(int *a,int *b,int *c,int an,int bn,int cn){
//     int D;
//     int temp=abs(a[0]-b[0]);
//     int temp1=abs(b[0]-c[0]);
//     int temp2=abs(c[0]-a[0]);
//计算|a-b|的最小值
//     for(int i=0;i<an;i++){
//         for(int j=0;j<bn;j++){
//             temp=min(temp,abs(a[i]-b[j]));
//         }
//     }
//计算|b-c|的最小值
//     for(int i=0;i<an;i++){
//         for(int j=0;j<bn;j++){
//             temp1=min(temp1,abs(b[i]-c[j]));
//         }
//     }
//计算|c-a|的最小值
//     for(int i=0;i<an;i++){
//         for(int j=0;j<bn;j++){
//             temp2=min(temp2,abs(c[i]-a[j]));
//         }
//     }
//     D=temp+temp1+temp2;
//     printf("%d",D);
// }
// -------------
// int f(LNode *str1,LNode *str2){
//     int n=0;
//     int m=0;
//     //求链表长度
//     while(str1->next!=NULL){
//         n++;
//         str1=str1->next;
//     }
//      while(str2->next!=NULL){
//         m++;
//         str2=str2->next;
//     }
//     //如果第一个链表大于第二个链表,则将str1指针放置在m-n+1的位置上
//     if(m<n){
//     for(int i=0;i<m-n+1;i++){
//         str1=str1->next;
//     }
//     }
//     //与上面相反,如果m<n,则将str2放置在n-m+1的位置上
//     if(m>n){
//         for(int i=0;i<n-m+1;i++){
//             str2=str2->next;
//     }
//     }
//     while(str1->next!=NULL&&str1->next!=str2->next){
//         str1=str1->next;
//         str2=str2->next;
//     }
//     return str1->next;
// }
//-------------------------
// LNode* RemoveElement(LNode *head){
//     LNode *cur=head->next;
//     while(cur!=NULL){
//          LNode *p=cur->next;
//         LNode *pre=cur;
//         while(p!=NULL){
//             if(abs(p->data)==abs(cur->data)){
//                 pre->next=p->next;
//                 free(p);
//                 p=pre->next;
//             }
//             else{
//                 pre=p;
//                 p=p->next;
//             }
//         }
//         cur=cur->next;
//     }
//     return head;
// }
//-------------------------
// void move(LNode *head){
//     LNode *slow,*fast;
//     slow=fast=head->next;
//     //寻找中间结点
//     while(fast!=NULL && fast->next!=NULL){
//         slow=slow->next;
//         fast=fast->next->next;
//     }
//     //右部分链表翻转
//     slow=Reverse(slow);
//     LNode *pre=head->next;
//     //摘取元素,放到相应位置上
//     while(slow!=NULL){
//         fast=slow->next;
//         slow->next=pre->next;
//         pre->next=slow;
//         pre=slow->next;
//         slow=fast;
//     }
//     pre->next=NULL;
// }
// void push(){//入队
//     if(rear->next==front){
//         newNode;//申请一片新的空间
//         rear->next=newNode->next;
//         rear->next=newNode;
//     }
//     else{
//     rear->data=val;//元素入队
//     rear=newNode; 
//     }
// }
// int pop(){
//     if(rear==front){
//         return;
//     }
//     else{
//         val=front->data;
//         front=front->next;
//         return val;
//     }
// }
int main () {
    int data[] = {1,2,3,4,5,6};//链表中的值
    int n = sizeof(data) / sizeof(data[0]);
    LNode *L = createListByTailInsert(data, n); // 创建链表
    printf("链表内容为:\n");
    printList(L); // 打印链表
    // 2015数据结构RemoveElement(L);
    //2019数据结构move(L);
    // --------------
    // int s1[]={-1,0,9};
    // int s2[]={-25,-10,1,11};
    // int s3[]={2,9,17,30,41};
    // int an=sizeof(s1)/sizeof(int);
    // int bn=sizeof(s2)/sizeof(int);
    // int cn=sizeof(s3)/sizeof(int);
    // 2020数据结构MinDistance(s1,s2,s3,an,bn,cn);
    //2012数据结构f()
   
    return 0;
}