商品货架管理
1、用栈和队列实现
#include <stdafx.h>
#include <stdio.h>
#include <string.h>
#define MaxSize 3
//定义商品信息
typedef struct
{
char name[20]; //商品名称
long int date; //商品生产日期
}ShangPin;
//定义顺序栈
typedef struct
{
ShangPin sp[MaxSize];
int top;
}SeqStack;
//定义顺序队列
typedef struct
{
ShangPin sp[MaxSize];
int front;
int rear;
}SeqQueue;
//入栈操作即插入新商品
int push(SeqStack *S)
{
int i=0;
for(S->top=0;S->top<MaxSize-1;S->top++)
{
printf("请输入第%d件商品名称:",S->top+1);
scanf("%s",&S->sp[S->top].name);
printf("请输入对应生产日期:");
scanf("%d",&S->sp[S->top].date);
printf("\n");
}
return(--S->top);
}
//利用栈S和循环队列Q来进行倒货架
int daohuojia(SeqStack *S,SeqQueue *Q,int TOP)
{
ShangPin NewGoods;
S->top=TOP;
Q->front=Q->rear=0;
printf("请输入你要添加的新商品名称:");
scanf("%s",&NewGoods.name);
printf("请输入对应生产日期:");
scanf("%d",&NewGoods.date);
while(S->top!=-1&&(NewGoods.date-S->sp[S->top].date)>0)
//比较新旧商品的生产日期
{
strcpy(Q->sp[Q->rear].name,S->sp[S->top].name);
Q->sp[Q->rear].date=S->sp[S->top].date;
Q->rear=(Q->rear+1)%MaxSize;
S->top--;
}
S->top++;//将新商品插入货架
strcpy(S->sp[S->top].name,NewGoods.name);
S->sp[S->top].date=NewGoods.date;
for(Q->front=MaxSize-2;Q->front>-1&&Q->front<MaxSize;Q->front=(Q->front-1)%MaxSize)
{
S->top++;
strcpy(S->sp[S->top].name,Q->sp[Q->front].name);
S->sp[S->top].date=Q->sp[Q->front].date;
}
return(S->top);
}
//输出插入新商品后的货架商品顺序
void pop(SeqStack *S,int TOP)
{
printf("更新商品后的货架商品顺序为:\n");
printf("商品名称 生产日期\n");
for(S->top=TOP;S->top>=0;S->top--)
{
printf(" %s ",S->sp[S->top].name);
printf("%d\n",S->sp[S->top].date);
}
printf("货架整理结束!\n");
}
void main()
{
/*char q[2];
printf("按l键退出, 其他任意键开始倒货架!");
scanf("%s",&q);
if(strcmp(q,"l")==0){
return;
}else{*/
int TOP=0;
SeqStack A;
SeqQueue B;
TOP=push(&A);
TOP=daohuojia(&A,&B,TOP);
pop(&A,TOP);
/*getchar();
}*/
}
2、用两个栈实现
#include <stdafx.h>
#include <stdio.h>
#include <string.h>
#define MaxSize 3
//定义商品信息
typedef struct
{
char name[20]; //商品名称
long int date; //商品生产日期
}ShangPin;
//定义顺序栈
typedef struct
{
ShangPin sp[MaxSize];
int top;
}SeqStack;
//入栈操作即插入新商品
int push(SeqStack *S1)
{
int i=0;
for(S1->top=0;S1->top<MaxSize-1;S1->top++)
{
printf("请输入第%d件商品名称:",S1->top+1);
scanf("%s",&S1->sp[S1->top].name);
printf("请输入对应生产日期:");
scanf("%d",&S1->sp[S1->top].date);
printf("\n");
}
return(--S1->top);
}
//利用栈S1和栈S2来进行倒货架
int daohuojia(SeqStack *S1,SeqStack *S2,int TOP)
{
ShangPin NewGoods;
S1->top=TOP;
S2->top=-1;
printf("请输入你要添加的新商品名称:");
scanf("%s",&NewGoods.name);
printf("请输入对应生产日期:");
scanf("%d",&NewGoods.date);
while(S1->top!=-1&&(NewGoods.date-S1->sp[S1->top].date)>0)
//比较新旧商品的生产日期
{
S2->top++;
strcpy(S2->sp[S2->top].name,S1->sp[S1->top].name);
S2->sp[S2->top].date=S1->sp[S1->top].date;
S1->top--;
}
S1->top++;//将新商品插入货架
strcpy(S1->sp[S1->top].name,NewGoods.name);
S1->sp[S1->top].date=NewGoods.date;
for(;S2->top>-1;S2->top--)
{
S1->top++;
strcpy(S1->sp[S1->top].name,S2->sp[S2->top].name);
S1->sp[S1->top].date=S2->sp[S2->top].date;
}
return(S1->top);
}
//输出插入新商品后的货架商品顺序
void pop(SeqStack *S1,int TOP)
{
printf("更新商品后的货架商品顺序为:\n");
printf("商品名称 生产日期:\n");
for(S1->top=TOP;S1->top>=0;S1->top--)
{
printf(" %s ",S1->sp[S1->top].name);
printf("%d\n",S1->sp[S1->top].date);
}
printf("货架整理结束!\n");
}
void main()
{
/*char q[2];
printf("按l键退出, 其他任意键开始倒货架!");
scanf("%s",&q);
if(strcmp(q,"l")==0){
return;
}else{*/
int TOP=0;
SeqStack A, B;
TOP=push(&A);
TOP=daohuojia(&A,&B,TOP);
pop(&A,TOP);
/*getchar();
}*/
}
分享到:
相关推荐
数据结构栈和队列的课程指导上几实验代码 相抵部分
栈和队列考试题 复习 栈和队列考试题 复习栈和队列考试题 复习
数据结构课件及课堂笔记 栈和队列 计算机类
数据结构栈和队列相关习题及答案
数据结构 栈和队列 教你重新认识栈和队列
数据结构 栈和队列的详细代码~数据结构 栈和队列的详细代码~数据结构 栈和队列的详细代码~数据结构 栈和队列的详细代码~
PPT内容是数据结构中有关栈和队列的知识,非常适合正在学习数据结构基础的同学
数据结构
该课件是数据结构栈和队列部分的,是基于C语言描述的数据结构
学生数据结构课程的教学,栈和队列课件,详细地介绍了栈和队列的使用方法。
基于c语言数据结构中栈和队列思想的简单停车场管理系统,以栈模拟停车场,以队列模拟车场外的便道,按照从终端读入的输入数据序列进行模拟管理。每一组输入数据包括三个数据项:汽车“到达”或“离去”信息、汽车...
数据结构栈和队列PP,绝对是原创的,这里貌似有关数据节构的资源不多,大家下了就看看吧
数据结构 栈和队列基本算法,我自己全部实现过了,没问题。
你是否也在学数据结构呢 是不是想要习题的参考答案呢 现在提供数据结构 栈和队列这一章的答案