代码拉取完成,页面将自动刷新
同步操作将从 柳婼/Aha-Algorithms-practise 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
2.4 链表.指针概念
malloc(4)//从内存中申请分配4个字节大小的内存空间
malloc(sizeof(int))//还可以使用 sizeof(int)获取 int 类型所占用的字节数
int *p;
p = (int *)malloc(sizeof(int));//想要操作这个空间,就需要用一个指针来指向这个空间
malloc 函数的返回类型是 void * 类型。void * 表示未确定类型的指针。在 C 和 C++中,
void * 类型可以强制转换为任何其他类型的指针。上面代码中我们将其强制转化 为整型指针,
以便告诉计算机这里的 4 个字节作为一个整体用来存放整数。
指针就是用来存储内存地址的,为什么要分不同类型的指针呢?因为指针变量存储的是一个内存空间的首地址(第一个字节的地址),
但是这个空间占用了多少个字节,用来存储什么类型的数,则是由指针的类型来标明的。这样系统才知道应该取多少个连续内存作为一个数据。
链表结构:
struct node {
int data;
struct node *next;
};
建立链表:
struct node *head;
head = NULL;
创建一个结点,并用临时指针指向这个结点:
struct node *p;
p = (struct node *)malloc(sizeof(struct node));
int t;
cin >> t;
p->data = t;
p->next = NULL;
创建一个链表全过程:
#include <iostream>
using namespace std;
struct node {
int data;
struct node *next;
};
int main() {
struct node *head, *p, *q, *t;
int n, a;
cin >> n;
head = NULL;
for (int i = 1; i <= n; i++) {
cin >> a;
p = (struct node *)malloc(sizeof(struct node));
p->data = a;
p->next = NULL;
if (head == NULL) {
head = p;
}else {
q->next = p;
}
q = p;
}
//在链表中插入一个结点(按顺序)
int b;
cin >> b;
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) {
cout << t->data;
t = t->next;
}
return 0;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。