帮我用c语言链表写一个酒水进货管理系统
时间: 2023-05-19 19:00:59 浏览: 63
好的,我可以帮你用 C 语言链表写一个酒水进货管理系统。以下是一个简单的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
char name[20];
int quantity;
float price;
} Item;
typedef struct Node {
Item item;
struct Node* next;
} Node;
Node* head = NULL;
void add_item() {
Node* new_node = (Node*) malloc(sizeof(Node));
printf("Enter item name: ");
scanf("%s", new_node->item.name);
printf("Enter item quantity: ");
scanf("%d", &new_node->item.quantity);
printf("Enter item price: ");
scanf("%f", &new_node->item.price);
new_node->next = head;
head = new_node;
}
void display_items() {
Node* current = head;
printf("Name\tQuantity\tPrice\n");
while (current != NULL) {
printf("%s\t%d\t\t%.2f\n", current->item.name, current->item.quantity, current->item.price);
current = current->next;
}
}
void search_item() {
char name[20];
printf("Enter item name to search: ");
scanf("%s", name);
Node* current = head;
while (current != NULL) {
if (strcmp(current->item.name, name) == 0) {
printf("Name\tQuantity\tPrice\n");
printf("%s\t%d\t\t%.2f\n", current->item.name, current->item.quantity, current->item.price);
return;
}
current = current->next;
}
printf("Item not found.\n");
}
void delete_item() {
char name[20];
printf("Enter item name to delete: ");
scanf("%s", name);
Node* current = head;
Node* previous = NULL;
while (current != NULL) {
if (strcmp(current->item.name, name) == 0) {
if (previous == NULL) {
head = current->next;
} else {
previous->next = current->next;
}
free(current);
printf("Item deleted.\n");
return;
}
previous = current;
current = current->next;
}
printf("Item not found.\n");
}
int main() {
int choice;
while (1) {
printf("\n1. Add item\n");
printf("2. Display items\n");
printf("3. Search item\n");
printf("4. Delete item\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
add_item();
break;
case 2:
display_items();
break;
case 3:
search_item();
break;
case 4:
delete_item();
break;
case 5:
exit(0);
default:
printf("Invalid choice.\n");
}
}
return 0;
}
```
这个程序使用链表来存储酒水的名称、数量和价格,并提供了添加、显示、搜索和删除酒水的功能。你可以根据自己的需求进行修改和扩展。