1:第三章基础实验1.1
查看提交统计提问
总时间限制: 1000ms 内存限制: 2000kB
描述
设计字符顺序栈的基本运算程序,并用相关数据测试。
void InitStack(SqStack &st)//初始化顺序栈st
void DestroyStack(SqStack st)//销毁顺序栈st
int Push(SqStack &st,ElemType x)//进栈元素x
int Pop(SqStack &st,ElemType &x)//出栈元素x
int GetTop(SqStack st,ElemType &x)//取栈顶元素x
int StackEmpty(SqStack st)//判断栈是否为空
程序实现过程如下所示:
初始化栈st
判断栈是否为空
a进栈
b进栈
c进栈
d进栈
栈不空
栈顶元素:d
出栈次序:d c b a
销毁栈st
输入
a b c d
输出
Yes
d
d c b a
样例输入
a b c d
样例输出
Yes
d
d c b a
#include<iostream>
#include<cstdlib>
using namespace std;
const int MaxSize=100;
#define ElemType char
typedef struct
{
ElemType data[MaxSize];
int top;
}SqStack;
void InitStack(SqStack *&s)
{
s=(SqStack *)malloc(sizeof (SqStack));
s->top=-1;
}
void DestoryStack(SqStack *&s)
{
free(s);
}
bool StackEmpty(SqStack *&s)
{
return(s->top==-1);
}
bool Push(SqStack *&s,ElemType &e)
{
if(s->top==MaxSize)
return false;
s->top++;
s->data[s->top]=e;
return true;
}
bool Pop(SqStack *&s,ElemType &e)
{
if(s->top==-1)
return false;
e=s->data[s->top];
s->top--;
return true;
}
bool GetTop(SqStack *&s,ElemType &e)
{
if(s->top==-1)
return false;
e=s->data[s->top];
return true;
}
int main()
{
SqStack *s;
InitStack(s);
if(StackEmpty(s))
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
char e,f,g,h;
// do
// {
// cin>>e;
// Push(s,e);
// }while(getchar()!='\n');
cin>>e>>f>>g>>h;
Push(s,e);
Push(s,f);
Push(s,g);
Push(s,h);
if(GetTop(s,e)==1)
{
cout<<e<<endl;
}
while(GetTop(s,e))
{
cout<<e<<" ";
Pop(s,e);
}
cout<<endl;
DestoryStack(s);
return 0;
}
2:补充代码 - 栈
查看提交统计提问
总时间限制: 10000ms 内存限制: 10000kB
描述
补充栈编程代码实现判断给定的出栈入栈顺序是否可行。其中“I”表示入栈,“O”表示出栈,栈的初态和终态均必须为空。
输入
以“I”和“O”表示的出栈入栈顺序字符串
输出
如果给定的出入栈顺序在栈初始状态和最终状态均为空,而且不会出现上溢和下溢的情况下输出1,否则输出0。
样例输入
IIIOIOIO
样例输出
0
#include<iostream>
#include<string.h>
using namespace std;
#define MAX_SIZE 100
typedef char ElemType;
typedef struct {
ElemType data[MAX_SIZE];
int top;
} SqStack;
void InitStack(SqStack &st)
{
st.top=-1;
}
int PushStack(SqStack &st, ElemType x)
{ if (st.top == MAX_SIZE-1)
return 0;
else {
st.top++;
st.data[st.top] = x;
return 1;
}
}
int PopStack(SqStack &st, ElemType &x)
{
if (st.top == -1)
return 0;
else {
x = st.data[st.top];
st.top--;
return 1;
}
}
int isEmpty(SqStack st)
{
if (st.top == -1)
return 1;
else
return 0;
}
int OperationJudge(SqStack sqst, char opstr[], int n)
{
int i=0;
int j=0;
int flag=0;
while(i!=-1&&j<n)
{
if(opstr[j]=='I')
{
i++;
}
else
{
i--;
}
j++;
if(i==-1||i>n)
{
flag=1;
}
}
if(i==0&&flag==0)
return 1;
return 0;
}
int main()
{
SqStack st;
InitStack(st);
char opstr[MAX_SIZE];
cin >> opstr;
int islegal = OperationJudge(st, opstr, strlen(opstr));
cout << islegal << endl;
return 0;
}
3:链队列
查看提交统计提问
总时间限制: 10000ms 内存限制: 5000kB
描述
编写一个程序实现链队列(假设队列中元素类型为char)的各种基本运算,并在此基础上设计一个主程序完成如下功能:
(1)初始化队列q;
(2)判断队列q是否为空;
(3)依次进队列元素。输入两行数据,第一行是进队字符数据的个数,第二行是具体入队的字符数据。
3
a b c
(4)出队一个元素,并输出该元素;
(5)输出队列q的元素个数;
(6)依次进队列元素。输入两行数据,第一行是进队字符数据的个数,第二行是具体入队的字符数据。
3
d e f
(7)输出队列q的元素个数;
(8)输出出队序列;
(9)释放队列。
输入
按照程序要求输入。
输出
按照程序要求输出。
样例输入
3
a b c
3
d e f
样例输出
yes
a
2
5
b c d e f
#include<iostream>
#include<cstdlib>
using namespace std;
const int MaxSize=100;
#define ElemType char
typedef struct qnode
{
ElemType data;
struct qnode *next;
}DataNode;
typedef struct
{
DataNode *front;
DataNode *rear;
}LinkQuNode;
void InitQueue(LinkQuNode *&q)
{
q=(LinkQuNode *)malloc(sizeof(LinkQuNode));
q->front=q->rear=NULL;
}
void DestoryQueue(LinkQuNode *&q)
{
DataNode *p=q->front,*r;
if(p!=NULL)
{
r=p->next;
while(r!=NULL)
{
free(p);
p=r;
r=p->next;
}
}
free(p);
free(q);
}
bool QueueEmpty(LinkQuNode *q)
{
return(q->rear==NULL);
}
/*入队操作要注意别动指向队头的front指针
如果是q->front->next = p那每次就会插到队头。
而队列是队头出,队尾进。
也不要忘了新节点p->next = NULL;*/
void enQueue (LinkQuNode *&q,ElemType e)
{
DataNode * p ;
p =( DataNode *) malloc ( sizeof ( DataNode ));
p -> data = e ;//把e的值赋给p
p -> next = NULL ;//p是最后一个
if ( q -> front == NULL ) //如果队列为空
q -> front = q -> rear = p ;
else {
q -> rear -> next = p ;//尾节点的下一个指向p
q->rear=p;//尾节点指向p
}
}
bool deQueue ( LinkQuNode *& q , ElemType & e)
{
DataNode * t ;
if ( q -> front == NULL ) return false ;//队列为空
t = q -> front ; //指向第一个数据结点
if ( q -> front == q -> rear ) //队列中只有一个结点时
q -> front = q -> rear = NULL ;//把这个销毁就行了
else //有多个时
q -> front = q -> front -> next ;//让front下移一个
e = t -> data ;
free ( t );
return true ;
}
void enQueue1 (LinkQuNode *&q,ElemType e)
{
DataNode * p ;
p -> data = e ;//把e的值赋给p
q -> rear -> next = p ;//尾节点的下一个指向p
q->rear=p;
p -> next = NULL ;//p是最后一个
if ( q -> front == NULL ) //如果队列为空
q -> front = q -> rear = p ;
else {
q -> rear -> next = p ;//尾节点的下一个指向p
q->rear=p;//尾节点指向p
}
}
int getlength(LinkQuNode *&q)
{
int i=0;
DataNode *p=q->front;
while(p)
{
i++;
p=p->next;
}
return i;
}
void print(LinkQuNode *&q)
{
// q=(LinkQuNode *)malloc(sizeof(LinkQuNode));
DataNode *p=q->front;
// if (QueueEmpty(q)) {
// printf("空队\n");
// }
while (p) {
cout<<p->data<<" ";
p = p->next;
}
cout<<endl;
// cout<<p->data<<endl;
}
int main()
{
LinkQuNode *q;
InitQueue(q);
if(QueueEmpty(q)==1)
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
int n;
cin>>n;
char e;
for(int i=0;i<n;i++)
{
cin>>e;
enQueue (q ,e );
}
if(deQueue (q,e)==1)
{
cout<<e<<endl;
// deQueue (q , e);
}
print(q);
cout<<getlength(q)<<endl;
int m;
cin>>m;
char f;
for(int i=0;i<m;i++)
{
cin>>f;
enQueue (q ,f);
}
print(q);
cout<<getlength(q)<<endl;
DestoryQueue(q);
return 0;
}