一、数组输入输出
#include<stdio.h>
int main()
{
int a[100];
int n;
scanf(“%d”, &n);
for (int i = 0; i < n; i++)
scanf(“%d”, &a[i]);
for (int i = 0; i < n; i++)
printf(“%d “, a[i]);
}
二、链表的输入输出
#include<stdio.h>
#include<malloc.h>
struct node//先定义一个结构体,里面包含数据与结构体指针
{
int data;
struct node* next;
};
int main()
{
struct node* head, * tail, * p;
head = (struct node*)malloc(sizeof(struct node));//先在头指针开辟一个存储空间,但是不储存数据
head->data = 9;
head->next = NULL;//初始化头指针指向空指针
tail = NULL;//尾指针初始化为空指针
int x;
int n;
scanf(”%d”, &n);//输入想要的数字n个
int i = 0;
//输入数组
while (i<n)
{
p = (struct node*)malloc(sizeof(struct node));
scanf(“%d”, &x);
p->data = x;
p->next = NULL;//p的指向下一个指针为空指针
if (head->next == NULL)
head->next = p;//固定头指针指向的指针,然后保持不变
else
tail->next = p;//此时的空间是新开辟的空间,tail->next即是上一个空间p->next
tail = p;//为指针指向p,可以理解作为一个桥梁,用来储存是一个指向的空间
i++;
}
//输出数组
int j;
for (j = 0, p = head->next; j < n; p = p->next,j++)
printf("%d ", p->data);
return 0;
}
C语言中数组和链表的输入输出
最新推荐文章于 2024-07-06 11:13:36 发布