使用数据结构知识,用c编写一个电子相册
时间: 2023-02-21 09:26:15 浏览: 76
如果要使用数据结构知识来编写电子相册,可以使用链表存储每一张照片的信息,比如照片的名称、拍摄日期、描述等。每一个结点代表一张照片,包含照片的信息,通过链表串联所有结点,就可以得到一个电子相册。
以下是C语言代码的示例:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Photo {
char name[100];
char date[100];
char description[100];
struct Photo *next;
};
int main() {
struct Photo *album = NULL;
struct Photo *current, *previous;
// 循环输入多张照片的信息,直到用户输入结束命令
while (1) {
current = (struct Photo *)malloc(sizeof(struct Photo));
printf("Enter photo name (enter 'end' to stop): ");
scanf("%s", current->name);
if (strcmp(current->name, "end") == 0) {
break;
}
printf("Enter photo date: ");
scanf("%s", current->date);
printf("Enter photo description: ");
scanf("%s", current->description);
current->next = NULL;
if (album == NULL) {
album = current;
} else {
previous->next = current;
}
previous = current;
}
// 遍历链表,打印所有照片的信息
printf("\nElectronic Album:\n");
current = album;
while (current != NULL) {
printf("Name: %s\n", current->name);
printf("Date: %s\n", current->date);
printf("Description: %s\n", current->description);
printf("---\n");
current = current->next;
}
return 0;
}
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)