2024.02.02作业

1.请简述栈区和堆区的区别

栈区借助栈的思想,先进后出,地址申请从大地知道小地址;堆区借助队列的思想,先进先出,地址申请从小地址到大地址

栈区内存计算机自动申请自动释放;堆区内存手动申请手动释放

栈区的大小在几M;堆区的大小在几个G

栈区内存比较连续;堆区内存容易出现片段化

2.

#include <stdio.h>

int main()
{
    int n;
    scanf("%d", &n);

    int a[n];
    for (int i = 0; i < n; i++)
    {
        scanf("%d", &a[i]);
    }

    int x;
    scanf("%d", &x);

    printf("新数组:\n");
    int count = 0;
    for (int i = 0; i < n; i++)
    {
        if (a[i] != x)
        {
            printf("%2d", a[i]);
        }
        else
        {
            count++;
        }
    }
    puts("");

    printf("新数组长度:%d\n", n - count);

    return 0;
}

3.请编程实现单链表的头插,头删,尾插,尾删

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

typedef int datatype;
typedef struct Node
{
    datatype data;
    struct Node* next;
} *sqlist;

sqlist create_node()
{
    sqlist node = (sqlist)malloc(sizeof(struct Node));

    if (NULL == node)
    {
        return NULL;
    }

    node->next = NULL;
    return node;
}

sqlist head_insert(sqlist head, datatype element)
{
    sqlist p = create_node();
    if (NULL == p)
    {
        puts("头插:头插失败");
        return head;
    }

    printf("头插:%d\n", element);
    p->data = element;
    if (NULL == head)
    {
        head = p;
    }
    else
    {
        p->next = head;
        head = p;
    }

    return head;
}

sqlist head_delete(sqlist head)
{
    if (NULL == head)
    {
        puts("头删:空链表");
        return NULL;
    }

    
    if (NULL == head->next)
    {
        printf("头删:%d\n", head->data);
        free(head);
        head = NULL;
        return head;
    }

    sqlist p = head;
    head = head->next;
    printf("头删:%d\n", p->data);
    free(p);
    p = NULL;

    return head;
}

sqlist tail_insert(sqlist head, datatype element)
{
    sqlist p = create_node();
    if (NULL == p)
    {
        puts("尾插:尾插失败");
        return head;
    }

    printf("尾插:%d\n", element);
    p->data = element;

    if (NULL == head)
    {
        head = p;
        return head;
    }

    sqlist q = head;
    while (q->next != NULL)
    {
        q = q->next;
    }
    q->next = p;

    return head;
}

sqlist tail_delete(sqlist head)
{
    if (NULL == head)
    {
        puts("尾删:空链表");
        return NULL;
    }

    if (NULL == head->next)
    {
        printf("尾删:%d\n", head->data);
        free(head);
        head = NULL;
        return head;
    }

    sqlist p = head;
    while (p->next->next != NULL)
    {
        p = p->next;
    }
    sqlist q = p->next;
    p->next = NULL;
    printf("尾删:%d\n", q->data);
    free(q);
    q = NULL;

    return head;
}

void show(sqlist head)
{
    printf("展示:");
    while (head != NULL)
    {
        printf("%2d", head->data);
        head = head->next;
    }
    puts("");
}

int main()
{
    int n = 5;

    sqlist head = NULL;
    for (int i = 0; i < n; i++)
    {
        head = head_insert(head, i);
    }
    show(head);

    for (int i = 0; i < n; i++)
    {
        head = tail_insert(head, i);
    }
    show(head);

    head = head_delete(head);
    head = tail_delete(head);
    show(head);

    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值