1.栈的顺序表及操作

顺序表就是用数组表示

#include <iostream>
#include <iomanip>
#include<vector>
using namespace std;

const int N = 100;

int a[N];
int top = -1;
void push_Stack(int x) {
	if (top == N - 1) {
		return;
	}
	a[++top] = x;
}
void pop_Stack() {
	if (top == -1) return;
	top--;
}

void top_Stack() {
	cout << a[top - 1];
}
bool Is_Empty() {
	if (top == -1) return 1;
	else return 0;
}
void print() {
	for (int i = 0; i <= top; i++) {
		cout << a[i] << " ";
	}
}
int main() {
	push_Stack(1);
	push_Stack(2);
    push_Stack(3);
    push_Stack(4);
	print();

}

2.链式表示就是用链表表示他的操作

#include <iostream>
#include <iomanip>
#include<vector>
using namespace std;

struct node {
	int data;
	node* next;
};
typedef struct node node;
node* top;
node* Init_Stack() {
	
	top = NULL;
	return top;
}
void push_Stack(int x) {
	node* temp = new node();
	temp->data = x;
	temp->next = top;
	top = temp;
}
void pop_Stack() {
	node* temp=top;
	if (top == NULL) {
		return;
	}
	top =temp->next;
	
	delete(temp);
}
void top_Stack() {
	cout << top->data;
}
void print() {
	node* temp;
	temp = top;
	while (temp != NULL) {
		cout << temp->data << " ";
		temp = temp->next;
	}
}
int main() {
	Init_Stack();
	push_Stack(1);
	push_Stack(2);
	push_Stack(6);
	pop_Stack();
    push_Stack(3);
    push_Stack(4);
	print();

}

7-1 银行业务队列简单模拟

设某银行有A、B两个业务窗口,且处理业务的速度不一样,其中A窗口处理速度是B窗口的2倍 —— 即当A窗口每处理完2个顾客时,B窗口处理完1个顾客。给定到达银行的顾客序列,请按业务完成的顺序输出顾客序列。假定不考虑顾客先后到达的时间间隔,并且当不同窗口同时处理完2个顾客时,A窗口顾客优先输出。

输入格式:

输入为一行正整数,其中第1个数字N(1000)为顾客总数,后面跟着N位顾客的编号。编号为奇数的顾客需要到A窗口办理业务,为偶数的顾客则去B窗口。数字间以空格分隔。

输出格式:

按业务处理完成的顺序输出顾客的编号。数字间以空格分隔,但最后一个编号后不能有多余的空格。

输入样例:

8 2 1 3 9 4 11 13 15

输出样例:

1 3 2 9 11 4 13 15
#include<stdio.h>
int main()
{
    int A[1000],head1=0,tail1=0;
    int B[1000],head2=0,tail2=0;
    int n,num;
    int i,j;
    scanf("%d",&n);
    for(i=0;i<n;i++){
        scanf("%d",&num);
        if(num%2==0)B[tail2++]=num;
        else A[tail1++]=num;
    }
    n=1;
    while(head1!=tail1 || head2!=tail2){
        for(j=0;j<2;j++){
            if(head1!=tail1 && n==1){printf("%d",A[head1++]);n++;}
            else if(head1!=tail1 && n!=1){printf(" %d",A[head1++]);n++;}
        }
        
        if(head2!=tail2 && n==1){
            printf("%d",B[head2++]);n++;
        }else if(head2!=tail2 && n!=1){
            printf(" %d",B[head2++]);n++;
        }
    }
    return 0;
}

 

posted on 2024-07-25 20:34  fafrkvit  阅读(2)  评论(0编辑  收藏  举报