第四次

栈和队列

栈的示意图:

在这里插入图片描述

操作特性:后进先出

存储结构:顺序存储和链接存储

(1)
顺序栈

类的声明:

const intStackSize=100;

template

class SeqStack

{

public:

SeqStack( ){top=-1;} ;

~SeqStack( );

void Push (
T x );

T Pop ( );

T GetTop( )
{if (top!=-1) return data[top];}

boolEmpty ( ) { if(top==-1) return 1;else return 0;}

private:

T data[StackSize];

inttop;

}

入栈:
在这里插入图片描述
void Push(T x)

template

void SeqStack::Push( T x)

{

if (top==StackSize-1)

throw “溢出”;

top++;

data[top]=x;

}

出栈:
在这里插入图片描述
T Pop()

template
T

SeqStack:: Pop ( )

{

if (top==-1)

throw “溢出”;

x=data[top–];

return x;

}

(2)链栈

template

class LinkStack

{

public:

LinkStack(){top=NULL;}

~LinkStack( );

void Push(T x);

T Pop( );

T GetTop( ){if(top!=NULL)returntop->data;}

Bool Empty ( ){return(top==NULL? 1: 0);}

private:

Node *top;

}

入栈:

template

void LinkStack::Push(Tx)

{

s=new
Node;

s->data=x;

s->next=top;

top=s;

}

出栈:

template

T LinkStack::Pop( )

{

if (top==NULL)
throw “下溢”;

x=top->data;

p=top;

top=top->next;

delete p;

return x;

}

循环队列:

在这里插入图片描述

队空的条件:front=rear

队满的条件:(rear+1) mod QueueSize=front

类的声明:

const intQueueSize=100;

template

class CirQueue

{

public:

CirQueue(
){front=rear=QueueSize-1;}

~
CirQueue( );

void
EnQueue(Tx);

T DeQueue(
);

T
GetQueue( );

Bool Empty(
){return(front==rear? 1: 0);}

private:

T
data[QueueSize];

Int front,
rear;

}

入队:

template

void CirQueue::EnQueue(T x)

{

if ((rear+1) % QueueSize ==front) throw “上溢”;

rear=(rear+1) % QueueSize; data[rear]=x;

}

出队:

template

T CirQueue::DeQueue( )

{ if (rear==front) throw “下溢”; front=(front+1) %
QueueSize;

return data[front];

}

读队头元素:

template

T CirQueue::GetQueue( )

{ if (rear==front) throw “下溢”; i=(front+1) %
QueueSize;

return data[i];

}

链队列:

类的声明:

template

class LinkQueue

{

public:

LinkQueue( );
~LinkQueue( ); void EnQueue(T x);

T DeQueue( );

T GetQueue( );

int Empty( ){return(front==rear?1:0);}

private:

Node *front, *rear;

};

构造函数: LinkQueue( );

front template LinkQueue::LinkQueue( )

{ rear
front=new Node;

front->next=NULL;

rear=front;

}

入队:

template

void LinkQueue::EnQueue(T x)

{

s=new Node; s->data=x; s->next=NULL;
rear->next=s; rear=s;

}

出队:

template

T LinkQueue::DeQueue( )

{

if (rear==front) throw “下溢”;

p=front->next; x=p->data; front->next=p->next; if (p->next==NULL) rear=front;

delete p; return x;

}

括号匹配:

算法思想:

1) 凡出现左括号,则进栈;

2) 凡出现右括号,首先检查栈是否空若栈空,则表明该“右括号”多余,

否则和栈顶元素比较,若相匹配,则“左括号出栈” ,否则表明不匹配。

3) 表达式检验结束时,

若栈空,则表明表达式中匹配正确,否则表明“左括号”有余。

bool matching(char exp[]) {

// 检验表达式中所含括号是否正确嵌套,

//若是,则返回TRUE,否则返回FALSE. ‘#’ 为表达式的结束符 int state = 1,i=0; char ch,e; ch = exp[i++];

SeqStack S; // 构造空栈 while (ch!=’\0’ && state) { if(ch==’(’ || ch==’[’)
S.Push(ch); // 凡左括号一律入栈

else if(ch== ‘)’) if (!S.Empty() &&
S.GetTop()’(’) e=S.Pop(); else state =
0; else if(ch
’]’)

if (!S.Empty() && S.GetTop()==’[’) e=S.Pop();
else state = 0;

ch = exp[i++];

} // while

if ( state && S.Empty() ) return 1; else return 0;

}//matching

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值