(2024.1.8-2024.1.14)C语言学习小结

既然之前说了要尝试坚持写博客,那就试试吧。本周花在C语言上的时间不多,简要回顾一下。

动态数组

学习并实践了基本的动态数组知识,即calloc、malloc、relloc、free。以下是基本综合所学内容写的代码,实现动态数据添加。

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n, OnceNum, Size, CurrentNum = 0;
    int *arr, *stick;
    char select;

    printf("Please enter the inital size of the array:");  //设定数组初始大小
    scanf("%d", &n);

    arr = malloc(n * sizeof(int)); 
    stick = arr;
    if (arr == NULL) // 判断是否成功创建数组
    {
        printf("NULL!\n");
        return 1;   
    }
    Size = n * sizeof(int);

    printf("Do you want to append elment(s)? Y/N:"); //询问是否进行赋值
    scanf(" %c", &select);

    while (select == 'Y' || select == 'y')
    {
        printf("How many?"); //赋值数据的数量
        scanf("%d", &OnceNum);

        if (CurrentNum + OnceNum <= n) //判断是否超出数组大小
        {
            for (int i = 1; i <= OnceNum; i += 1)
            {
                printf("Please enter:");
                scanf("%d", arr);
                arr++;
            }
            CurrentNum += OnceNum;
        }
        else
        {
            printf("Current space is not enough...Please wait and enter again.\n"); //超出数组原大小则扩大数组
            realloc(stick, (n * 2) * sizeof(int));
            n *= 2;
            if (stick == NULL)
            {
            printf("NULL!\n");
            return 1;   
            }
        }

        printf("Current data : "); //显示当前数组数据
        for (int i = 0; i < CurrentNum; i += 1)
        {
            printf("%d ", stick[i]);
        }
        printf("\n");

        printf("Do you want to append element(s)? Y/N:");
        scanf(" %c", &select);
    }

    printf("\nFinal data : "); //显示最终数组数据
    for (int i = 0; i < CurrentNum; i += 1)
    {
        printf("%d ", stick[i]);
    }
    printf("\n");

    free(stick); //释放数组空间
    printf("\nbye\n");
}

简单的单向链表

学习了基础的单向链表知识,并尝试写一个涵盖全部基本操作(创建、插入、删除等)的程序,但是实际写代码的过程中bug一个接一个地来,修完一个又来一个,导致原定的计划并未完成,节点的删除已经释放都还没做好,以下是半成品代码。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct link //定义链表结构体
{
    int data;
    struct link *next;
};

void Display(struct link *head) // 显示链表全部数据
{
    struct link *display = head;
    int n = 1;
    while (display != NULL)
    {
        printf("Node%d: %d\n", n, display -> data);
        display = display -> next;
        n += 1;
    }
}

void AddNode(struct link **head) //新增节点
{
    int rank;
    struct link *guide = *head;
    struct link *NN = NULL;
    NN = (struct link *)malloc(sizeof(struct link));
    NN -> next = NULL;
    scanf("%d %d", &NN -> data, &rank);
    for (int i = 1; i < rank - 1; i += 1)
    {
        guide = guide -> next;
    }
    if (guide == NULL) //超出链表,插至尾节点
    {
        guide = NN;
    }
    else if (guide -> next == NULL) //插至尾节点
    {
        guide -> next = NN;
    }
    else if (rank == 1) //插至首节点
    {
        NN -> next = guide;
        *head = NN;
    }
    else //插至中间特定位置
    {
        NN -> next = guide -> next;
        guide -> next = NN;
    }
    
}

void DeleteNode() //删除节点
{

}

void Free(struct link *head) //释放链表空间
{
    struct link *f = head, *temp = NULL;
    while (f != NULL)
    {   
        temp = f;
        f = f -> next;
        if (f != NULL)
        free(temp);
    }
}

int main()
{
    int n = 1;
    struct link *head = NULL;
    struct link N1, N2;

    head = &N1;
    scanf("%d", &N1.data);

    N1.next = &N2; 
    N2.next = NULL;
    N2.data = 0;

    AddNode(&head);
    AddNode(&head);
    Display(head);
    DeleteNode();
    DeleteNode();
    Display(head);
    Free(head);
    return 0;
}

期间频繁出错的就在新增节点AddNode部分,遇到不少问题:

  • 没想到用malloc新增节点;
  • 首节点情况没有单独列出;
  • 起初函数参数是*head,即修改的指针是形参,参数未传递指针的指针(或者用return也行);
  • 没分清结构体指针与结构体
  • ……

其他编程练习

也做了写一点点oj平台上的编程题,其中有一道“从字符串提取数字”没做好,当时没想到用for+while循环嵌套实现更灵活。

posted @ 2024-01-14 19:34  20231309  阅读(35)  评论(0编辑  收藏  举报