• 线性表-链式存储结构(2)


    1.编程实现单链表的建立/测长/打印

    typedef struct student
    {
        int data;
        struct student *next;
    }node;
    
    node *creat()
    {
        node *head, *p, *s;
        int x, cycle = 1;
        head = (node *)malloc(sizeof(node));
        p = head; //p指向尾部的结点
        while (cycle)
        {
            printf("
     please input the data: ");
            scanf("%d", &x);
            if (x != 0)
            {
                s = (node *)malloc(sizeof(node));
                s->data = x;
                printf("
    %d", s->data);
                p->next = s;
                p = s;
            }
            else cycle = 0;
        }
        head = head->next;
        p->next = NULL;
        return (head);
    }
    
    int length(node *head)
    {
        node *p = head;
        int x = 0;
        while (p != NULL)
        {
            p = p->next;
            x++;
        }
        return x;
    }
    
    void print(node *head)
    {
        node *p;int n;
        n = length(head);
        printf("
     Now ,these %d records are:
    ", n);
        p = head;
        if (head != NULL)
            while (p != NULL)
            {
                printf("
        %d", p->data);
                p = p->next;
            }
    }

    2.实现单链表的插入

    node *insert(node *head, int num)
    {
        node *p0, *p1, *p2 = NULL;
        p1 = head;
        p0 = (node *)malloc(sizeof(node));
        p0->data = num;
        while (p0->data > p1->data&&p1->next != NULL)
        {
            p2 = p1;
            p1 = p1->next;
        }
        if (p0->data <= p1->data)
        {
            if (head == p1) //头
            {
                p0->next = p1;
                head = p0;
            }
            else //中
            {
                p2->next = p0;
                p0->next = p1;
            }
        }
        else //尾
        {
            p1->next = p0;
            p0->next = NULL;
        }
        
        return (head);
    
    }

    3.实现单链表的删除

    node *del(node **head, int num)
    {
        node *p1 = NULL, *p2 = NULL;
        p1 = *head;
        while (num != p1->data&&p1->next != NULL)
        {
            p2 = p1;
            p1 = p1->next;
        }
        if (num == p1->data)
        {
            if (p1 == *head)
            {
                *head = p1->next;
                free(p1);
            }
            else
                p2->next = p1->next;
        }
        else printf("
     %d could not been found", num);
        return *head;
    
    }

    4.实现单链表排序

    node *sort(node *head) //冒泡
    {
        node *p;
        int n;int temp;
        n = length(head);
        if (head == NULL || head->next == NULL)return head;
        p = head;
        for (int j = 1;j < n;++j)
        {
            p = head;
            for (int i = 0;i < n - j;++i)
            {
                if (p->data > p->next->data)
                {
                    temp = p->data;
                    p->data = p->next->data;
                    p->next->data = temp;
                }
                p = p->next;
            }
        }
        return head;
    }

    5.实现单链表的逆置

    node *reverse(node *head)
    {
        node *p1, *p2, *p3; //前驱指针 当前指针 缓存指针
        if (head == NULL || head->next == NULL) //没带头结点的空链表 和带头结点的空链表 判断是否为空
            return head;
        p1 = head, p2 = p1->next;
        while (p2)
        {
            p3 = p2->next;
            p2->next = p1;
            p1 = p2; //p1 p2后移
            p2 = p3;
        }
        head->next = NULL;
        head = p1;
        return head;
    }
    void main()
    {
    	node *head;
    	int del_num, insert_num;
    	head = creat();
    	print(head);
    
    	printf("
     please input the delete data:");
    	scanf("%d", &del_num);
    	head = del(&head, del_num);
    	print(head);
    
    	printf("
    please input the insert data:");
    	scanf("%d", &insert_num);
    	head = insert(head, insert_num);
    	print(head);
    
    	system("pause");
    }
    
  • 相关阅读:
    一个弹框表单复用create,edit表单操作
    数组去重,防抖节流,渲染几万条数据不卡页面,32个手撕JS,彻底摆脱初级前端
    解决vue表单回显数据无法修改的问题
    正则
    将UTC时间转换为当地的时间
    Vue页面上实时显示当前时间,每秒更新
    互联网协议
    记录:批量重命名文件夹下的文件
    IdentityServer4 无法登录,一直跳转登录界面
    同解与等价
  • 原文地址:https://www.cnblogs.com/Yang-Xin-Yi/p/14646282.html
Copyright © 2020-2023  润新知