栈和队列的基本操作及应用

栈和队列(顺序、链式)



题目

商店货架以栈的方式摆放商品。生产日期越近的越靠近栈底,出货时从栈顶取货。一天营业结束,如果货架不满,则需上货。入货直接将商品摆放到货架上,则会使生产日期越近的商品越靠近栈顶。这样就需要倒货架,使生产日期越近的越靠近栈底。

保存商品的信息可以包括商品名、生产日期。



思路

天天活在ddl中,习惯最后一天写作业。

S T L STL STL模板库的功能写的函数。

栈:
在这里插入图片描述

队列:(back()没写)
在这里插入图片描述
同时,我把函数全放结构体里了,这样模拟时就可以像用 S T L STL STL 库一样顺手了。

(说不定课设什么的还要用,懒得再写一遍了)

然后按实验报告的要求模拟一遍就可以了。
倒货架时,用一个临时栈 s t a 2 sta2 sta2 和一个队列 q u e que que 模拟。



顺序存储

多写了一个函数 i s f u l l ( ) isfull() isfull() 判断栈/队列有没有满。
不能压行好痛苦昂。

#include<stdio.h>
#include<string>
#include<iostream>
using namespace std;

const int Max_num=1e3;

struct Date{
    int year,month,day;
    Date(){}
    Date(int y,int m,int d){
        year=y;
        month=m;
        day=d;
    }
};

struct Goods{
	string name;
	Date date;
	Goods(){}
    Goods(string _name,Date _date){
        name=_name;
        date=_date;
    }
};
Goods input(){
	string _name;
	int year,month,day;
	cin>>_name>>year>>month>>day;
	Date _date=Date(year,month,day);
	Goods ret=Goods(_name,_date);
	return ret;
}
void print(Goods g){
	cout<<g.name<<' '<<g.date.year<<'.'<<g.date.month<<'.'<<g.date.day<<endl;
}
struct Stackk{
	Goods s[Max_num+17];
	int index;
	void init(){
		index=0;
	}
	bool empty(){
		return index==0;
	}
	bool isfull(){
		return index>=Max_num;
	}
	int size(){
		return index;
	}
	void push(Goods x){
		if(isfull()) {
			puts("erroe_push");
			exit(0);
		} 
		s[index++]=x;
	}
	void pop(){
		if(empty()) {
			puts("erroe_pop");
			exit(0);
		}
		index--;
		
	}
	Goods top(){
		if(empty()) {
			puts("erroe_top");
			exit(0);
		}
		return s[index-1];
	}
};

struct Queuee{
	Goods q[Max_num+17];
	int l,index;
	void init(){
		l=0,index=0;
	}
	bool empty(){
		return l==index;
	}
	bool isfull(){
		return index>=Max_num;
	}
	int size(){
		return index-l;
	}
	void push(Goods x){
		if(isfull()) {
			puts("erroe_push");
			exit(0);
		} 
		q[index++]=x;
	}
	void pop(){
		if(empty()) {
			puts("erroe_pop");
			exit(0);
		}
		l++;
	}
	Goods front(){
		if(empty()){
			puts("erroe_front");
			exit(0);
		}
		return q[l];
	}
};

int main(){
	int n,m,k;	//原有 n 个货品,卖出 m 件,入库 k 件
	Stackk sta;
	sta.init();
	// printf("here");
	Queuee que;
	que.init();
	Goods tmp;
	
	//原有 n 件货品
	cin>>n;
	for(int i=0;i<n;++i) {
		tmp=input();
		// print(tmp);
		sta.push(tmp);
	}
	
	//卖出 m 件货品
	cin>>m;
	n-=m;
	printf("\nThe goods sold:\n");
	for(int i=0;i<m;++i) {
		tmp=sta.top();
		sta.pop();
		print(tmp);
	}

	//入库 k 件货品
	printf("\nNew :\n");
	cin>>k;	
	n+=k;
	for(int i=0;i<k;++i){
		tmp=input();
		que.push(tmp);
	}
	//倒货架
	Stackk sta2;
	sta2.init();
	while(!sta.empty()){
		tmp=sta.top();
		sta.pop();
		sta2.push(tmp);
	}
	while(!sta2.empty()){
		tmp=sta2.top();
		sta2.pop();
		que.push(tmp);
	}
	while(!que.empty()){
		tmp=que.front();
		que.pop();
		sta.push(tmp);
	}

	//此时货架上的货品
	printf("\nNow :\n");
	while(!sta.empty()){
		tmp=sta.top();
		sta.pop();
		print(tmp);
	}
	return	0;
}

链式储存

多写了一个函数 d e s ( ) des() des() 清空栈/队列。
但我主函数运行结束时,所有栈/队列都是空的,所以并没有用到这个函数。

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<string>
#include<iostream>
using namespace std;

const int Max_num=1e3;

struct Date{
    int year,month,day;
    Date(){}
    Date(int y,int m,int d){
        year=y;
        month=m;
        day=d;
    }
};
struct Goods{
	string name;
	Date date;
	Goods(){}
    Goods(string _name,Date _date){
        name=_name;
        date=_date;
    }
};
Goods input(){
	string _name;
	int year,month,day;
	cin>>_name>>year>>month>>day;
	Date _date=Date(year,month,day);
	Goods ret=Goods(_name,_date);
	return ret;
}
void print(Goods g){
	cout<<g.name<<' '<<g.date.year<<'.'<<g.date.month<<'.'<<g.date.day<<endl;
}

typedef struct LNode{
    Goods data;
    struct LNode *next;
}LNode,*pNode;

struct Stackk{
	int sz;
	pNode base,topp;
	void init(){
		sz=0;
		base=(pNode)malloc(sizeof(LNode));
		base->next=NULL;
		topp=base;
	}
	bool empty(){
		return base==topp;
	}
	int size(){
		return sz;
	}
	void push(Goods x){
	    pNode p=(pNode)malloc(sizeof(LNode));
	    p->next=NULL;
	    topp->data=x;
	    topp->next=p;
	    topp=p;
	    sz++;
	}
	void pop(){
		if(empty()){
			puts("error_pop");
			exit(0);
		}
		pNode p=base;
		while(p->next!=topp)p=p->next;
		pNode q=p->next;
		p->next=NULL;
		topp=p;
		free(q);
		sz--;
	}
	Goods top(){
		if(empty()) {
			puts("erroe_top");
			exit(0);
		}
		pNode p=base;
		while(p->next!=topp)p=p->next;
		return p->data;
	}
	void des(){
		while(!empty()) pop();
	}
};

struct Queuee{
	int sz;
	pNode head,la;
	void init(){
		sz=0;
		head=(pNode)malloc(sizeof(LNode));
		head->next=NULL;
        la=head;
	}
	bool empty(){
		return head==la;
	}
	int size(){
		return sz;
	}
	void push(Goods x){
		pNode p=(pNode)malloc(sizeof(LNode));
        p->next=NULL;
        la->data=x;
        la->next=p;
        la=p;
        sz++;
	}
	void pop(){
		if(empty()) {
			puts("erroe_pop");
			exit(0);
		}
		pNode p=head;
        head=head->next;
        free(p);
        sz--;
	}
	Goods front(){
		if(empty()){
			puts("erroe_front");
			exit(0);
		}
		return head->data;
	}
	void des(){
		while(!empty()) pop();
	}
};

int main(){
	int n,m,k;	//原有 n 个货品,卖出 m 件,入库 k 件
	Stackk sta;
	sta.init();
	// printf("here");
	Queuee que;
	que.init();
	Goods tmp;
	
	//原有 n 件货品
	cin>>n;
	for(int i=0;i<n;++i) {
		tmp=input();
		// print(tmp);
		sta.push(tmp);
	}
	//卖出 m 件货品
	cin>>m;
	n-=m;
	printf("\nThe goods sold:\n");
	for(int i=0;i<m;++i) {
		tmp=sta.top();
		sta.pop();
		print(tmp);
	}

	//入库 k 件货品
	printf("\nNew:\n");
	cin>>k;	
	n+=k;
	for(int i=0;i<k;++i){
		tmp=input();
		que.push(tmp);
	}
	//倒货架
	Stackk sta2;
	sta2.init();
	while(!sta.empty()){
		tmp=sta.top();
		sta.pop();
		sta2.push(tmp);
	}
	while(!sta2.empty()){
		tmp=sta2.top();
		sta2.pop();
		que.push(tmp);
	}
	while(!que.empty()){
		tmp=que.front();
		que.pop();
		sta.push(tmp);
	}

	//此时货架上的货品
	printf("\nNow : \n");
	while(!sta.empty()){
		tmp=sta.top();
		sta.pop();
		print(tmp);
	}
	return	0;
}

生成测试数据

这题输入太麻烦了,像对拍的 d a t a data data 写个自动生成测试数据的程序。

#include <string>
#include <iostream>
#include <algorithm>
#include <stdlib.h>
#include <time.h>
using namespace std;

const int Max_num=1e3;

struct Date{
    int year,month,day;
    Date(){}
    Date(int y,int m,int d){
        year=y;
        month=m;
        day=d;
    }
    friend bool operator < (Date x,Date y){
        if(x.year!=y.year) return x.year<y.year;
        if(x.month!=y.month) return x.month<y.month;
        return x.day<y.day;
    }
};

struct Goods{
    string name;
    Date date;
    Goods(){}
    Goods(string _name,Date _date){
        name=_name;
        date=_date;
    }
    Goods input(){
        Goods ret;
        cin>>ret.name>>ret.date.year>>ret.date.month>>ret.date.day;
        return ret;
    }
    void print(){
        cout<<name<<' '<<date.year<<'.'<<date.month<<'.'<<date.day<<endl;
    }
}g[Max_num+17];

bool cmp(Goods i,Goods j){return j.date<i.date;}

int main(){
    srand(time(0));
    int n=rand()%10+1;
    int m=rand()%n+1;
    int k=rand()%10+1;
    for(int i=0;i<n;++i){
        string na;
        na+=('a'+rand()%26);
        na+=('0'+rand()%10);
        int y=rand()%5+2010;
        int m=rand()%12+1;
        int d=rand()%30+1;
        Date da=Date(y,m,d);
        g[i]=Goods(na,da);
    }
    sort(g,g+n,cmp);
    cout<<n<<endl;
    for(int i=0;i<n;++i){
       cout<<g[i].name<<' '<<g[i].date.year<<' '<<g[i].date.month<<' '<<g[i].date.day<<endl;
    }
    cout<<m<<endl;
    cout<<k<<endl;
    for(int i=0;i<k;++i){
        string name;
        name+=('a'+rand()%26);
        name+=('0'+rand()%10);
        int year=rand()%5+2015;
        int month=rand()%12+1;
        int day=rand()%30+1;
        Date date=Date(year,month,day);
        g[i]=Goods(name,date);
    }   
    sort(g,g+k,cmp);
    for(int i=0;i<k;++i){
           cout<<g[i].name<<' '<<g[i].date.year<<' '<<g[i].date.month<<' '<<g[i].date.day<<endl;
    }
    return 0;
}

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值