idk why these stuffs get stashed for so long and I didn't ever commit them
This commit is contained in:
@@ -1,33 +1,30 @@
|
||||
/***链栈实现括号匹配***/
|
||||
#include<string>
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
#include"LinkStack.h"
|
||||
|
||||
//算法3.11 括号的匹配
|
||||
bool match(string exp)
|
||||
//括号的匹配
|
||||
bool Matching()
|
||||
{
|
||||
//检验表达式(表达式以"#"结束)中所含"["和"]"、"("和")"是否匹配,如果匹配,则返回true,否则返回false。
|
||||
//检验表达式中所含括号是否正确匹配,如果匹配,则返回true,否则返回false。
|
||||
//表达式以“#”结束
|
||||
SNode<char> *S;
|
||||
InitStack(S);
|
||||
int flag=1; // 标记查找结果以控制循环及返回结果
|
||||
char ch;
|
||||
char e,x;
|
||||
int i=0;
|
||||
ch=exp[i++]; // 读入第一个字符
|
||||
while(ch!='#' && flag)
|
||||
char c;
|
||||
char x;
|
||||
cin>>c; // 读入第一个字符
|
||||
while(c!='#' && flag)
|
||||
{
|
||||
switch (ch)
|
||||
{
|
||||
case '[':
|
||||
case '(': // 若是左括号,则将其压入栈
|
||||
switch (c){
|
||||
case '[':
|
||||
case '(': // 若是左括号,则将其压入栈
|
||||
cout<<"左括号进栈!"<<endl;
|
||||
Push(S,ch);
|
||||
Push(S,c);
|
||||
break;
|
||||
case ')' : // 若是右括号“)”,则根据栈顶元素的值分情况考虑
|
||||
GetTop(S,e);
|
||||
if (!StackEmpty(S) && e=='(' ) // 若栈非空且栈顶元素是“(”,则匹配成功
|
||||
case ')' : // 若是右括号“)”,则根据当前栈顶元素的值分情况考虑
|
||||
GetTop(S,x);
|
||||
if (!StackEmpty(S) && x=='(' ) // 若栈非空且栈顶元素是“(”,则匹配成功
|
||||
{
|
||||
Pop(S,x);
|
||||
cout<<"右括号出栈!"<<endl;
|
||||
@@ -35,35 +32,31 @@ bool match(string exp)
|
||||
else
|
||||
flag=0; // 若栈空或栈顶元素不是“(”,则非法
|
||||
break;
|
||||
case ']' : // 若是右括号“]”,则根据栈顶元素的值分情况考虑
|
||||
GetTop(S,e);
|
||||
if (!StackEmpty(S) && e=='[' ) // 若栈顶元素是“[”,则匹配成功
|
||||
case ']' : // 若是右括号“]”,则根据当前栈顶元素的值分情况考虑
|
||||
GetTop(S,x);
|
||||
if (!StackEmpty(S) && x=='[' ) // 若栈顶元素是“[”,则匹配成功
|
||||
Pop(S,x);
|
||||
else
|
||||
flag=0; // 若栈空或栈顶元素不是“[”,则非法
|
||||
break;
|
||||
}//switch
|
||||
ch=exp[i++]; //继续读入下一个字符
|
||||
cin>>c; //继续读入下一个字符
|
||||
}//while
|
||||
if (StackEmpty(S) && flag ) // 栈空且标志为true,括号匹配,返回true
|
||||
DestroyStack(S);
|
||||
if (StackEmpty(S) && flag )
|
||||
return true;
|
||||
else // 否则,括号不匹配,返回false
|
||||
else
|
||||
return false;
|
||||
}//match
|
||||
|
||||
|
||||
}//Matching
|
||||
|
||||
int main()
|
||||
{
|
||||
int flag;
|
||||
string exp;
|
||||
cout<<"请输入待匹配的表达式,以“#”结束:"<<endl;
|
||||
cin>>exp;
|
||||
flag = match(exp);
|
||||
flag = Matching();
|
||||
if(flag)
|
||||
cout<<"括号匹配成功!"<<endl;
|
||||
else
|
||||
cout<<"括号匹配失败!"<<endl;
|
||||
return 0;
|
||||
}//end of main function
|
||||
|
||||
}
|
||||
|
||||
@@ -1,158 +0,0 @@
|
||||
#include<iostream>
|
||||
#include<string>
|
||||
using namespace std;
|
||||
|
||||
struct dancer // 舞者信息
|
||||
{ string name; // 姓名
|
||||
char sex; // 性别
|
||||
};
|
||||
|
||||
|
||||
struct Node // 队列结点
|
||||
{ dancer data; // 数据域
|
||||
Node* next; // 指针域,指向后继
|
||||
}*front,*rear; // 队头、队尾
|
||||
|
||||
struct LinkQueue
|
||||
{ Node *front;
|
||||
Node *rear;
|
||||
}; // 男队和女队
|
||||
|
||||
void InitialLinkQueue(LinkQueue &Q) // 初始化队列
|
||||
{
|
||||
Q.front=new Node;
|
||||
Q.front->next=NULL;
|
||||
Q.rear=Q.front;
|
||||
}
|
||||
|
||||
void DestroyLinkQueue(LinkQueue &Q) // 销毁队列
|
||||
{ Node *p;
|
||||
while(Q.front!=NULL)
|
||||
{ p=Q.front;
|
||||
Q.front=Q.front->next;
|
||||
delete p;
|
||||
}
|
||||
}
|
||||
|
||||
void EnQueue(LinkQueue &Q,dancer &e) // 入队
|
||||
{
|
||||
Node *s=new Node;
|
||||
s->data=e;
|
||||
s->next=Q.rear->next;
|
||||
Q.rear->next=s;
|
||||
Q.rear=s;
|
||||
}
|
||||
|
||||
bool IsEmpty(LinkQueue Q) // 判队空
|
||||
{
|
||||
if (Q.front==Q.rear)
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
bool DeQueue(LinkQueue &Q,dancer &e) // 出队
|
||||
{
|
||||
Node *p;
|
||||
if (IsEmpty(Q)) // 队空,
|
||||
{
|
||||
cout<<"队列为空,无法出队列!";
|
||||
return false;
|
||||
}
|
||||
p=Q.front->next;
|
||||
e=p->data;
|
||||
Q.front->next=p->next;
|
||||
if (p==Q.rear) // 只有一个元素,出队
|
||||
Q.rear=Q.front; // 修改队尾指针
|
||||
delete p;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool GetHead(LinkQueue Q,dancer &e)
|
||||
{ if(IsEmpty(Q)) // 队空
|
||||
{
|
||||
cout<< "队列为空,无法取得队首元素!";
|
||||
return false;
|
||||
}
|
||||
e=Q.front->next->data; // 返回队头元素
|
||||
return true;
|
||||
}
|
||||
|
||||
void QueueTranverse(LinkQueue Q) // 遍历队
|
||||
{ Node *p;
|
||||
p=Q.front->next;
|
||||
while(p!=NULL)
|
||||
{ cout<<(p->data).name<<" ";
|
||||
p=p->next;
|
||||
}
|
||||
cout<<endl;
|
||||
}
|
||||
|
||||
void EntranHall(dancer person[],int num) // 舞者入场
|
||||
{
|
||||
int i;
|
||||
for(i=0;i<num;i++)
|
||||
{
|
||||
cout<<"请输入第"<<i+1<<"个舞者性别(F(女) or M(男))及姓名:"<<endl;
|
||||
cin>>person[i].sex;
|
||||
cin>>person[i].name;
|
||||
}
|
||||
cout<<"现有舞者:"<<endl;
|
||||
for(i=0;i<num;i++)
|
||||
{
|
||||
cout<<i+1<<":"<<person[i].sex
|
||||
<<","<<person[i].name<<endl;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
//算法3.24
|
||||
void DancePartner(dancer person[],int num)
|
||||
{
|
||||
dancer newdancer,m,f,p;
|
||||
LinkQueue GenQueue;
|
||||
LinkQueue LadyQueue;
|
||||
InitialLinkQueue(GenQueue); // 初始化男队
|
||||
InitialLinkQueue(LadyQueue); // 初始化女队
|
||||
for(int i=0;i<num;i++) // 舞者入场
|
||||
{ p=person[i];
|
||||
if(p.sex=='F')
|
||||
EnQueue(LadyQueue,p);
|
||||
else
|
||||
EnQueue(GenQueue,p);
|
||||
}
|
||||
while ( (!IsEmpty(GenQueue)) && (!IsEmpty(LadyQueue)) ) // 匹配舞者
|
||||
{
|
||||
DeQueue(GenQueue,m); // 女士出队
|
||||
DeQueue(LadyQueue,f); // 男士出队
|
||||
cout<<m.name <<"<---配对--->"<<f.name<<endl; // 男、女配队
|
||||
}
|
||||
if (!IsEmpty(GenQueue))
|
||||
{
|
||||
GetHead(GenQueue,m);
|
||||
cout<<m.name<<"先生还在等着呢!"<<endl;
|
||||
}
|
||||
else if (!IsEmpty(LadyQueue))
|
||||
{
|
||||
GetHead(LadyQueue,f);
|
||||
cout<<f.name<<"女士还在等着呢!"<<endl;
|
||||
}
|
||||
else
|
||||
cout<<"配对完美结束!"<<endl;
|
||||
DestroyLinkQueue(GenQueue); // 销毁队列
|
||||
DestroyLinkQueue(LadyQueue);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
dancer *person;
|
||||
int num;
|
||||
cout<<"请输入舞伴总数量:"<<endl;
|
||||
cin>>num;
|
||||
person=new dancer[num];
|
||||
EntranHall(person, num); // 舞者入场
|
||||
DancePartner(person,num); // 舞者匹配
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -10,9 +10,8 @@ void dispmenu()
|
||||
cout<<"3-元素出队\n";
|
||||
cout<<"4-取队头元素\n";
|
||||
cout<<"5-取队尾元素\n";
|
||||
cout<<"6-清空队\n";
|
||||
cout<<"7-测队空\n";
|
||||
cout<<"8-显示队列元素\n";
|
||||
cout<<"6-测队空\n";
|
||||
cout<<"7-显示队列元素\n";
|
||||
cout<<"0-退出\n";
|
||||
}
|
||||
|
||||
@@ -29,7 +28,7 @@ int main()
|
||||
do
|
||||
{
|
||||
dispmenu(); // 显示主菜单
|
||||
cout<<"功能选择(1~8,0 退出):";
|
||||
cout<<"功能选择(1~7,0 退出):";
|
||||
cin>>choice;
|
||||
switch(choice)
|
||||
{
|
||||
@@ -37,7 +36,7 @@ int main()
|
||||
InitQueue(Q);
|
||||
cout<<endl<<"创建成功!"<<endl;
|
||||
break;
|
||||
case 2: //入队
|
||||
case 2: //入栈
|
||||
cout<<"输入要插入的元素值:"<<endl;
|
||||
cin>>e;
|
||||
cout<<endl;
|
||||
@@ -50,7 +49,7 @@ int main()
|
||||
cout<<endl<<"入队不成功!"<<endl;
|
||||
break;
|
||||
|
||||
case 3: // 出队
|
||||
case 3: // 出栈
|
||||
if(DeQueue(Q,e))
|
||||
{
|
||||
cout<<endl<<"出队成功!出队元素为:"<<e<<endl;
|
||||
@@ -64,9 +63,9 @@ int main()
|
||||
case 4: // 获取队头元素
|
||||
if(GetHead(Q,e))
|
||||
{
|
||||
cout<<"队列元素为:"<<endl;
|
||||
DispQueue(Q);
|
||||
cout<<endl<<"队头元素为:"<<e<<endl;
|
||||
cout<<"队列元素为:"<<endl;
|
||||
DispQueue(Q);
|
||||
}
|
||||
else
|
||||
cout<<endl<<"队空!"<<endl;
|
||||
@@ -74,33 +73,30 @@ int main()
|
||||
case 5: // 获取队尾元素
|
||||
if(GetTail(Q,e))
|
||||
{
|
||||
cout<<"队列元素为:"<<endl;
|
||||
DispQueue(Q);
|
||||
cout<<endl<<"队尾元素为:"<<e<<endl;
|
||||
|
||||
}
|
||||
else
|
||||
cout<<endl<<"队空!"<<endl;
|
||||
break;
|
||||
case 6: // 清空队
|
||||
ClearQueue(Q);
|
||||
cout<<endl<<"队已空!"<<endl;
|
||||
case 7: // 测队空
|
||||
case 6: // 测队空
|
||||
if(QueueEmpty(Q))
|
||||
cout<<endl<<"空队!"<<endl;
|
||||
else
|
||||
cout<<endl<<"不是空队!"<<endl;
|
||||
break;
|
||||
case 8: // 查看队列元素
|
||||
case 7: // 查看队列元素
|
||||
DispQueue(Q);
|
||||
cout<<endl;
|
||||
cin.get(pause);
|
||||
system("pause");
|
||||
break;
|
||||
case 0: // 退出
|
||||
DestroyQueue(Q);
|
||||
cout<<"结束运行Bye-bye!"<<endl;
|
||||
break;
|
||||
default: // 无效选择
|
||||
cout<<"无效选择!\n";
|
||||
cout<<"Invalid choice\n";
|
||||
break;
|
||||
}
|
||||
}while(choice!=0);
|
||||
@@ -10,17 +10,18 @@ void dispmenu()
|
||||
cout<<"2-元素入栈\n";
|
||||
cout<<"3-元素出栈\n";
|
||||
cout<<"4-取栈顶元素\n";
|
||||
cout<<"5-헌왕攬\n";
|
||||
cout<<"6-꿎攬왕\n";
|
||||
cout<<"7-鞫刻攬禱羹\n";
|
||||
cout<<"5-测栈空\n";
|
||||
cout<<"6-显示栈元素\n";
|
||||
cout<<"0-退出\n";
|
||||
}
|
||||
|
||||
char pause;
|
||||
|
||||
//主函数
|
||||
int main()
|
||||
{
|
||||
int e;
|
||||
SNode<int> * S;
|
||||
LNode<int> * S;
|
||||
system("cls"); // 清屏
|
||||
int choice;
|
||||
do
|
||||
@@ -66,26 +67,24 @@ int main()
|
||||
else
|
||||
cout<<endl<<"栈空!"<<endl;
|
||||
break;
|
||||
case 5: // 헌왕攬
|
||||
DestroyStack(S);
|
||||
cout<<"攬綠헌왕!"<<endl;
|
||||
break;
|
||||
case 6: //꿎攬왕
|
||||
case 5: //测栈空
|
||||
if(StackEmpty(S))
|
||||
cout<<endl<<"空栈!"<<endl;
|
||||
else
|
||||
cout<<endl<<"不是空栈!"<<endl;
|
||||
break;
|
||||
case 7: //鞫刻攬禱羹
|
||||
case 6: //显示栈元素
|
||||
DispStack(S);
|
||||
cout<<endl;
|
||||
cin.get(pause);
|
||||
system("pause");
|
||||
break;
|
||||
case 0: //退出
|
||||
DestroyStack(S);
|
||||
cout<<"써監頓契Bye-bye!"<<endl;
|
||||
cout<<"结束运行Bye-bye!"<<endl;
|
||||
break;
|
||||
default: //非法选择
|
||||
cout<<"轟槻朞嶝!\n";
|
||||
cout<<"Invalid choice\n";
|
||||
break;
|
||||
}
|
||||
}while(choice!=0);
|
||||
@@ -1,44 +1,42 @@
|
||||
|
||||
template <class DT>
|
||||
struct SNode // 结点
|
||||
struct LNode //结点
|
||||
{
|
||||
DT data; // 数据域,存储数据元素值
|
||||
SNode *next; // 指针域,指向下一个结点
|
||||
DT data; //数据域,存储数据元素值
|
||||
LNode *next;//指针域,指向下一个结点
|
||||
};
|
||||
|
||||
|
||||
//【算法3.6】
|
||||
template <class DT>
|
||||
bool InitStack(SNode<DT> *&S) // 创建空链栈
|
||||
void InitStack(LNode<DT> *&S)//创建空链栈
|
||||
{
|
||||
//L=new LNode<DT>; //创建头结点
|
||||
//if(!L) exit(1); //创建失败,结束运行
|
||||
S=NULL;
|
||||
return true;
|
||||
}
|
||||
|
||||
//【算法3.7】
|
||||
template <class DT>
|
||||
void DestroyStack(SNode<DT> *(&S)) // 释放链栈所占内存
|
||||
void DestroyStack(LNode<DT> *(&S))//释放链栈
|
||||
{
|
||||
SNode<DT> *p;
|
||||
while(S) //从头结点开始,依次释放结点
|
||||
LNode<DT> *p;
|
||||
while(S)//从头结点开始,依次释放结点
|
||||
{
|
||||
p=S;
|
||||
S=S->next;
|
||||
delete p;
|
||||
}
|
||||
S=NULL;
|
||||
//L=NULL;//头结点指向空
|
||||
}
|
||||
|
||||
|
||||
|
||||
//【算法3.8】
|
||||
template<class DT>
|
||||
bool Push(SNode<DT> *&S,DT e)
|
||||
bool Push(LNode<DT> *&S,DT e)
|
||||
{
|
||||
SNode<DT> *p;
|
||||
p=new SNode<DT>;
|
||||
if(!p)
|
||||
return false; //创建失败,结束运行
|
||||
LNode<DT> *p;
|
||||
p=new LNode<DT>;
|
||||
if(!p) return false; //创建失败,结束运行
|
||||
p->data=e; // 新结点赋值
|
||||
p->next=S; //结点S链接到p结点之后
|
||||
S=p;
|
||||
@@ -47,9 +45,9 @@ bool Push(SNode<DT> *&S,DT e)
|
||||
|
||||
//【算法3.9】
|
||||
template<class DT>
|
||||
bool Pop(SNode<DT> *&S,DT &e)
|
||||
bool Pop(LNode<DT> *&S,DT &e)
|
||||
{
|
||||
SNode<DT> *p;
|
||||
LNode<DT> *p;
|
||||
if(S==NULL) return false;
|
||||
p=S;
|
||||
e=p->data;
|
||||
@@ -61,9 +59,9 @@ bool Pop(SNode<DT> *&S,DT &e)
|
||||
|
||||
//【算法3.10】
|
||||
template<class DT>
|
||||
bool GetTop(SNode<DT> *S,DT &e)
|
||||
bool GetTop(LNode<DT> *S,DT &e)
|
||||
{
|
||||
SNode<DT> *p;
|
||||
LNode<DT> *p;
|
||||
if(S==NULL) return false;
|
||||
p=S;
|
||||
e=p->data;
|
||||
@@ -72,7 +70,7 @@ bool GetTop(SNode<DT> *S,DT &e)
|
||||
|
||||
//测栈空
|
||||
template<class DT>
|
||||
bool StackEmpty(SNode<DT> *S)
|
||||
bool StackEmpty(LNode<DT> *S)
|
||||
{
|
||||
if(S==NULL)
|
||||
return true;
|
||||
@@ -82,9 +80,9 @@ bool StackEmpty(SNode<DT> *S)
|
||||
|
||||
//显示栈内容
|
||||
template<class DT>
|
||||
void DispStack(SNode<DT> *S)
|
||||
void DispStack(LNode<DT> *S)
|
||||
{
|
||||
SNode<DT> *p;
|
||||
LNode<DT> *p;
|
||||
p=S;
|
||||
while(p)
|
||||
{
|
||||
@@ -1,126 +0,0 @@
|
||||
|
||||
#include<iostream>//cout,cin
|
||||
using namespace std;
|
||||
#include "SqQueue.h"
|
||||
|
||||
char pause;
|
||||
|
||||
|
||||
void dispmenu() //菜单
|
||||
{
|
||||
cout<<endl;
|
||||
cout<<"1-初始化顺序队列\n";
|
||||
cout<<"2-元素入队\n";
|
||||
cout<<"3-元素出队\n";
|
||||
cout<<"4-取队头元素\n";
|
||||
cout<<"5-取队尾元素\n";
|
||||
cout<<"6-清空队\n";
|
||||
cout<<"7-测队空\n";
|
||||
cout<<"8-测队满\n";
|
||||
cout<<"9-显示队列元素\n";
|
||||
cout<<"10-显示队头、队尾指针\n";
|
||||
cout<<"0-退出\n";
|
||||
}
|
||||
|
||||
|
||||
//主函数
|
||||
int main()
|
||||
{
|
||||
int i;
|
||||
int e;
|
||||
SqQueue<int> Q; // 元素类型为整型的顺序队列
|
||||
system("cls"); // 清屏
|
||||
int choice;
|
||||
do
|
||||
{
|
||||
dispmenu(); //显示主菜单
|
||||
cout<<"功能选择(1~10,0 退出!):";
|
||||
cin>>choice;
|
||||
switch(choice)
|
||||
{
|
||||
case 1: // 初始化顺序队列
|
||||
cout<<"请输入要创建的顺序队列的长度";
|
||||
cin>>i;
|
||||
cout<<endl;
|
||||
InitQueue (Q,i);
|
||||
cout<<endl<<"创建成功!"<<endl;
|
||||
break;
|
||||
case 2: // 入队
|
||||
cout<<"输入要入队的元素值:"<<endl;
|
||||
cin>>e;
|
||||
cout<<endl;
|
||||
if(EnQueue(Q,e))
|
||||
{
|
||||
cout<<endl<<"入队成功!入队后队列元素为:"<<endl;
|
||||
DispQueue(Q);
|
||||
}
|
||||
else
|
||||
cout<<endl<<"队满,不能入队!"<<endl;
|
||||
break;
|
||||
case 3: // 出队
|
||||
if(DeQueue(Q,e))
|
||||
{
|
||||
cout<<endl<<"队列元素为:";
|
||||
DispQueue(Q);
|
||||
cout<<endl<<"出队元素为:"<<e<<endl;
|
||||
}
|
||||
else
|
||||
cout<<endl<<"队空,不能出队!"<<endl;
|
||||
break;
|
||||
case 4: // 取队头元素
|
||||
if(GetHead(Q,e))
|
||||
{
|
||||
cout<<endl<<"队列元素为:";
|
||||
DispQueue(Q);
|
||||
cout<<endl<<"队头元素为:"<<e<<endl;
|
||||
}
|
||||
else
|
||||
cout<<endl<<"队空!"<<endl;
|
||||
break;
|
||||
case 5: // 取队尾元素
|
||||
if(GetTail(Q,e))
|
||||
{
|
||||
cout<<endl<<"队列元素为:";
|
||||
DispQueue(Q);
|
||||
cout<<endl<<"队尾元素为:"<<e<<endl;
|
||||
}
|
||||
else
|
||||
cout<<endl<<"队空!无数据元素"<<endl;
|
||||
break;
|
||||
case 6: // 清空队
|
||||
ClearQueue(Q);
|
||||
cout<<"队已清空!"<<endl;
|
||||
break;
|
||||
case 7: // 测队空
|
||||
if(QueueEmpty(Q))
|
||||
cout<<endl<<"空队!"<<endl;
|
||||
else
|
||||
cout<<endl<<"非空队!"<<endl;
|
||||
break;
|
||||
case 8: // 测队满
|
||||
if(QueueFull(Q))
|
||||
cout<<endl<<"满队!"<<endl;
|
||||
else
|
||||
cout<<endl<<"非满队!"<<endl;
|
||||
break;
|
||||
case 9: // 显示队列元素
|
||||
DispQueue(Q);
|
||||
cout<<endl;
|
||||
break;
|
||||
case 10:
|
||||
cout<<"\nQ.fornt="<<Q.front<<endl;
|
||||
cout<<"Q.rear="<<Q.rear<<endl;
|
||||
break;
|
||||
case 0: // 退出
|
||||
DestroyQueue(Q);
|
||||
cout<<"结束运行Bye-bye!"<<endl;
|
||||
break;
|
||||
default: // 无效选择
|
||||
cout<<"无效选择!\n";
|
||||
break;
|
||||
}
|
||||
}while(choice!=0);
|
||||
|
||||
return 0;
|
||||
}//end of main function
|
||||
|
||||
@@ -13,11 +13,8 @@ void dispmenu()
|
||||
cout<<"2-元素入栈\n";
|
||||
cout<<"3-元素出栈\n";
|
||||
cout<<"4-取栈顶元素\n";
|
||||
cout<<"5-清空栈\n";
|
||||
cout<<"6-测栈空\n";
|
||||
cout<<"7-测栈满\n";
|
||||
cout<<"8-显示栈元素\n";
|
||||
cout<<"9-查看栈顶指针\n";
|
||||
cout<<"5-测栈空\n";
|
||||
cout<<"6-显示栈元素\n";
|
||||
cout<<"0-退出\n";
|
||||
}
|
||||
|
||||
@@ -50,17 +47,17 @@ int main()
|
||||
cin>>e;
|
||||
if(Push(S,e))
|
||||
{
|
||||
cout<<"入栈成功!入栈后栈元素为:"<<endl;
|
||||
cout<<"入栈成功!入栈后,栈元素为:"<<endl;
|
||||
DispStack(S);
|
||||
}
|
||||
else
|
||||
cout<<endl<<"栈满,不能入栈!"<<endl;
|
||||
cout<<endl<<"入栈不成功!"<<endl;
|
||||
break;
|
||||
case 3: // 出栈
|
||||
if(Pop(S,e))
|
||||
{
|
||||
cout<<"出栈成功!出栈元素为:"<<e<<endl;
|
||||
cout<<"出栈后栈元素为:"<<endl;
|
||||
cout<<"出栈后,栈元素为:"<<endl;
|
||||
DispStack(S);
|
||||
}
|
||||
else
|
||||
@@ -74,36 +71,22 @@ int main()
|
||||
else
|
||||
cout<<endl<<"栈空!"<<endl;
|
||||
break;
|
||||
case 5: // 清空栈
|
||||
ClearStack(S);
|
||||
cout<<endl<<"栈已空!"<<endl;
|
||||
break;
|
||||
case 6: // 测栈空
|
||||
case 5: // 测栈空
|
||||
if(StackEmpty(S))
|
||||
cout<<endl<<"空栈!"<<endl;
|
||||
else
|
||||
cout<<endl<<"不是空栈!"<<endl;
|
||||
break;
|
||||
case 7: // 测栈满
|
||||
if(StackFull(S))
|
||||
cout<<endl<<"栈满!"<<endl;
|
||||
else
|
||||
cout<<endl<<"栈不满!"<<endl;
|
||||
break;
|
||||
case 8: // 显示栈元素
|
||||
case 6: // 显示栈元素
|
||||
DispStack(S);
|
||||
cout<<endl;
|
||||
break;
|
||||
case 9: // 显示栈顶指针
|
||||
cout<<"栈顶指针top= "<<S.top<<endl;
|
||||
cout<<endl;
|
||||
break;
|
||||
case 0: // 退出
|
||||
case 0: // 退出
|
||||
DestroyStack(S);
|
||||
cout<<"结束运行 Bye-bye!"<<endl;
|
||||
cout<<"结束运行 Bye-bye!"<<endl;
|
||||
break;
|
||||
default: // 无效选择
|
||||
cout<<"无效选择!\n";
|
||||
cout<<"Invalid choice\n";
|
||||
break;
|
||||
}
|
||||
}while(choice!=0);
|
||||
@@ -87,21 +87,3 @@ void DispStack(SqStack<DT> S)
|
||||
cout<<endl;
|
||||
}
|
||||
|
||||
template<class DT>
|
||||
void ClearStack(SqStack<DT> &S)
|
||||
{
|
||||
S.top=-1;
|
||||
|
||||
}
|
||||
|
||||
template<class DT>
|
||||
int StackFull(SqStack<DT> S)
|
||||
{
|
||||
if(S.top==S.stacksize-1)
|
||||
return 1;
|
||||
else
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#include<iostream> //cout,cin
|
||||
using namespace std;
|
||||
#include"SqStack.h"
|
||||
#include<stdlib.h> // atoi()
|
||||
|
||||
char pause;
|
||||
|
||||
@@ -51,9 +52,9 @@ char Precede(char t1,char t2) //
|
||||
}
|
||||
|
||||
|
||||
bool In(char ch) // 判断 ch 是否为运算符
|
||||
bool In(char c) // 判断 c 是否为运算符
|
||||
{
|
||||
switch(ch)
|
||||
switch(c)
|
||||
{
|
||||
case'+':
|
||||
case'-':
|
||||
@@ -69,21 +70,21 @@ bool In(char ch) //
|
||||
|
||||
float Operate(float a,char theta,float b)
|
||||
{ // 实施一次运算
|
||||
float ch;
|
||||
float c;
|
||||
switch(theta)
|
||||
{
|
||||
case'+':ch=a+b;
|
||||
case'+':c=a+b;
|
||||
break;
|
||||
case'-':ch=a-b;
|
||||
case'-':c=a-b;
|
||||
break;
|
||||
case'*':ch=a*b;
|
||||
case'*':c=a*b;
|
||||
break;
|
||||
case'/':ch=a/b;
|
||||
case'/':c=a/b;
|
||||
}
|
||||
return ch;
|
||||
return c;
|
||||
}
|
||||
|
||||
//算法3.12
|
||||
|
||||
float Val_Exp(char *exp) // 中缀表达式求值。
|
||||
{
|
||||
SqStack<char> OP; // 运算符栈
|
||||
@@ -91,82 +92,82 @@ float Val_Exp(char *exp) //
|
||||
InitStack(OP,30);
|
||||
InitStack(OD,30);
|
||||
char theta;
|
||||
float a,b,result;
|
||||
char ch,x; // 存放由键盘接收的字符
|
||||
float a,b,d;
|
||||
char c,x; // 存放由键盘接收的字符
|
||||
char z[6]; // 存放符点数字符串
|
||||
int i;
|
||||
Push(OP,'='); // # 是表达式结束标志
|
||||
ch=*exp++;
|
||||
c=*exp++;
|
||||
GetTop(OP,x);
|
||||
while(ch!='='||x!='=')
|
||||
while(c!='='||x!='=')
|
||||
{
|
||||
if(ch>='0'&&ch<='9'||ch=='.') // ch 是操作数
|
||||
if(c>='0'&&c<='9'||c=='.') // c 是操作数
|
||||
{
|
||||
i=0;
|
||||
do
|
||||
{
|
||||
z[i]=ch;
|
||||
z[i]=c;
|
||||
i++;
|
||||
ch=*exp++;
|
||||
} while(ch>='0'&&ch<='9'||ch=='.');
|
||||
c=*exp++;
|
||||
} while(c>='0'&&c<='9'||c=='.');
|
||||
z[i]='\0';
|
||||
result=atof(z); // 将字符串数组转为符点型存于result
|
||||
Push(OD,result);
|
||||
d=atof(z); // 将字符串数组转为符点型存于d
|
||||
Push(OD,d);
|
||||
}
|
||||
else if(In(ch)) // 是7种运算符之一
|
||||
switch(Precede(x,ch))
|
||||
else if(In(c)) // 是7种运算符之一
|
||||
switch(Precede(x,c))
|
||||
{
|
||||
case'<':Push(OP,ch); // 栈顶元素优先权低
|
||||
ch=*exp++;
|
||||
case'<':Push(OP,c); // 栈顶元素优先权低
|
||||
c=*exp++;
|
||||
break;
|
||||
case'=':Pop(OP,x); // 脱括号并接收下一字符
|
||||
ch=*exp++;
|
||||
c=*exp++;
|
||||
break;
|
||||
case'>':Pop(OP,theta); // 退栈并将运算结果入栈
|
||||
Pop(OD,b);
|
||||
Pop(OD,a);
|
||||
Push(OD,Operate(a,theta,b));
|
||||
}
|
||||
else // ch是非法字符
|
||||
else // c是非法字符
|
||||
{
|
||||
cout<<"ERROR3"<<endl;;
|
||||
exit(0);
|
||||
}
|
||||
GetTop(OP,x);
|
||||
}
|
||||
GetTop(OD,result);
|
||||
GetTop(OD,d);
|
||||
DestroyStack(OP);
|
||||
DestroyStack(OD);
|
||||
return result;
|
||||
return d;
|
||||
}
|
||||
|
||||
void CreatePostExp(char * exp,char * &postexp) // 由中缀式求后缀式
|
||||
{
|
||||
char ch,x;
|
||||
char c,x;
|
||||
int i=0;
|
||||
SqStack<char> OP;
|
||||
InitStack(OP,30);
|
||||
Push(OP,'='); // # 是表达式结束标志
|
||||
cout<<"中缀表达式:"<<exp<<endl;
|
||||
ch=*exp++;
|
||||
while(ch)
|
||||
c=*exp++;
|
||||
while(c)
|
||||
{
|
||||
if((ch>='0'&&ch<='9')||ch=='.')
|
||||
if((c>='0'&&c<='9')||c=='.')
|
||||
{
|
||||
postexp[i++]=ch;
|
||||
ch=*exp++;
|
||||
postexp[i++]=c;
|
||||
c=*exp++;
|
||||
}
|
||||
if(In(ch)) // 是7种运算符之一
|
||||
if(In(c)) // 是7种运算符之一
|
||||
{
|
||||
postexp[i++]=' ';
|
||||
GetTop(OP,x);
|
||||
switch(Precede(x,ch))
|
||||
switch(Precede(x,c))
|
||||
{
|
||||
case'<':Push(OP,ch); // 栈顶元素优先权低
|
||||
ch=*exp++;
|
||||
case'<':Push(OP,c); // 栈顶元素优先权低
|
||||
c=*exp++;
|
||||
break;
|
||||
case'=':Pop(OP,x); // 脱括号并接收下一字符
|
||||
ch=*exp++;
|
||||
c=*exp++;
|
||||
break;
|
||||
case'>':
|
||||
Pop(OP,postexp[i++]); // 运算符出栈输出
|
||||
@@ -179,43 +180,42 @@ void CreatePostExp(char * exp,char * &postexp) //
|
||||
DestroyStack(OP);
|
||||
}
|
||||
|
||||
//算法3.13
|
||||
float Val_PostExp(char *postexp) // 后缀表达式求值
|
||||
{
|
||||
int i;
|
||||
char z[6];
|
||||
float result=0,d=0,a,b;
|
||||
char ch;
|
||||
float v=0,d=0,a,b;
|
||||
char c;
|
||||
SqStack<float> OD;
|
||||
InitStack(OD,30);
|
||||
ch=*postexp++;
|
||||
while(ch!='\0')
|
||||
c=*postexp++;
|
||||
while(c!='\0')
|
||||
{
|
||||
|
||||
if(ch>='0'&&ch<='9'||ch=='.') // ch为操作数符号
|
||||
if(c>='0'&&c<='9'||c=='.') // c为操作数符号
|
||||
{
|
||||
i=0;
|
||||
do
|
||||
{
|
||||
z[i++]=ch;
|
||||
ch=*postexp++; // 取下一个操作数符号
|
||||
} while(ch>='0'&&ch<='9'||ch=='.');
|
||||
z[i++]=c;
|
||||
c=*postexp++; // 取下一个操作数符号
|
||||
} while(c>='0'&&c<='9'||c=='.');
|
||||
z[i]='\0';
|
||||
d=atof(z); // 将字符串数组转为符点型存于result
|
||||
d=atof(z); // 将字符串数组转为符点型存于d
|
||||
Push(OD,d); // 操作数进栈
|
||||
}
|
||||
if(In(ch)) // ch为运算符
|
||||
if(In(c)) // c为运算符
|
||||
{
|
||||
Pop (OD,a);
|
||||
Pop (OD,b);
|
||||
Push (OD,Operate(b,ch,a));
|
||||
ch=*postexp++;
|
||||
Pop (OD,a);
|
||||
Push (OD,Operate(a,c,b));
|
||||
c=*postexp++;
|
||||
}
|
||||
ch=*postexp++;
|
||||
c=*postexp++;
|
||||
}
|
||||
Pop (OD,result);
|
||||
DestroyStack(OD);
|
||||
return result;
|
||||
Pop (OD,v);
|
||||
//DestoryStack(OD);
|
||||
return v;
|
||||
}
|
||||
|
||||
|
||||
@@ -224,6 +224,7 @@ void main()
|
||||
{
|
||||
//int i;
|
||||
char exp[20]="(2.2+5)+4*(5-3.1)=";
|
||||
|
||||
char *postexp;
|
||||
postexp=new char[30];
|
||||
*postexp='\0';
|
||||
@@ -275,7 +276,7 @@ void main()
|
||||
cout<<"\n结束运行,Bye-Bye!"<<endl;
|
||||
break;
|
||||
default://
|
||||
cout<<"\n无效选择!\n";
|
||||
cout<<"\nInvalid choice\n";
|
||||
break;
|
||||
}
|
||||
}while(choice!=0);
|
||||
|
||||
38
OrigFiles/3-特殊线性表/BracketsMatch(括号匹配)/BracketsMatch.cbp
Normal file
38
OrigFiles/3-特殊线性表/BracketsMatch(括号匹配)/BracketsMatch.cbp
Normal file
@@ -0,0 +1,38 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
|
||||
<CodeBlocks_project_file>
|
||||
<FileVersion major="1" minor="6" />
|
||||
<Project>
|
||||
<Option title="BracketsMatch" />
|
||||
<Option pch_mode="2" />
|
||||
<Option compiler="gcc" />
|
||||
<Build>
|
||||
<Target title="Debug">
|
||||
<Option output="bin/Debug/BracketsMatch" prefix_auto="1" extension_auto="1" />
|
||||
<Option object_output="obj/Debug/" />
|
||||
<Option type="1" />
|
||||
<Option compiler="gcc" />
|
||||
<Compiler>
|
||||
<Add option="-g" />
|
||||
</Compiler>
|
||||
</Target>
|
||||
<Target title="Release">
|
||||
<Option output="bin/Release/BracketsMatch" prefix_auto="1" extension_auto="1" />
|
||||
<Option object_output="obj/Release/" />
|
||||
<Option type="1" />
|
||||
<Option compiler="gcc" />
|
||||
<Compiler>
|
||||
<Add option="-O2" />
|
||||
</Compiler>
|
||||
<Linker>
|
||||
<Add option="-s" />
|
||||
</Linker>
|
||||
</Target>
|
||||
</Build>
|
||||
<Compiler>
|
||||
<Add option="-Wall" />
|
||||
<Add option="-fexceptions" />
|
||||
</Compiler>
|
||||
<Unit filename="BracketsMatch.cpp" />
|
||||
<Extensions />
|
||||
</Project>
|
||||
</CodeBlocks_project_file>
|
||||
107
OrigFiles/3-特殊线性表/BracketsMatch(括号匹配)/BracketsMatch.cpp
Normal file
107
OrigFiles/3-特殊线性表/BracketsMatch(括号匹配)/BracketsMatch.cpp
Normal file
@@ -0,0 +1,107 @@
|
||||
/***链栈实现括号匹配***/
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
|
||||
#define OK 1
|
||||
#define ERROR 0
|
||||
#define OVERFLOW -2
|
||||
typedef char SElemType;
|
||||
typedef int Status;
|
||||
typedef struct SNode{
|
||||
int data;
|
||||
struct SNode *next;
|
||||
}SNode,*LinkStack;
|
||||
|
||||
Status InitStack(LinkStack &S)
|
||||
{
|
||||
S = NULL;
|
||||
return OK;
|
||||
}
|
||||
bool StackEmpty(LinkStack S)
|
||||
{
|
||||
if(!S)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
Status Push(LinkStack &S,SElemType e)
|
||||
{
|
||||
SNode *p = new SNode;
|
||||
if(!p)
|
||||
{
|
||||
return OVERFLOW;
|
||||
}
|
||||
p->data = e;
|
||||
p->next = S;
|
||||
S = p;
|
||||
return OK;
|
||||
}
|
||||
Status Pop(LinkStack &S,SElemType &e)
|
||||
{
|
||||
SNode *p;
|
||||
if(!S)
|
||||
return ERROR;
|
||||
e = S->data;
|
||||
p = S;
|
||||
S = S->next;
|
||||
delete p;
|
||||
return OK;
|
||||
}
|
||||
Status GetTop(LinkStack &S,SElemType &e)
|
||||
{
|
||||
if(!S)
|
||||
return ERROR;
|
||||
e = S->data;
|
||||
return OK;
|
||||
}
|
||||
|
||||
//括号的匹配
|
||||
Status Matching(LinkStack S)
|
||||
{
|
||||
//检验表达式中所含括号是否正确匹配,如果匹配,则返回true,否则返回false。
|
||||
//表达式以“#”结束
|
||||
int flag=1; //标记查找结果以控制循环及返回结果
|
||||
char c;
|
||||
SElemType x;
|
||||
cin>>c; //读入第一个字符
|
||||
while(c!='#' && flag)
|
||||
{
|
||||
switch (c){
|
||||
case '[':
|
||||
case '(': //若是左括号,则将其压入栈
|
||||
Push(S,c);
|
||||
break;
|
||||
case ')' : //若是右括号“)”,则根据当前栈顶元素的值分情况考虑
|
||||
GetTop(S,x);
|
||||
if (!StackEmpty(S) && x=='(' ) //若栈非空且栈顶元素是“(”,则匹配成功
|
||||
Pop(S,x);
|
||||
else
|
||||
flag=0; //若栈空或栈顶元素不是“(”,则非法
|
||||
break;
|
||||
case ']' : //若是右括号“]”,则根据当前栈顶元素的值分情况考虑
|
||||
GetTop(S,x);
|
||||
if (!StackEmpty(S) && x=='[' ) //若栈顶元素是“[”,则匹配成功
|
||||
Pop(S,x);
|
||||
else
|
||||
flag=0; //若栈空或栈顶元素不是“[”,则非法
|
||||
break;
|
||||
}//switch
|
||||
cin>>c; //继续读入下一个字符
|
||||
}//while
|
||||
if (StackEmpty(S) && flag )
|
||||
return 1;
|
||||
else
|
||||
return 0;
|
||||
}//Matching
|
||||
|
||||
int main()
|
||||
{
|
||||
LinkStack S;
|
||||
InitStack(S);
|
||||
cout<<"请输入待匹配的表达式,以“#”结束:"<<endl;
|
||||
int flag = (int)Matching(S);
|
||||
if(flag)
|
||||
cout<<"括号匹配成功!"<<endl;
|
||||
else
|
||||
cout<<"括号匹配失败!"<<endl;
|
||||
return 0;
|
||||
}
|
||||
10
OrigFiles/3-特殊线性表/BracketsMatch(括号匹配)/BracketsMatch.layout
Normal file
10
OrigFiles/3-特殊线性表/BracketsMatch(括号匹配)/BracketsMatch.layout
Normal file
@@ -0,0 +1,10 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
|
||||
<CodeBlocks_layout_file>
|
||||
<FileVersion major="1" minor="0" />
|
||||
<ActiveTarget name="Debug" />
|
||||
<File name="BracketsMatch.cpp" open="1" top="1" tabpos="1" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
|
||||
<Cursor>
|
||||
<Cursor1 position="2162" topLine="83" />
|
||||
</Cursor>
|
||||
</File>
|
||||
</CodeBlocks_layout_file>
|
||||
40
OrigFiles/3-特殊线性表/DancePartner(舞伴问题)/DancePartner.cbp
Normal file
40
OrigFiles/3-特殊线性表/DancePartner(舞伴问题)/DancePartner.cbp
Normal file
@@ -0,0 +1,40 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
|
||||
<CodeBlocks_project_file>
|
||||
<FileVersion major="1" minor="6" />
|
||||
<Project>
|
||||
<Option title="DancePartner" />
|
||||
<Option pch_mode="2" />
|
||||
<Option compiler="gcc" />
|
||||
<Build>
|
||||
<Target title="Debug">
|
||||
<Option output="bin/Debug/DancePartner" prefix_auto="1" extension_auto="1" />
|
||||
<Option object_output="obj/Debug/" />
|
||||
<Option type="1" />
|
||||
<Option compiler="gcc" />
|
||||
<Compiler>
|
||||
<Add option="-g" />
|
||||
</Compiler>
|
||||
</Target>
|
||||
<Target title="Release">
|
||||
<Option output="bin/Release/DancePartner" prefix_auto="1" extension_auto="1" />
|
||||
<Option object_output="obj/Release/" />
|
||||
<Option type="1" />
|
||||
<Option compiler="gcc" />
|
||||
<Compiler>
|
||||
<Add option="-O2" />
|
||||
</Compiler>
|
||||
<Linker>
|
||||
<Add option="-s" />
|
||||
</Linker>
|
||||
</Target>
|
||||
</Build>
|
||||
<Compiler>
|
||||
<Add option="-Wall" />
|
||||
<Add option="-fexceptions" />
|
||||
</Compiler>
|
||||
<Unit filename="DancePartner.cpp" />
|
||||
<Extensions>
|
||||
<lib_finder disable_auto="1" />
|
||||
</Extensions>
|
||||
</Project>
|
||||
</CodeBlocks_project_file>
|
||||
117
OrigFiles/3-特殊线性表/DancePartner(舞伴问题)/DancePartner.cpp
Normal file
117
OrigFiles/3-特殊线性表/DancePartner(舞伴问题)/DancePartner.cpp
Normal file
@@ -0,0 +1,117 @@
|
||||
#include<iostream>
|
||||
#include<string>
|
||||
using namespace std;
|
||||
|
||||
struct dancer
|
||||
{ string name;
|
||||
char sex;
|
||||
};
|
||||
|
||||
struct Node
|
||||
{ dancer data;
|
||||
Node* next;
|
||||
}*front,*rear;
|
||||
|
||||
struct LinkQueue
|
||||
{ Node *front;
|
||||
Node *rear;
|
||||
}Gentleman, Lady;
|
||||
|
||||
void InitialLinkQueue(LinkQueue& queue)
|
||||
{ queue.front=new Node;
|
||||
queue.front->next=NULL;
|
||||
queue.rear=queue.front;
|
||||
}
|
||||
|
||||
void DeleteLinkQueue(LinkQueue& queue)
|
||||
{ Node *p;
|
||||
while(queue.front!=NULL)
|
||||
{ p=queue.front;
|
||||
queue.front=queue.front->next;
|
||||
delete p;
|
||||
}
|
||||
}
|
||||
|
||||
void EnQueue(LinkQueue& queue,dancer& value)
|
||||
{
|
||||
Node* s=new Node;
|
||||
s->data=value;
|
||||
s->next=queue.rear->next;
|
||||
queue.rear->next=s;
|
||||
queue.rear=s;
|
||||
}
|
||||
|
||||
int IsEmpty(LinkQueue queue)
|
||||
{
|
||||
if (queue.front==queue.rear) return 1;
|
||||
else return 0;
|
||||
}
|
||||
|
||||
dancer DeQueue(LinkQueue &queue)
|
||||
{ dancer x;
|
||||
Node *p;
|
||||
if (IsEmpty(queue)) throw "队列为空,无法出队列!";
|
||||
p=queue.front->next;
|
||||
x=p->data;
|
||||
queue.front->next=p->next;
|
||||
if (p==queue.rear) queue.rear=queue.front;
|
||||
delete p;
|
||||
return x;
|
||||
}
|
||||
|
||||
dancer GetHead(LinkQueue queue)
|
||||
{ if (IsEmpty(queue)) throw "队列为空,无法取得队首元素!";
|
||||
return queue.front->next->data;
|
||||
}
|
||||
|
||||
void QueueTranverse(LinkQueue queue)
|
||||
{ Node *p;
|
||||
p=queue.front->next;
|
||||
while(p!=NULL)
|
||||
{ cout<<(p->data).name<<" ";
|
||||
p=p->next;
|
||||
}
|
||||
cout<<endl;
|
||||
}
|
||||
|
||||
int main()
|
||||
{ cout<<"请输入舞伴总数量:"<<endl;
|
||||
int num;
|
||||
cin>>num;
|
||||
|
||||
InitialLinkQueue(Gentleman);
|
||||
InitialLinkQueue(Lady);
|
||||
|
||||
for(int i=0;i<num;i++)
|
||||
{ cout<<"请输入舞者性别(F(女) or M(男))及姓名:"<<endl;
|
||||
char sex;
|
||||
cin>>sex;
|
||||
string name;
|
||||
cin>>name;
|
||||
dancer newdancer;
|
||||
newdancer.name=name;
|
||||
newdancer.sex=sex;
|
||||
if(sex=='F')
|
||||
EnQueue(Lady,newdancer);
|
||||
if(sex=='M')
|
||||
EnQueue(Gentleman,newdancer);
|
||||
}
|
||||
|
||||
while ( (!IsEmpty(Gentleman)) && (!IsEmpty(Lady)) )
|
||||
cout<<DeQueue(Gentleman).name<<"<---配对--->"<<DeQueue(Lady).name<<endl;
|
||||
if (!IsEmpty(Gentleman))
|
||||
cout<<GetHead(Gentleman).name<<"先生还在等着呢!"<<endl;
|
||||
else
|
||||
if (!IsEmpty(Lady))
|
||||
cout<<GetHead(Lady).name<<"女士还在等着呢!"<<endl;
|
||||
else
|
||||
cout<<"配对完美结束!"<<endl;
|
||||
|
||||
DeleteLinkQueue(Gentleman);
|
||||
DeleteLinkQueue(Lady);
|
||||
|
||||
system("pause");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
5
OrigFiles/3-特殊线性表/DancePartner(舞伴问题)/DancePartner.depend
Normal file
5
OrigFiles/3-特殊线性表/DancePartner(舞伴问题)/DancePartner.depend
Normal file
@@ -0,0 +1,5 @@
|
||||
# depslib dependency file v1.0
|
||||
1617974749 source:c:\users\86138\desktop\zykathy\dancepartner(Îè°éÎÊÌâ)\dancepartner.cpp
|
||||
<iostream>
|
||||
<string>
|
||||
|
||||
10
OrigFiles/3-特殊线性表/DancePartner(舞伴问题)/DancePartner.layout
Normal file
10
OrigFiles/3-特殊线性表/DancePartner(舞伴问题)/DancePartner.layout
Normal file
@@ -0,0 +1,10 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
|
||||
<CodeBlocks_layout_file>
|
||||
<FileVersion major="1" minor="0" />
|
||||
<ActiveTarget name="Debug" />
|
||||
<File name="DancePartner.cpp" open="1" top="1" tabpos="1" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
|
||||
<Cursor>
|
||||
<Cursor1 position="1512" topLine="18" />
|
||||
</Cursor>
|
||||
</File>
|
||||
</CodeBlocks_layout_file>
|
||||
@@ -0,0 +1,38 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
|
||||
<CodeBlocks_project_file>
|
||||
<FileVersion major="1" minor="6" />
|
||||
<Project>
|
||||
<Option title="EvaluateExpression" />
|
||||
<Option pch_mode="2" />
|
||||
<Option compiler="gcc" />
|
||||
<Build>
|
||||
<Target title="Debug">
|
||||
<Option output="bin/Debug/EvaluateExpression" prefix_auto="1" extension_auto="1" />
|
||||
<Option object_output="obj/Debug/" />
|
||||
<Option type="1" />
|
||||
<Option compiler="gcc" />
|
||||
<Compiler>
|
||||
<Add option="-g" />
|
||||
</Compiler>
|
||||
</Target>
|
||||
<Target title="Release">
|
||||
<Option output="bin/Release/EvaluateExpression" prefix_auto="1" extension_auto="1" />
|
||||
<Option object_output="obj/Release/" />
|
||||
<Option type="1" />
|
||||
<Option compiler="gcc" />
|
||||
<Compiler>
|
||||
<Add option="-O2" />
|
||||
</Compiler>
|
||||
<Linker>
|
||||
<Add option="-s" />
|
||||
</Linker>
|
||||
</Target>
|
||||
</Build>
|
||||
<Compiler>
|
||||
<Add option="-Wall" />
|
||||
<Add option="-fexceptions" />
|
||||
</Compiler>
|
||||
<Unit filename="EvaluateExpression.cpp" />
|
||||
<Extensions />
|
||||
</Project>
|
||||
</CodeBlocks_project_file>
|
||||
@@ -0,0 +1,147 @@
|
||||
/***链栈实现表达式求值***/
|
||||
#include<iostream>
|
||||
#include<string>
|
||||
using namespace std;
|
||||
|
||||
const char oper[7] = {'+','-','*','/','(',')','#'};
|
||||
#define OK 1
|
||||
#define ERROR 0
|
||||
#define OVERFLOW -2
|
||||
typedef char SElemType;
|
||||
typedef int Status;
|
||||
typedef struct SNode{
|
||||
int data;
|
||||
struct SNode *next;
|
||||
}SNode,*LinkStack;
|
||||
|
||||
Status InitStack(LinkStack &S)
|
||||
{
|
||||
S = NULL;
|
||||
return OK;
|
||||
}
|
||||
bool StackEmpty(LinkStack S)
|
||||
{
|
||||
if(!S)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
Status Push(LinkStack &S,SElemType e)
|
||||
{
|
||||
SNode *p = new SNode;
|
||||
if(!p)
|
||||
{
|
||||
return OVERFLOW;
|
||||
}
|
||||
p->data = e;
|
||||
p->next = S;
|
||||
S = p;
|
||||
return OK;
|
||||
}
|
||||
Status Pop(LinkStack &S,SElemType &e)
|
||||
{
|
||||
SNode *p;
|
||||
if(!S)
|
||||
return ERROR;
|
||||
e = S->data;
|
||||
p = S;
|
||||
S = S->next;
|
||||
delete p;
|
||||
return OK;
|
||||
}
|
||||
Status GetTop(LinkStack &S,SElemType &e)
|
||||
{
|
||||
if(!S)
|
||||
return ERROR;
|
||||
e = S->data;
|
||||
return OK;
|
||||
}
|
||||
bool In(char ch)
|
||||
{//判断ch是否为运算符
|
||||
for(int i = 0;i < 7;i ++)
|
||||
{
|
||||
if(ch == oper[i])
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
char Precede(char theta1,char theta2)
|
||||
{//判断运算符优先级
|
||||
if((theta1 == '('&&theta2 == ')')||(theta1 == '#'&&theta2 == '#'))
|
||||
{
|
||||
return '=';
|
||||
}
|
||||
else if(theta1 == '('||theta1 == '#'||theta2 == '('
|
||||
||(theta1 == '+'||theta1 == '-')&&(theta2 == '*'||theta2 == '/'))
|
||||
{
|
||||
return '<';
|
||||
}
|
||||
else
|
||||
return '>';
|
||||
}
|
||||
char Operate(char first,char theta,char second)
|
||||
{//计算两数运算结果
|
||||
switch(theta)
|
||||
{
|
||||
case '+':
|
||||
return (first - '0')+(second - '0')+48;
|
||||
case '-':
|
||||
return (first - '0')-(second - '0')+48;
|
||||
case '*':
|
||||
return (first - '0')*(second - '0')+48;
|
||||
case '/':
|
||||
return (first - '0')/(second - '0')+48;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 表达式求值
|
||||
char EvaluateExpression()
|
||||
{
|
||||
// 算术表达式求值的算符优先算法。设OPTR和OPND分别为运算符栈和操作数栈,
|
||||
// OP 为运算符集合
|
||||
LinkStack OPTR,OPND;
|
||||
char ch,theta,a,b,x,top;
|
||||
InitStack ( OPTR);
|
||||
Push (OPTR,'#');
|
||||
InitStack ( OPND);
|
||||
ch = getchar();
|
||||
while (ch != '#' || (GetTop(OPTR,top) ,top!= '#') )
|
||||
{
|
||||
if (!In(ch))
|
||||
{
|
||||
Push(OPND, ch);
|
||||
ch = getchar();
|
||||
} // ch不是运算符则进栈
|
||||
else
|
||||
switch (GetTop(OPTR, top),Precede(top,ch))
|
||||
{ //比较OPTR的栈顶元素和ch的优先权
|
||||
case '<': //当前字符ch压入OPTR栈,读入下一字符ch
|
||||
Push(OPTR, ch);
|
||||
ch = getchar();
|
||||
break;
|
||||
case '>': //弹出OPTR栈顶的运算符进行相应运算,并将运算结果入栈
|
||||
Pop(OPTR, theta);
|
||||
Pop(OPND, b);
|
||||
Pop(OPND, a);
|
||||
Push(OPND, Operate(a, theta, b));
|
||||
break;
|
||||
case '=': //脱括号并接收下一字符
|
||||
Pop(OPTR, x);
|
||||
ch = getchar();
|
||||
break;
|
||||
} // switch
|
||||
} // while
|
||||
GetTop(OPND,ch);
|
||||
return ch;
|
||||
} // EvaluateExpression
|
||||
|
||||
int main()
|
||||
{
|
||||
cout<<"请输入要计算的表达式(操作数和结果都在0-9的范围内,以#结束):"<<endl;
|
||||
char res = EvaluateExpression();
|
||||
cout<<"计算结果为"<<res<<endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
# depslib dependency file v1.0
|
||||
1617954043 source:c:\users\86138\desktop\zykathy\evaluateexpression(±í´ïʽ¼ÆËã)\evaluateexpression.cpp
|
||||
<iostream>
|
||||
<string>
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
|
||||
<CodeBlocks_layout_file>
|
||||
<FileVersion major="1" minor="0" />
|
||||
<ActiveTarget name="Debug" />
|
||||
<File name="EvaluateExpression.cpp" open="1" top="1" tabpos="1" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
|
||||
<Cursor>
|
||||
<Cursor1 position="2981" topLine="18" />
|
||||
</Cursor>
|
||||
</File>
|
||||
</CodeBlocks_layout_file>
|
||||
97
OrigFiles/3-特殊线性表/LinkQueue(链队)/LinkQueue.cpp
Normal file
97
OrigFiles/3-特殊线性表/LinkQueue(链队)/LinkQueue.cpp
Normal file
@@ -0,0 +1,97 @@
|
||||
#include<iostream> //cout,cin
|
||||
using namespace std;
|
||||
#include "LinkQueue.h"
|
||||
|
||||
void dispmenu()
|
||||
{//显示主菜单
|
||||
cout<<endl;
|
||||
cout<<"1-初始化链队\n";
|
||||
cout<<"2-元素入队\n";
|
||||
cout<<"3-元素出队\n";
|
||||
cout<<"4-取队头元素\n";
|
||||
cout<<"5-取队尾元素\n";
|
||||
cout<<"6-测队空\n";
|
||||
cout<<"7-显示队列元素\n";
|
||||
cout<<"8-退出\n";
|
||||
}
|
||||
|
||||
char pause;
|
||||
|
||||
//主函数
|
||||
int main()
|
||||
{
|
||||
int e;
|
||||
LinkQueue<int> Q;
|
||||
system("cls"); //执行系统命令cls,清屏
|
||||
|
||||
int choice;
|
||||
do
|
||||
{
|
||||
dispmenu(); //显示主菜单
|
||||
cout<<"Enter choice(1~8):";
|
||||
cin>>choice;
|
||||
switch(choice)
|
||||
{
|
||||
case 1: //初始化链队
|
||||
InitQueue(Q);
|
||||
cout<<endl<<"创建成功!"<<endl;
|
||||
break;
|
||||
case 2: //入栈
|
||||
cout<<"输入要插入的元素值:"<<endl;
|
||||
cin>>e;
|
||||
cout<<endl;
|
||||
if(EnQueue(Q,e))
|
||||
cout<<endl<<"入队成功!"<<endl;
|
||||
else
|
||||
cout<<endl<<"入队不成功!"<<endl;
|
||||
break;
|
||||
|
||||
case 3: //出栈
|
||||
if(DeQueue(Q,e))
|
||||
{
|
||||
cout<<endl<<"出队元素为:"<<e<<endl;
|
||||
cout<<endl<<"出队成功!"<<endl;
|
||||
}
|
||||
else
|
||||
cout<<endl<<"队空,出队失败!"<<endl;
|
||||
break;
|
||||
|
||||
case 4: //获取队头元素
|
||||
if(GetHead(Q,e))
|
||||
{
|
||||
cout<<endl<<"队头元素为:"<<e<<endl;
|
||||
}
|
||||
else
|
||||
cout<<endl<<"队空!"<<endl;
|
||||
break;
|
||||
case 5: //获取队尾元素
|
||||
if(GetTail(Q,e))
|
||||
{
|
||||
cout<<endl<<"队尾元素为:"<<e<<endl;
|
||||
}
|
||||
else
|
||||
cout<<endl<<"队空!"<<endl;
|
||||
break;
|
||||
case 6: //测队空
|
||||
if(QueueEmpty(Q))
|
||||
cout<<endl<<"空队!"<<endl;
|
||||
else
|
||||
cout<<endl<<"不是空队!"<<endl;
|
||||
break;
|
||||
case 7: //查看队列元素
|
||||
DispQueue(Q);
|
||||
cout<<endl;
|
||||
cin.get(pause);
|
||||
system("pause");
|
||||
break;
|
||||
case 8: //退出
|
||||
DestroyQueue(Q);
|
||||
cout<<"结束运行"<<endl;
|
||||
break;
|
||||
default: //非法选择
|
||||
cout<<"Invalid choice\n";
|
||||
break;
|
||||
}
|
||||
}while(choice!=8);
|
||||
return 0;
|
||||
}//end of main function
|
||||
@@ -35,21 +35,6 @@ void DestroyQueue(LinkQueue<DT> &Q)//
|
||||
Q.front=Q.front->next;
|
||||
delete p;
|
||||
}
|
||||
Q.rear=Q.front=NULL;
|
||||
}
|
||||
|
||||
template <class DT>
|
||||
void ClearQueue(LinkQueue<DT> &Q) // 清空链队
|
||||
{
|
||||
QNode<DT> *p;
|
||||
while(Q.front->next) //从队头开始,依次释放结点
|
||||
{
|
||||
p=Q.front->next;
|
||||
Q.front->next=p->next;
|
||||
delete p;
|
||||
}
|
||||
Q.front->next=NULL; // 空队
|
||||
Q.rear=Q.front;
|
||||
}
|
||||
|
||||
//【算法3.21】 入队
|
||||
88
OrigFiles/3-特殊线性表/LinkStack(链栈)/LinkStack.cpp
Normal file
88
OrigFiles/3-特殊线性表/LinkStack(链栈)/LinkStack.cpp
Normal file
@@ -0,0 +1,88 @@
|
||||
#include<iostream> //cout,cin
|
||||
using namespace std;
|
||||
#include "LinkStack.h"
|
||||
|
||||
|
||||
void dispmenu()
|
||||
{//显示主菜单
|
||||
cout<<endl;
|
||||
cout<<"1-初始化链栈\n";
|
||||
cout<<"2-元素入栈\n";
|
||||
cout<<"3-元素出栈\n";
|
||||
cout<<"4-取栈顶元素\n";
|
||||
cout<<"5-测栈空\n";
|
||||
cout<<"6-显示栈元素\n";
|
||||
cout<<"7-退出\n";
|
||||
}
|
||||
|
||||
char pause;
|
||||
|
||||
//主函数
|
||||
int main()
|
||||
{
|
||||
int e;
|
||||
LNode<int> * S;
|
||||
system("cls"); // 清屏
|
||||
int choice;
|
||||
do
|
||||
{
|
||||
dispmenu(); //显示主菜单
|
||||
cout<<"Enter choice(1~7):";
|
||||
cin>>choice;
|
||||
switch(choice)
|
||||
{
|
||||
case 1: //初始化链栈
|
||||
InitStack(S);
|
||||
cout<<endl<<"创建成功!"<<endl;
|
||||
break;
|
||||
case 2: //入栈
|
||||
cout<<"输入要插入的元素值:"<<endl;
|
||||
cin>>e;
|
||||
cout<<endl;
|
||||
if(Push(S,e))
|
||||
cout<<endl<<"入栈成功!"<<endl;
|
||||
else
|
||||
cout<<endl<<"入栈不成功!"<<endl;
|
||||
break;
|
||||
|
||||
case 3: //出栈
|
||||
if(Pop(S,e))
|
||||
{
|
||||
cout<<endl<<"出栈元素为:"<<e<<endl;
|
||||
cout<<endl<<"出栈成功!"<<endl;
|
||||
}
|
||||
else
|
||||
cout<<endl<<"栈空,出栈失败!"<<endl;
|
||||
break;
|
||||
|
||||
case 4: //获取栈顶元素
|
||||
if(GetTop(S,e))
|
||||
{
|
||||
cout<<endl<<"栈顶元素为:"<<e<<endl;
|
||||
}
|
||||
else
|
||||
cout<<endl<<"栈空!"<<endl;
|
||||
break;
|
||||
case 5: //测栈空
|
||||
if(StackEmpty(S))
|
||||
cout<<endl<<"空栈!"<<endl;
|
||||
else
|
||||
cout<<endl<<"不是空栈!"<<endl;
|
||||
break;
|
||||
case 6: //显示栈元素
|
||||
DispStack(S);
|
||||
cout<<endl;
|
||||
cin.get(pause);
|
||||
system("pause");
|
||||
break;
|
||||
case 7: //退出
|
||||
DestroyStack(S);
|
||||
cout<<"结束运行"<<endl;
|
||||
break;
|
||||
default: //非法选择
|
||||
cout<<"Invalid choice\n";
|
||||
break;
|
||||
}
|
||||
}while(choice!=7);
|
||||
return 0;
|
||||
}//end of main function
|
||||
94
OrigFiles/3-特殊线性表/LinkStack(链栈)/LinkStack.h
Normal file
94
OrigFiles/3-特殊线性表/LinkStack(链栈)/LinkStack.h
Normal file
@@ -0,0 +1,94 @@
|
||||
|
||||
template <class DT>
|
||||
struct LNode //结点
|
||||
{
|
||||
DT data; //数据域,存储数据元素值
|
||||
LNode *next;//指针域,指向下一个结点
|
||||
};
|
||||
|
||||
|
||||
//【算法3.6】
|
||||
template <class DT>
|
||||
void InitStack(LNode<DT> *&S)//创建空链栈
|
||||
{
|
||||
//L=new LNode<DT>; //创建头结点
|
||||
//if(!L) exit(1); //创建失败,结束运行
|
||||
S=NULL;
|
||||
}
|
||||
|
||||
//【算法3.7】
|
||||
template <class DT>
|
||||
void DestroyStack(LNode<DT> *(&S))//释放链栈
|
||||
{
|
||||
LNode<DT> *p;
|
||||
while(S)//从头结点开始,依次释放结点
|
||||
{
|
||||
p=S;
|
||||
S=S->next;
|
||||
delete p;
|
||||
}
|
||||
//L=NULL;//头结点指向空
|
||||
}
|
||||
|
||||
//【算法3.8】
|
||||
template<class DT>
|
||||
bool Push(LNode<DT> *&S,DT e)
|
||||
{
|
||||
LNode<DT> *p;
|
||||
p=new LNode<DT>;
|
||||
if(!p) return false; //创建失败,结束运行
|
||||
p->data=e; // 新结点赋值
|
||||
p->next=S; //结点S链接到p结点之后
|
||||
S=p;
|
||||
return true; // 插入成功,返回true
|
||||
}
|
||||
|
||||
//【算法3.9】
|
||||
template<class DT>
|
||||
bool Pop(LNode<DT> *&S,DT &e)
|
||||
{
|
||||
LNode<DT> *p;
|
||||
if(S==NULL) return false;
|
||||
p=S;
|
||||
e=p->data;
|
||||
S=S->next;
|
||||
delete p;
|
||||
return true; // 删除成功,返回true
|
||||
}
|
||||
|
||||
|
||||
//【算法3.10】
|
||||
template<class DT>
|
||||
bool GetTop(LNode<DT> *S,DT &e)
|
||||
{
|
||||
LNode<DT> *p;
|
||||
if(S==NULL) return false;
|
||||
p=S;
|
||||
e=p->data;
|
||||
return true; // 删除成功,返回true
|
||||
}
|
||||
|
||||
//测栈空
|
||||
template<class DT>
|
||||
bool StackEmpty(LNode<DT> *S)
|
||||
{
|
||||
if(S==NULL)
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
//显示栈内容
|
||||
template<class DT>
|
||||
void DispStack(LNode<DT> *S)
|
||||
{
|
||||
LNode<DT> *p;
|
||||
p=S;
|
||||
while(p)
|
||||
{
|
||||
cout<<p->data<<"\t";
|
||||
p=p->next;
|
||||
}
|
||||
cout<<endl;
|
||||
}
|
||||
|
||||
111
OrigFiles/3-特殊线性表/SqQueue(循环队列)/SqQueue.cpp
Normal file
111
OrigFiles/3-特殊线性表/SqQueue(循环队列)/SqQueue.cpp
Normal file
@@ -0,0 +1,111 @@
|
||||
|
||||
#include<iostream>//cout,cin
|
||||
using namespace std;
|
||||
#include "SqQueue.h"
|
||||
|
||||
char pause;
|
||||
|
||||
|
||||
void dispmenu()
|
||||
{//显示主菜单
|
||||
cout<<endl;
|
||||
cout<<"1-初始化顺序队列\n";
|
||||
cout<<"2-元素入队\n";
|
||||
cout<<"3-元素出队\n";
|
||||
cout<<"4-取队头元素\n";
|
||||
cout<<"5-取队尾元素\n";
|
||||
cout<<"6-测队空\n";
|
||||
cout<<"7-测队满\n";
|
||||
cout<<"8-显示队列元素\n";
|
||||
cout<<"9-退出\n";
|
||||
}
|
||||
|
||||
|
||||
//主函数
|
||||
int main()
|
||||
{
|
||||
int i;
|
||||
int e;
|
||||
SqQueue<int> Q;//建立容量为20、元素类型为整型的空顺序栈
|
||||
system("cls");//执行系统命令cls,清屏
|
||||
|
||||
int choice;
|
||||
do
|
||||
{
|
||||
dispmenu();//显示主菜单
|
||||
cout<<"Enter choice(1~9):";
|
||||
cin>>choice;
|
||||
switch(choice)
|
||||
{
|
||||
case 1://初始化顺序队列
|
||||
cout<<"请输入要创建的顺序队列的长度";
|
||||
cin>>i;
|
||||
cout<<endl;
|
||||
InitQueue (Q,i);
|
||||
cout<<endl<<"创建成功!"<<endl;
|
||||
break;
|
||||
case 2://入队
|
||||
cout<<"输入要入队的元素值:"<<endl;
|
||||
cin>>e;
|
||||
cout<<endl;
|
||||
if(EnQueue(Q,e))
|
||||
cout<<endl<<"入队成功!"<<endl;
|
||||
else
|
||||
cout<<endl<<"入队不成功!"<<endl;
|
||||
break;
|
||||
case 3://出队
|
||||
if(DeQueue(Q,e))
|
||||
{
|
||||
cout<<endl<<"出队元素为:"<<e<<endl;
|
||||
cout<<endl<<"出队成功!"<<endl;
|
||||
}
|
||||
else
|
||||
cout<<endl<<"队空,出队失败!"<<endl;
|
||||
break;
|
||||
case 4://取队头元素
|
||||
if(GetHead(Q,e))
|
||||
{
|
||||
cout<<endl<<"队头元素为:"<<e<<endl;
|
||||
}
|
||||
else
|
||||
cout<<endl<<"队空!"<<endl;
|
||||
break;
|
||||
case 5://取队尾元素
|
||||
if(GetTail(Q,e))
|
||||
{
|
||||
cout<<endl<<"队尾元素为:"<<e<<endl;
|
||||
}
|
||||
else
|
||||
cout<<endl<<"队空!"<<endl;
|
||||
break;
|
||||
case 6://测队空
|
||||
if(QueueEmpty(Q))
|
||||
cout<<endl<<"空队!"<<endl;
|
||||
else
|
||||
cout<<endl<<"不是空队!"<<endl;
|
||||
break;
|
||||
case 7://测队满
|
||||
if(QueueFull(Q))
|
||||
cout<<endl<<"队满!"<<endl;
|
||||
else
|
||||
cout<<endl<<"队不满!"<<endl;
|
||||
break;
|
||||
case 8://显示队列元素
|
||||
DispQueue(Q);
|
||||
cout<<endl;
|
||||
cin.get(pause);
|
||||
system("pause");
|
||||
break;
|
||||
case 9://退出
|
||||
DestroyQueue(Q);
|
||||
cout<<"结束运行"<<endl;
|
||||
break;
|
||||
default://非法选择
|
||||
cout<<"Invalid choice\n";
|
||||
break;
|
||||
}
|
||||
}while(choice!=9);
|
||||
|
||||
return 0;
|
||||
}//end of main function
|
||||
|
||||
111
OrigFiles/3-特殊线性表/SqQueue(循环队列)/SqQueue.h
Normal file
111
OrigFiles/3-特殊线性表/SqQueue(循环队列)/SqQueue.h
Normal file
@@ -0,0 +1,111 @@
|
||||
template <class DT>
|
||||
struct SqQueue //顺序队类
|
||||
{
|
||||
DT *base; //队列首址
|
||||
int front; //队头指针
|
||||
int rear; //队尾指针
|
||||
int queuesize;//队容量
|
||||
};
|
||||
|
||||
//基本操作的实现
|
||||
//【算法3.14】
|
||||
template <class DT>
|
||||
void InitQueue(SqQueue<DT> &Q, int m)
|
||||
{//构建函数,创建一栈容量为m的空队
|
||||
Q.base=new DT[m];// 申请队列空间
|
||||
if(Q.base==NULL)
|
||||
{
|
||||
cout<<"未创建成功!";
|
||||
exit (1);
|
||||
}
|
||||
Q.front=Q.rear=0;
|
||||
Q.queuesize=m;
|
||||
}
|
||||
|
||||
|
||||
//算法3.15】
|
||||
template <class DT>
|
||||
void DestroyQueue(SqQueue<DT> &Q)//析构函数
|
||||
{//释放队列空间
|
||||
delete [] Q.base;
|
||||
Q.front=Q.rear=0;
|
||||
Q.queuesize=0;
|
||||
}
|
||||
|
||||
|
||||
//【算法3.16】
|
||||
template<class DT>
|
||||
bool EnQueue(SqQueue<DT> &Q,DT e)
|
||||
{
|
||||
if((Q.rear+1)%Q.queuesize==Q.front) //队满
|
||||
return false;
|
||||
Q.base[Q.rear]=e;
|
||||
Q.rear=(Q.rear+1)% Q.queuesize;
|
||||
return true; // 插入成功,返回true
|
||||
}
|
||||
|
||||
//【算法3.17】
|
||||
template<class DT>
|
||||
bool DeQueue(SqQueue<DT> &Q,DT &e)
|
||||
{
|
||||
if(Q.front==Q.rear) //队空
|
||||
return false;
|
||||
e=Q.base[Q.front];
|
||||
Q.front=(Q.front+1)%Q.queuesize;
|
||||
return true; // 删除成功,返回true
|
||||
}
|
||||
|
||||
|
||||
//【算法3.18】
|
||||
template<class DT>
|
||||
bool GetHead(SqQueue<DT> Q,DT &e)
|
||||
{
|
||||
if(Q.front==Q.rear) //队空
|
||||
return false;
|
||||
e=Q.base[Q.front];
|
||||
return true; //返回true
|
||||
}
|
||||
|
||||
//取队尾元素
|
||||
template<class DT>
|
||||
bool GetTail(SqQueue<DT> Q,DT &e)
|
||||
{
|
||||
if(Q.front==Q.rear) //队空
|
||||
return false;
|
||||
e=Q.base[(Q.rear - 1+Q.queuesize) % Q.queuesize];
|
||||
return true; //返回true
|
||||
}
|
||||
|
||||
//测队空
|
||||
template<class DT>
|
||||
bool QueueEmpty(SqQueue<DT> Q)
|
||||
{
|
||||
if(Q.front==Q.rear)
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
//测队满
|
||||
template<class DT>
|
||||
bool QueueFull(SqQueue<DT> Q)
|
||||
{
|
||||
if((Q.rear+1)%Q.queuesize==Q.front) //队满
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
//显示队列内容
|
||||
template<class DT>
|
||||
void DispQueue(SqQueue<DT> Q)
|
||||
{
|
||||
int i=Q.front;
|
||||
while(i!=Q.rear)
|
||||
{
|
||||
cout<<Q.base[i]<<"\t";
|
||||
i=(i+1)%Q.queuesize;
|
||||
}
|
||||
cout<<endl;
|
||||
}
|
||||
|
||||
95
OrigFiles/3-特殊线性表/SqStack(顺序栈)/SqStack.cpp
Normal file
95
OrigFiles/3-特殊线性表/SqStack(顺序栈)/SqStack.cpp
Normal file
@@ -0,0 +1,95 @@
|
||||
|
||||
#include<iostream>//cout,cin
|
||||
using namespace std;
|
||||
#include "SqStack.h"
|
||||
|
||||
char pause;
|
||||
|
||||
|
||||
void dispmenu()
|
||||
{ //显示主菜单
|
||||
cout<<endl;
|
||||
cout<<"1-初始化顺序栈\n";
|
||||
cout<<"2-元素入栈\n";
|
||||
cout<<"3-元素出栈\n";
|
||||
cout<<"4-取栈顶元素\n";
|
||||
cout<<"5-测栈空\n";
|
||||
cout<<"6-显示栈元素\n";
|
||||
cout<<"7-退出\n";
|
||||
}
|
||||
|
||||
|
||||
//主函数
|
||||
int main()
|
||||
{
|
||||
int i;
|
||||
int e;
|
||||
SqStack<int> S;//建立容量为20、元素类型为整型的空顺序栈
|
||||
system("cls"); // 清屏
|
||||
|
||||
int choice;
|
||||
do
|
||||
{
|
||||
dispmenu(); // 显示主菜单
|
||||
cout<<"Enter choice(1~7):";
|
||||
cin>>choice;
|
||||
switch(choice)
|
||||
{
|
||||
case 1: // 初始化顺序栈
|
||||
cout<<"请输入要创建的顺序栈的长度";
|
||||
cin>>i;
|
||||
cout<<endl;
|
||||
InitStack (S,i);
|
||||
cout<<endl<<"创建成功!"<<endl;
|
||||
break;
|
||||
case 2: // 入栈
|
||||
cout<<"输入要入栈的元素值:"<<endl;
|
||||
cin>>e;
|
||||
cout<<endl;
|
||||
if(Push(S,e))
|
||||
cout<<endl<<"入栈成功!"<<endl;
|
||||
else
|
||||
cout<<endl<<"入栈不成功!"<<endl;
|
||||
break;
|
||||
case 3: // 出栈
|
||||
if(Pop(S,e))
|
||||
{
|
||||
cout<<endl<<"出栈元素为:"<<e<<endl;
|
||||
cout<<endl<<"出栈成功!"<<endl;
|
||||
}
|
||||
else
|
||||
cout<<endl<<"栈空,出栈失败!"<<endl;
|
||||
break;
|
||||
case 4: // 取栈顶元素
|
||||
if(GetTop(S,e))
|
||||
{
|
||||
cout<<endl<<"栈顶元素为:"<<e<<endl;
|
||||
}
|
||||
else
|
||||
cout<<endl<<"栈空!"<<endl;
|
||||
break;
|
||||
case 5: // 测栈空
|
||||
if(StackEmpty(S))
|
||||
cout<<endl<<"空栈!"<<endl;
|
||||
else
|
||||
cout<<endl<<"不是空栈!"<<endl;
|
||||
break;
|
||||
case 6: // 显示栈元素
|
||||
DispStack(S);
|
||||
cout<<endl;
|
||||
cin.get(pause);
|
||||
system("pause");
|
||||
break;
|
||||
case 7: // 退出
|
||||
DestroyStack(S);
|
||||
cout<<"结束运行"<<endl;
|
||||
break;
|
||||
default: // 无效选择
|
||||
cout<<"Invalid choice\n";
|
||||
break;
|
||||
}
|
||||
}while(choice!=7);
|
||||
|
||||
return 0;
|
||||
}//end of main function
|
||||
|
||||
89
OrigFiles/3-特殊线性表/SqStack(顺序栈)/SqStack.h
Normal file
89
OrigFiles/3-特殊线性表/SqStack(顺序栈)/SqStack.h
Normal file
@@ -0,0 +1,89 @@
|
||||
template <class DT>
|
||||
struct SqStack // 顺序栈
|
||||
{
|
||||
DT *base; // 栈首址
|
||||
int top; // 栈顶指针
|
||||
int stacksize; // 栈容量
|
||||
};
|
||||
|
||||
//基本操作的实现
|
||||
//【算法3.1】 // 初始化栈
|
||||
template <class DT>
|
||||
void InitStack(SqStack<DT> &S, int m)
|
||||
{
|
||||
S.base=new DT[m]; // 申请栈空间
|
||||
if(S.base==NULL) // 申请失败,退出
|
||||
{
|
||||
cout<<"未创建成功!";
|
||||
exit (1);
|
||||
}
|
||||
S.top=-1; // 设置空栈属性
|
||||
S.stacksize=m;
|
||||
}
|
||||
|
||||
|
||||
//算法3.2】 // 销毁栈
|
||||
template <class DT>
|
||||
void DestroyStack(SqStack<DT> &S)
|
||||
{
|
||||
delete [] S.base; // 释放栈空间
|
||||
S.top=-1;
|
||||
S.stacksize=0; // 设置栈属性
|
||||
}
|
||||
|
||||
|
||||
//【算法3.3】 // 入栈
|
||||
template<class DT>
|
||||
bool Push(SqStack<DT> &S,DT e)
|
||||
{
|
||||
if(S.top==S.stacksize-1) // 栈满,不能入栈
|
||||
return false; // 返回false
|
||||
S.top++;
|
||||
S.base[S.top]=e;
|
||||
return true; // 入栈成功,返回true
|
||||
}
|
||||
|
||||
//【算法3.4】 // 出栈
|
||||
template<class DT>
|
||||
bool Pop(SqStack<DT> &S,DT &e)
|
||||
{
|
||||
if(S.top==-1) // 栈空
|
||||
return false; // 返回false
|
||||
e=S.base[S.top]; // 取栈顶元素
|
||||
S.top--; // 栈顶指针下移
|
||||
return true; // 出栈成功,返回true
|
||||
}
|
||||
|
||||
|
||||
//【算法3.5】 // 获取栈顶元素
|
||||
template<class DT>
|
||||
bool GetTop(SqStack<DT> S,DT &e)
|
||||
{
|
||||
if(S.top==-1) // 栈空
|
||||
return false; // 返回false
|
||||
e=S.base[S.top]; // 栈非空,取栈顶元素
|
||||
return true; // 返回true
|
||||
}
|
||||
|
||||
// 测栈空
|
||||
template<class DT>
|
||||
bool StackEmpty(SqStack<DT> S)
|
||||
{
|
||||
if(S.top==-1) // 空栈,返回true
|
||||
return true;
|
||||
else // 空栈,返回false
|
||||
return false;
|
||||
}
|
||||
|
||||
//显示栈内容
|
||||
template<class DT>
|
||||
void DispStack(SqStack<DT> S)
|
||||
{
|
||||
int i=S.top;
|
||||
while(i>=0)
|
||||
{
|
||||
cout<<S.base[i--]<<"\t";
|
||||
}
|
||||
cout<<endl;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user