答案来自网络,如果有错误请联系我更正(772173629),谢谢合作!
一、选择
- D、A
- A
- C
- D
- C
- B
- C
- C
- C
- A
二、填空
- 栈顶
- 链式栈
- 空
- 不可能
- O(1)
- A、D
- S=NULL
- 链表头、链表头
- 头结点的指针域为空
- 将单链表的首结点指针赋空值
三、判断
- 错
- 错
- 错
- 对
- 错
- 错
- 对
- 错
- 错
- 错
四、简答题:
1、可能的次序有CDBAE、CDEBA、CDBEA
2、高级语言变量名:以字母开头的字母数字串。
以A最先出栈的高级语言变量名有AP321
以P最先出栈的高级语言变量名有P321A,P32A1,P3A21,PA321
3、(1)能够得到,操作序列为AAADDAADADDD
(2)不能得到,执行ADAAAADDAD,得到输出序列1546后,栈中元素从栈顶到栈底为32,不能让2先出栈,所以得不到输出序列154623.
五、算法设计
1、配对返回1,不配对返回0
int match(){char a[],int n} {
char st[maxsize];
int top = -1;
int i=0 ,flag = 1;
while(i<n&&flag=1) {
if(a[i]=='('||a[i]=='['||a[i]=='{') {
top++;
st[top]=a[i];
}
else if(a[i]==')'){
if(st[top]=='(') top--;
else flag = 0;
}
else if(a[i]==']') {
if(st[top]=='[') top--;
else flag = 0;
}
else if(a[i]=='}') {
if(st[top]=='{') top--;
else flag = 0;
}
i++;
}
if(top>=0) flag = 0;
return flag ;
}
2、
int total = 4;
char str[]='1234';
int sum =0;
void A(int m,int a[],int curp) {
int x,i;
if(m>total&&empty()){
cout<<" ";
for(int i=0;i<curp;i++) {
cout<<str[a[i]-1];
}
cout<<endl;
sum++;
}
if(m<=total) {
push(m);
A(m+1,a,curp);
pop();
}
if(!empty()) {
x=pop();
a[curp]=x;
curp++;
A(m,a,curp);
push(x);
}
}
void main() {
int a[maxsize];
initstack();
cout<<"所有出栈序列"<<endl;
A(1,a,0);
cout<<"出栈序列个数:"<<sum<<endl;
}
3、
typedef struct{
ElemType a[maxsize] ; //栈s1和s2的共享空间
int top1,top2;
} stack;
//栈初始化
void InitStack(stack &st) {
st.top1 = -1;
st.top2 = maxsize;
}
//判断栈空
int StackEmpty( stack st,int i) {
if(i=1) return st.top1==-1;
else return st.top2=maxsize;
}
//进栈
int Push(stack &st,int i,ElemType x) {
if(st.top1==st.top2-1) return 0;
if(i==1){
st.top1++;
st.a[st.top2]=x;
} else if(i==2) {
st.top2--;
st.a[st.top2]=x;
} else {
return 0;
}
return 1;
}
//出栈
int Pop(stack &st,int i,ElemType &x) {
if(i==1) {
if(st.top1==-1) return 0;
else {
x=st.a[st.top1];
st.top1--;
}
}
else if(i==2) {
if(st.top2==maxsize) return 0;
else {
x = st.a[st.top2];
st.top2++;
}
}
else{ return 0; }
return 1;
}
4、
int GetBottom(SqStack st,ElemType &x) {
ElemType e;
SqStack tmpst;
InitStack(tmpst);
if(StackEmpty(st)) {
return 0;
}
while(!StackEmpty(st)) {
Pop(st,x);
Push(tmpst,x);
}
while(!StackEmpty(tmpst)) {
Pop(tmpst,e);
Push(st,e);
}
return 1;
}
5、
#include <iostream.h>
struct Node
{
int data;
Node *next;
};
class LinkQueue
{
private:
Node *front;
Node *rear;
public:
LinkQueue();
~LinkQueue();
void EnQueue(int e);
int DeQueue();
void QueueDisplay();
};
LinkQueue::LinkQueue()
{
front=new Node;
front->next=NULL;
rear=front;
};
LinkQueue::~LinkQueue()
{
Node *p;
while(front!=NULL)
{
p=front;
front=front->next;
delete p;
}
};
void LinkQueue::EnQueue(int e)
{
Node *s;
s=new Node;
s->data=e;
s->next=rear->next;
rear->next=s;
rear=s;
if(front->next==NULL)
front->next=s;
};
int LinkQueue::DeQueue()
{
int e;
Node *p;
if(rear==front)
{
cout<<"Empty!";
return -1;
}
p=front->next;
e=p->data;
front->next=p->next;
if(p->next==NULL)
rear=front;
delete p;
return e;
};
void LinkQueue::QueueDisplay()
{
Node *p;
p=front;
while(p->next)
{
p=p->next;
cout<<p->data<<endl;
}
};
void main(void)
{
int com,patient;
int outpatient;
LinkQueue plist;
while(true)
{
cout<<"选择操作:"<<endl;
cout<<"1.病人排队"<<endl;
cout<<"2.病人就诊"<<endl;
cout<<"3.查看所有病历号"<<endl;
cout<<"4.列出所有病历号并退出"<<endl;
cout<<"5.下班退出"<<endl;
cin>>com;
switch(com)
{
case 1:
cout<<"输入正整数病历号:"<<endl;
cin>>patient;
plist.EnQueue(patient);
break;
case 2:
outpatient=plist.DeQueue();
if(outpatient>0)
cout<<outpatient<<"病人就诊"<<endl;
break;
case 3:
cout<<"所有病历号:"<<endl;
plist.QueueDisplay();
break;
case 4:
cout<<"所有病历号:"<<endl;
plist.QueueDisplay();
return;
case 5:
return;
default:
cout<<"错误命令!"<<endl;
break;
};
};
}