📄 kcsj.c
字号:
#define LEN sizeof(struct mod)
#define NULL 0
#include "stdio.h" /*I/O函数*/
#include "stdlib.h" /*其它说明*/
#include "string.h" /*字符串函数*/
#include "conio.h" /*屏幕操作函数*/
#include "mem.h" /*内存操作函数*/
#include "ctype.h" /*字符操作函数*/
#include "alloc.h" /*动态地址分配函数*/
typedef struct mod
{
char name[10];
char brand[20];
int price;
int number;
struct mod *next;
}store;
void *init(); /*初始化函数*/
store *create(); /*创建链表*/
store *insert(); /*插入*/
store *delete(); /*删除*/
void *search(); /*查寻*/
void print(); /* 打印*/
main()
{
int choose;
printf("***************MENU***************\n"); /*定义菜单字符串数组*/
printf(" 0. init list\n"); /*初始化*/
printf(" 1. Enter list\n"); /*输入记录*/
printf(" 2. insert record on price\n "); /*插入记录到表中*/
printf(" 3. Delete a record on brand\n"); /*从表中删除记录*/
printf(" 4. print\n"); /* 打印*/
printf(" 5. Search record on brand\n"); /*按照价格查找记录*/
printf(" 6. quit\n ");
printf("请输入您要进行的操作:\n");
scanf("%d",&choose);
while(choose!=0&&choose!=1&&choose!=2&&choose!=3&&choose!=4&&choose!=5&&choose!=6)
{ printf("数字按错");
scanf("%d",&choose);
}
while(choose==0||choose==1||choose==2||choose==3||choose==4||choose==5||choose==6)
{switch(choose)
{
case 0:init();break; /*执行初始化*/
case 1:create();break; /*创建链表*/
case 2:insert();break; /*插入链表*/
case 3:delete();break; /*删除记录*/
case 4:print();break; /*打印全部记录*/
case 5:search();break; /*查询*/
case 6:exit(0);
}
}
}
void *init()
{
return NULL;
}
store *create()
{ struct mod *head;
struct mod *p1,*p2;
int n=0;
p1=p2=(struct mod *)malloc(LEN);
clrscr(); /*清屏*/
printf("please input record \n"); /*提示输入记录*/
printf("****name********brand*********price***********number****\n ");
printf("-------------------------------------------------------\n");
scanf("%c%c%ld%d\n",&p1->name,&p1->brand,&p1->price,&p1->number);
head=NULL;
while(p1->number!=0)
{n=n+1;
if(n==1) head=p1;
else p2->next=p1;
p2=p1;
p1=(struct mod *)malloc(LEN);
scanf("%c%c%ld%d\n",&p1->name,&p1->brand,&p1->price,&p1->number);
}
printf("******************************************************\n");
p2->next=NULL;
}
struct mod *delete(struct mod *head)
{ char brand[20];
struct mod *p1,*p2; /*p1为查找到要删除的结点指针,p2为其前驱指针*/
clrscr(); /*清屏*/
printf("请输入您所要删除的brand:\n ");
scanf("%c\n",&brand);
if(head==NULL){printf("list is null!\n");}
p1=head;
while( strcmp(brand,p1->brand)&&p1->next!=NULL)
{p2=p1;p1=p1->next;}
if(strcmp(brand,p1->brand)==0)
{if(p1==head)head=p1->next;
else p2->next=p1->next;
free(p1);
printf("delete:%s",brand) ;
}
else printf("%s not been found\n",brand);
return(head);
}
void print(struct mod *head)
{struct mod *p;
p=head;
clrscr();
if(head!=NULL)
do
{printf("%c",&p->name);
printf("%c",&p->brand);
printf("%ld",&p->price);
printf("%d",&p->number);
p=p->next;
}while(p!=NULL);
}
struct mod *insert(struct mod *head)
{ struct mod *p0,*p1,*p2;
struct mod *c;
p1=head;
p0=(struct mod *)malloc(LEN);
if(!p0)
{
printf("\nout of memory"); /*如没有申请到,内存溢出*/
return NULL; /*返回空指针*/
}
printf("请输入您所要插入的数据: ");
scanf("%c\n%c\n%ld\n%d\n",&p1->name,&p1->brand,&p1->price,&p1->number);
if(head==NULL)
{
head=p0;
p0->next=NULL;
}
else
{while((p0->price>p1->price)&&(p1->next!=NULL))
{p2=p1;
p1=p1->next;
}
if(p0->price<=p1->price)
{if(head==p1) head=p0;
else p2->next=p0;
p0->next=p1;
}
}
}
void *search(struct mod *head)
{
struct mod *p; /* 移动指针*/
char s[10]; /*存放品牌的字符数组*/
clrscr(); /*清屏幕*/
printf("please enter brand for search\n");
scanf("%s",&s); /*输入品牌*/
p=head; /*将头指针赋给p*/
if(p==NULL) /*如果指针为空*/
printf("\nlist no %s brand\n",s); /*显示没有该brand*/
if (strcmp(p->brand,s)&&p!=NULL) /*当记录的姓名不是要找的,或指针不为空时*/
p=p->next; /*移动指针,指向下一结点*/
else /*显示找到的记录信息*/
{ printf("%c",&p->name);
printf("%c",&p->brand);
printf("%ld",&p->price);
printf("%d",&p->number);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -