#include "stdio.h"
#include "stdlib.h" //malloc
#include "string.h"
typedef struct {
char no[4]; //4位编号
char name[20]; //品牌
int peace; //价格
}TV;
//链表结点的定义:
typedef struct LNode {
TV data; //数据域
struct LNode* next; //指针域
}LNode, * LinkList;
//电视机信息
void in(LNode* head)
{
LinkList p, r; //p指向新结点; r指向当前链表的尾结点
int i, n; //n表示电视机数量
r = head;
printf("您想要输入多少种电视机信息呢?");
scanf("%d", &n);
printf("开始输入:\n");
for (i = 1; i <= n; i++)
{
p = (LinkList)malloc(sizeof(LNode));
p->next = NULL;
printf("编号:");
scanf("%s", p->data.no);
printf("品牌:");
scanf("%s", p->data.name);
printf("价格:");
scanf("%d", &p->data.peace);
r->next = p;
r = r->next;
printf("-----------------------------------------\n");
}
printf("********************************************************\n");
}
//输出电视机信息
void out(LNode* head)
{
LinkList p;
p = head;
printf("请输出电视机的信息:\n\n");
printf("序号\t品牌\t价格\n");
while (p->next != NULL)
{
p = p->next;
printf("%s\t%s\t%d\n", p->data.no, p->data.name, p->data.peace);
}
}
void research_1(LNode* head, TV e)
{
LinkList p;
p = head;
printf("请输入查找的品牌:\n");
scanf("%s", e.name);
while ((p->next != NULL) && strcmp(e.name, p->data.name))
{
p = p->next;
}
printf("序号 %s\t 价格 %d\n", p->data.no, p->data.peace);
}
void research_2(LNode* head, int i)
{
int k;
LinkList p;
printf("请输入您要查询的电视机编号:");
scanf("%d", &i);
if (i < 0)
{
printf("抱歉,没有为您查到这条信息\n");
}
p = head; k = 0;
while (p->next != NULL && k < i)
{
p = p->next;
k = k + 1;
}
if (i == k)
printf("序号:%s\t 品牌: %s\t价格: %d\n", p->data.no, p->data.name, p->data.peace);
else
printf("查找失败!\n");
}
void insert(LNode* head, int i, TV e)
{
int k;
LinkList p, r;
r = head; k = 0;
printf("您为这台电视准备的编号是:\n");
scanf("%d", &i);
printf("请输入电视机信息:\n");
printf("序号:\n");
scanf("%s", e.no);
printf("品牌:\n");
scanf("%s", e.name);
printf("价格:\n");
scanf("%d", &e.peace);
while (r != NULL && k < i - 1)
{
r = r->next;
k = k + 1;
}
if (r == NULL)
{
printf("插入失败!\n");
}
p = (LinkList)malloc(sizeof(LNode));
p->data = e;
p->next = r->next;
r->next = p;
printf("插入成功!\n");
}
void delete_1(LNode* head, int i, TV* e)
{
LinkList p, r;
int k;
r = head; k = 0;
printf("请输入你要删除的位置:\n");
scanf("%d", &i);
while (r->next != NULL && k < i - 1)
{
r = r->next;
k++;
}
if (r == NULL)
{
printf("删除失败!\n");
}
p = r->next;
r->next = p->next;
*e = p->data;
free(p);
printf("删除成功!\n");
}
void number(LNode* head)
{
int i;
LinkList p;
p = head->next; i = 0;
while (p != NULL)
{
p = p->next;
i++;
}
printf("电视机总数为:%d\n", i);
printf("\n");
}
void exit_1()
{
printf("退出成功!请按任意键结束!");
exit(0);
}
int main()
{
system( "color F1 ");
//system( "pause ");
//return 0;
LNode* head;
head = (LinkList)malloc(sizeof(LNode));
head->next = NULL;
int select = -1;
int i;
TV e;
printf("********************************************************************\n");
printf("@ 1. 想要输入的品牌数量,并填写电视机信息\n");
printf("@ 2. 为您查看系统中的电视机信息 \n");
printf("@ 3. 为您查询电视机品牌\n");
printf("@ 4. 为您查找相应编号的电视机\n");
printf("@ 5. 在库存中新建一条电视机信息\n");
printf("@ 6. 为您删除指定编号的电视机信息 \n");
printf("@ 7. 库存电视机的数量\n");
printf("@ 8. EXIT\n");
printf("********************************************************************\n");
printf("\n");
while (select != 0)
{
printf("您想要做什么?");
scanf("%d", &select);
printf("\n");
switch (select)
{
case 1: in(head); break;
case 2: out(head); break;
case 3: research_1(head, e); break;
case 4: research_2(head, i); break;
case 5: insert(head, i, e); break;
case 6: delete_1(head, i, &e); break;
case 7: number(head); break;
case 8: exit_1(); break;
}
}
return 0;
}
C语言链表——电视机管理系统
最新推荐文章于 2022-06-06 13:43:39 发布