链表的操作(C语言)

#include<stdio.h>
#include<stdlib.h>
//定义链表节点
typedef struct node{
    int data;
    struct node *next;
}Node;

Node *createList();
void printfNodeList(Node *node);
void insertNode1(Node *head, int data);
void insertNode2(Node *head, int data);
void destroyList(Node *head);
int listLength(Node *head);
Node *searchList(Node *head, int key);
void deleteNodeList(Node *head, Node *find);
void bubbleSort(Node *head);
void sortList(Node *head);
void reverseList(Node *head);
int main()
{
    //1.创建一个空链表
    Node *head = createList();
    //2.向空链表中插入数据
    insertNode1(head,1);
    insertNode1(head,3);
    insertNode1(head,5);
    //3.打印
    printfNodeList(head);
    //4.长度计算
    int count1 = listLength(head);
    printf("the list length is %i\n",count1);
    //5.查找
    Node *k = searchList(head,3);
    printfNodeList(k);
    int count2 = listLength(k);
    printf("the list length is %i\n",count2);
    return 0;
}
Node *createList(){
    //1.创建头节点
    Node *head = (Node *)malloc(sizeof(Node));
    if(head == NULL){
        return NULL;
    }
    head->next = NULL;
    return head;
}
//尾插法
void insertNode1(Node *head, int data){
    //1.定义变量记录最后一个节点
    Node *pre = head;
    while(pre != NULL & pre->next != NULL){
        pre = pre ->next;
    }
    //2.创建一个新的节点
    Node *cur = (Node *)malloc(sizeof(Node));
    cur->data = data;
    //3.让新节点链接到上一个节点后面
    pre->next = cur;
    //4.当前节点下一个节点等于NULL
    cur->next = NULL;
    //5.让当前节点变成下一个节点的上一个节点
    pre = cur;
}
//头插法
void insertNode2(Node *head, int data){
    //1.创建一个新的节点
    Node *cur = (Node *)malloc(sizeof (Node));
    cur->data = data;
    //2.让新节点的下一个节点指向头节点的下一个节点
    cur->next = head->next;
    //3.让头节点的下一个节点指向新节点
    head->next = cur;
}
//打印
void printfNodeList(Node *node){
    Node *head = node->next;
    while(head != NULL){
        int currentData = head->data;
        printf("currentData = %i\n",currentData);
        head = head->next;
    }
}
//销毁
void destroyList(Node *head){
    Node *cur = NULL;
    while (head != NULL) {
        cur = head->next;
        free(head);
        head = cur;
    }
}
//长度计算
int listLength(Node *head){
    int count = 0;
    head = head->next;
    while(head){
        count++;
        head = head->next;
    }
    return count;
}
//链表的查找
Node *searchList(Node *head, int key){
    head = head->next;
    while(head){
        if(head->data == key){
            break;
        }else{
            head = head->next;
        }
    }
    return head;
}
//链表删除
void deleteNodeList(Node *head, Node *find){
    while(head->next != find){
        head = head->next;
    }
    head->next = find ->next;
    free(find);
}
//链表排序
//(1)
void bubbleSort(Node *head){
    int temp;
    //1.计算链表的长度
    int len = listLength(head);
    //2.定义变量记录前后节点
    Node *cur = NULL;
    //3.相邻元素进行比较,进行冒泡排序
    for(int i = 0;i < len-1;i++){
        cur = head->next;
        for(int j = 0;j<len-1-i;j++){
            if((cur->data)>(cur->next->data)){
                temp = cur->data;
                cur->data = cur->next->data;
                cur->next->data = temp;
            }
            cur = cur->next;
        }
    }
}
//(2)
void sortList(Node *head){
    //1.计算链表的长度
    int len =  listLength(head);
    //2.定义变量保存前后两个节点
    Node *sh, *pre, *cur;
    for(int i = 0;i < len-1;i++){
        sh = head;
        pre = sh->next;//第一个节点
        cur = pre->next;//第二个节点
        for(int j = 0;j < len-i-1; j++){
            if((pre->data)>(cur->data)){
                //交换节点位置
                sh->next = cur;
                pre->next = cur->next;
                cur->next = pre;
                //恢复节点名称
                Node *temp = pre;
                pre = cur;
                cur = temp;
            }
            //让所有节点往后移动
            sh = sh->next;
            pre = pre->next;
            cur = cur->next;
        }
    }
}
//链表反转
void reverseList(Node *head){
    //1.将链表一分为二
    Node *pre, *cur;
    pre = head->next;
    head->next = NULL;
    while(pre){
        cur = pre->next;
        head->next = pre;
        pre = cur;
    }
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值