数据结构(栈和队列)

[color=red][b]商品货架管理[/b][/color]
[b][color=blue]1、用栈和队列实现[/color][/b]
#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();
}*/
}

[b][color=blue]2、用两个栈实现[/color][/b]
#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();
}*/
}
  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值