【题目内容】
编写一个程序,根据功能列表对数组进行操作。数组:[1,2,3,4,5],将数组存入数组、链表或文件中。
【输出形式】
请选择:1存入数组 2 存入链表 3 存入文件\n存入成功
【测试用例1】
输入:
3
输出:
请选择:1存入数组 2 存入链表 3 存入文件存入成功
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct node {
int data;
struct node * next;
} Node;
int main() {
int a[5],b[5]={1,2,3,4,5},i,x;
printf("请选择:1存入数组 2 存入链表 3 存入文件\n");
scanf("%d",&x);
if(x==1){
for(i=0;i<5;i++)
a[i]=b[i];
printf("存入成功");
}
if(x==2){
Node * head = (Node * )malloc(sizeof(Node));
head->next = NULL;
Node * r, * newNode;
r = head;
for(i=0;i<5;i++) {
newNode = (Node * )malloc(sizeof(Node));
newNode->data = b[i];
newNode->next = r->next;
r->next = newNode;
r = newNode;
}
r->next = NULL;
printf("存入成功");
}
if(x==3){
FILE *p;
p=fopen("123456.txt","w");//写入txt
for(i=0;i<5;i++)
fprintf(p,"%d",b[i]);
fclose(p);
printf("存入成功");
}
return 0;
}
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!