内容:
编写一个程序,实现顺序栈(假设栈中元素类型ElemType为char)的各种基本运算,并在此基础上设计一个程序完成以下功能:
程序:
#include
#include
#define maxsize 100
using namespace std;
typedef char ElemType;
typedef struct{
ElemType data[maxsize];
int top;
}SqStack;
void InitStack(SqStack *&s){
s=(SqStack *)malloc(sizeof(SqStack));
s->top=-1;
}
void DestroyStack(SqStack *&s){
free(s);
}
bool StackEmpty(SqStack *s){
return(s->top==-1);
}
bool Push(SqStack *&s,ElemType e){
if(s->top==maxsize-1){
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;
}
#include"sqstack.cpp"
int main(){
ElemType e;
SqStack *s;
cout<<"顺序栈s的基本运算如下:"<<endl;
cout<<"(1)初始化栈s"<<endl;
InitStack(s);
printf("(2)栈为%s\n",(StackEmpty(s)?"空":"非空"));
cout<<"(3)依次进栈元素a,b,c,d,e"<<endl;
Push(s,'a');
Push(s,'b');
Push(s,'c');
Push(s,'d');
Push(s,'e');
printf("(4)栈为%s\n",(StackEmpty(s)?"空":"非空"));
cout<<"(5)出栈序列:";
while(!StackEmpty(s)){
Pop(s,e);
cout<<e;
}
cout<<endl;
printf("(6)栈为%s\n",(StackEmpty(s)?"空":"非空"));
cout<<"(7)释放栈"<<endl;
DestroyStack(s);
return 1;
}
内容:
编写一个程序,实现链栈(假设栈中元素ElemType为char)的各种基本运算,并在此基础上设计一个程序完成如下功能:
程序:
#include
#include
using namespace std;
typedef char ElemType;
typedef struct linknode{
ElemType data;
struct linknode *next;
} LinkStNode;
void InitStack(LinkStNode *&s){
s=(LinkStNode *)malloc(sizeof(LinkStNode));
s->next=NULL;
}
void DestroyStack(LinkStNode *&s){
LinkStNode *p=s->next;
while(p!=NULL){
free(s);
s=p;
p=p->next;
}
free(s);
}
bool StackEmpty(LinkStNode *s){
return(s->next==NULL);
}
void Push(LinkStNode *&s,ElemType e){
LinkStNode *p;
p=(LinkStNode *)malloc(sizeof(LinkStNode));
p->data=e;
p->next=s->next;
s->next=p;
}
bool Pop(LinkStNode *&s,ElemType e){
LinkStNode *p;
if(s->next==NULL){
return false;
}
p=s->next;
e=p->data;
s->next=p->next;
free(p);
return true;
}
bool GetTop(LinkStNode *s,ElemType &e){
if(s->next==NULL){
return false;
}
e=s->next->data;
return true;
}
#include"listack.cpp"
int main(){
ElemType e;
LinkStNode *s;
cout<<"栈列s的基本运算如下:"<<endl;
cout<<" (1)初始化栈s"<<endl;
InitStack(s);
printf(" (2)栈为%s\n",(StackEmpty(s)?"空":"非空"));
cout<<" (3)依次进栈元素a,b,c,d,e"<<endl;
Push(s,'a');
Push(s,'b');
Push(s,'c');
Push(s,'d');
Push(s,'e');
printf(" (4)栈为%s\n",(StackEmpty(s)?"空":"非空"));
cout<<" (5)出栈序列:";
while(!StackEmpty(s)){
Pop(s,e);
printf("%c",e);
}
cout<<endl;
printf(" (6)栈为%s\n",(StackEmpty(s)?"空":"非空"));
cout<<" (7)释放栈"<<endl;
DestroyStack(s);
return 0;
}
内容:
编写一个程序,实现环形队列(假设栈中元素ElemType为char)的各种基本运算,并在此基础上设计一个程序完成如下功能:
程序:
#include
#include
#define maxsize 100
typedef char ElemType;
using namespace std;
typedef struct {
ElemType data[maxsize];
int front;
int rear;
}SqQueue;
void InitQueue(SqQueue *&q){
q=(SqQueue *)malloc(sizeof(SqQueue));
q->front=q->rear=0;
}
void DestroyQueue(SqQueue *&q){
free(q);
}
bool QueueEmpty(SqQueue *q){
return(q->front==q->rear);
}
bool enQueue(SqQueue *&q,ElemType e){
if((q->rear+1)%maxsize==q->front){
return false;
}
q->rear=(q->rear+1)%maxsize;
q->data[q->rear]=e;
return true;
}
bool deQueue(SqQueue *&q,ElemType &e){
if(q->front==q->rear){
return false;
}
q->front=(q->front+1)%maxsize;
e=q->data[q->front];
return true;
}
#include"sqqueue.cpp"
int main(){
ElemType e;
SqQueue *q;
cout<<"环形队列基本运算如下:"<<endl;
cout<<" (1)初始化队列q"<<endl;
InitQueue(q);
cout<<" (2)依次进队列元素a,b,c"<<endl;
if(!enQueue(q,'a'))cout<<" "<<endl;
if(!enQueue(q,'b'))cout<<" "<<endl;
if(!enQueue(q,'c'))cout<<" "<<endl;
printf(" (3)队列为%s\n",(QueueEmpty(q)?"空":"非空"));
if(deQueue(q,e)==0)
cout<<"队空,不能出队"<<endl;
else
cout<<" (4)出队一个元素"<<e<<endl;
cout<<" (5)依次进队列元素d,e,f"<<endl;
if(!enQueue(q,'d'))cout<<" 提示:队满,不能入队"<<endl;
if(!enQueue(q,'e'))cout<<" 提示:队满,不能入队"<<endl;
if(!enQueue(q,'f'))cout<<" 提示:队满,不能入队"<<endl;
cout<<" (6)出队列序列:";
while(!QueueEmpty(q)){
deQueue(q,e);
cout<<e<<" ";
}
cout<<endl;
cout<<" (7)释放队列"<<endl;
DestroyQueue(q);
return 1;
}
内容:
编写一个程序,实现链队(假设栈中元素ElemType为char)的各种基本运算,并在此基础上设计一个程序完成如下功能:
程序:
#include
#include
typedef char ElemType;
using namespace std;
typedef struct DataNode{
ElemType data;
struct DataNode *next;
}DataNode;
typedef struct{
DataNode *front;
DataNode *rear;
}LinkQuNode;
void InitQueue(LinkQuNode *&q){
q=(LinkQuNode *)malloc(sizeof(LinkQuNode));
q->front=q->rear=NULL;
}
void DestroyQueue(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);
}
bool enQueue(LinkQuNode *&q,ElemType e){
DataNode *p;
p=(DataNode *)malloc(sizeof(LinkQuNode));
p->data=e;
p->next=NULL;
if(q->rear==NULL){
q->front=q->rear=p;
}
else{
q->rear->next=p;
q->rear=p;
}
}
bool deQueue(LinkQuNode *&q,ElemType &e){
DataNode *t;
if(q->rear==NULL){
return false;
}
t=q->front;
if(q->front==q->rear){
q->front=q->rear=NULL;
}
else{
q->front=q->front->next;
}
e=t->data;
free(t);
return true;
}
#include"liqueue.cpp"
int main(){
ElemType e;
LinkQuNode *q;
cout<<"链队的基本运算如下:"<<endl;
cout<<" (1)初始化链队"<<endl;
InitQueue(q);
cout<<" (2)依次进链队元素a,b,c"<<endl;
enQueue(q,'a');
enQueue(q,'b');
enQueue(q,'c');
printf(" (3)链队为%s\n",(QueueEmpty(q)?"空":"非空"));
if(deQueue(q,e)==0){
cout<<" 提示:队空,不能出队"<<endl;
}else{
cout<<" (4)出队一个元素"<<e<<endl;
}
cout<<" (5)依次进链队元素d,e,f"<<endl;
enQueue(q,'d');
enQueue(q,'e');
enQueue(q,'f');
cout<<" (6)出链队序列:";
while(!QueueEmpty(q)){
deQueue(q,e);
cout<<e;
}
cout<<endl;
cout<<" (7)释放链队"<<endl;
DestroyQueue(q);
return 1;
}