《阿哈!算法》2-4链表

指针:存储一个地址,确切地说是存储一个内存空间的地址。

malloc函数的作用是从内存中申请分配指定字节大小的分配空间。可以用 malloc(sizeof(int));申请。我们用指针来对这空间进行操作。用一个指针指向这个空间,即存储这个空间的首地址。

        struct node *q;
        p=(struct node *)malloc(sizeof(struct node));

malloc函数返回类型是 void *类型,可以强制转换为任何其他类型的指针。 

ps:为什么申明指针时需要申明类型呢?

因为指针变量存储的是以恶搞内存空间的首地址(第一个字节的地址),但是这个空间占用了多少个字节,用来存储什么类型的数,则是由指针的类型来标明的。这样系统才知道应该取多少个连续内存作为一个数据。

结点的读入:

        struct node *p,*q;      
        scanf("%d",&a);
        //动态申请一个空间,用来存放一个结点,并用临时指针p指向这个结点
        p=(struct node *)malloc(sizeof(struct node));//指针p获取动态内存分配的内存空间地址
        //数据存储到当前结点中
        p->data=a;
        p->next=NULL;
        if(head==NULL)head=p;   //如果这是第一个创建的结点,则将头指针指向这个结点
        else
            q->next=p;          //如果不是第一个创建的结点,则将上一个结点的后继指针指向当前结点
        q=p;                    //指针q也要指向当前结点

 

 


题目代码如下:

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

const  int max=1000;

//用结构体表示链表的结点类型
struct node{
    int data;
    struct node *next;
};


int main() {
    struct node *head,*p,*q,*t;
    int n,a;
    scanf("%d",&n);
    head=NULL;      //头指针为空
    for (int i=1; i<=n; i++) {
        scanf("%d",&a);
        //动态申请一个空间,用来存放一个结点,并用临时指针p指向这个结点
        p=(struct node *)malloc(sizeof(struct node));//指针p获取动态内存分配的内存空间地址
        //数据存储到当前结点中
        p->data=a;
        p->next=NULL;
        if(head==NULL)head=p;   //如果这是第一个创建的结点,则将头指针指向这个结点
        else
            q->next=p;          //如果不是第一个创建的结点,则将上一个结点的后继指针指向当前结点
        q=p;                    //指针q也要指向当前结点
    }
    //链表中插入数
    scanf("%d",&a); //读入插入的数
    t=head;//从链表头部开始遍历
    while (t!=NULL) {
        if (t->next->data>a) {      //如果当前结点指向的下一个结点的值大于被插数,将数插入到中间用来存放新增结点
            p=(struct node *)malloc(sizeof(struct node));
            p->data=a;
            p->next=t->next;    //新增结点的后继指针指向当前结点的后继指针指向的结点
            t->next=p;          //当前结点的后继指针指向新增结点
            break;              //插入完毕退出循环
        }
        t=t->next;              //继续下一个循环
    }
    
    //输出链表中的所有数
    t=head;
    while (t!=NULL) {
        printf("%d ",t->data);
        t=t->next;
    }
    printf("\n");
    getchar();getchar();
    return 0;
}



 

### C语言编程题答案及学习资料 以下是关于C语言编程题的答案和相关学习资料的详细内容: #### 1. 题目一:分解质因数 ```c #include <stdio.h> int main() { int i, n; scanf("%d", &n); printf("%d=", n); for (i = 2; i <= n; i++) { while (n != i) { if (n % i == 0) { printf("%d*", i); n = n / i; } else { break; } } } printf("%d", n); return 0; } ``` 上述代码实现了将输入的整数分解为质因数的功能[^1]。用户可以通过修改输入值来测试不同数字的质因数分解。 #### 2. 题目二:计算阶乘 ```c #include <stdio.h> int main() { int i = 0, n = 0, ret = 1; scanf("%d", &n); for (i = 1; i <= n; i++) { ret *= i; } printf("%d\n", ret); return 0; } ``` 此代码用于计算并输出给定整数 `n` 的阶乘结果[^2]。 #### 3. 题目三:计算阶乘累加和 ```c #include <stdio.h> int main() { int i = 0, n = 0, ret = 1, sum = 0; scanf("%d", &n); for (i = 1; i <= n; i++) { ret *= i; sum += ret; } printf("%d\n", sum); return 0; } ``` 该程序可以计算从 `1!` 到 `n!` 的所有阶乘之和。 #### 4. 题目四:在有序数组中查找指定数字 以下是一个简单的二分查找算法实现,用于在一个升序排列的数组中查找特定数字: ```c #include <stdio.h> int binarySearch(int arr[], int left, int right, int target) { while (left <= right) { int mid = left + (right - left) / 2; if (arr[mid] == target) { return mid; } else if (arr[mid] < target) { left = mid + 1; } else { right = mid - 1; } } return -1; } int main() { int arr[] = {1, 3, 5, 7, 9, 11}; int n = sizeof(arr) / sizeof(arr[0]); int target; scanf("%d", &target); int result = binarySearch(arr, 0, n - 1, target); if (result == -1) { printf("Element not found\n"); } else { printf("Element found at index %d\n", result); } return 0; } ``` 这段代码通过二分查找法快速定位目标值的位置。 #### 5. 关于变量声明与定义的区别 在C语言中,变量的声明仅告知编译器变量的存在及其类型,而不会分配内存;而变量的定义不仅声明了变量,还会为其分配实际的存储空间。例如: ```c extern int x; // 声明 int x = 10; // 定义 ``` 函数的声明与定义也遵循类似的规则[^3]。 --- ### 学习资料推荐 1. **《C程序设计语言》**(The C Programming Language)——由Dennis M. Ritchie和Brian W. Kernighan编写,是学习C语言的经典教材。 2. **在线资源**:如菜鸟教程、优快云博客等网站提供了丰富的C语言学习资料和实例代码。 3. **LeetCode/Codeforces**:这些平台包含大量C语言相关的编程练习题,适合初学者和进阶学习者。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值