第3章栈和队列

         在写了2章博客后我突然明白了许多,有些东西只是写下来其实是没有多大用,只不过是把东西从一个地方搬到另一个地方,而我其实的确是仅仅把课本上的内容抄了一遍而已,现在我觉得数据结构最重要的是算法,因此如果想学好它首先应该把算法看懂,其次把它理解着背过,毕竟最后还是用在操作上,所以我觉得第3章的重点在于对算法的理解。有些东西只有记在自己脑子里才是最重要的。以下是我对于数据结构第3章的自我理解。

       本章分为两块,一是栈,二是队列。分别从顺序存储和逻辑存储来介绍的。

对于栈我认为就是个木桶,数据就像里面有不流动的水,木桶的底部就是栈底,上面就是栈顶,空栈就是桶里没水。

它遵循后进先出,即先进去的在最低部,却是最后出来的。

下面是顺序栈的相关算法

template<class DataType>

void SeqStack<class DataType>::push(DataType x)

{

if(top==stacksize-1)throw"上溢";//判断是否栈满

top++;

data[top]=x;

}

template<class DataType>

DataType SeqStack<class DataType>::Pop()

{

if(top==-1)throw"下溢"

x=data[top];

top--;//删除使得下移

return x;

}

两栈共享

template<class DataType>

void bothstack<DataType>::push(int i,DataType x)

{

if(top1==top2-1)throw"上溢";

if(i==1)

data[++top1]=x;

if(i==2)

data[top2++]=x;

}

template<class DataType>

DataType bothstack<class DataType>::Pop(int i)

{if(i==1)

{

if(top1==-1)throw"下溢";

return data[top1--];

if(i==2)

if(top2==stacksize-1)throw"下溢";

return data[top2++];

}

}

链栈的相关算法

template<class DataType>

void LInkStack<class DataType>::push(DataType x)

{s=new node;s->data=x;

s->next=top;top=s;//入栈使得top指针上移

}

template<class DataType>

void Linkstack<class DataType>

DataType<class DataType>::Pop()

{

if(top==NULL)throw" 下溢";

x=top->data;p=top;

top=top->next;//摘链

delete p;

return x;

}

关于栈实际上是利用top进行一系列的操作的;其指向栈顶元素.

队列的相关算法

队列的认识就像火车在轨道上行驶对头像是火车头,对尾像是最后一个车厢,它遵循先入先出的原则如同火车进入隧道车头先进车头先出。

循环对列的相关算法

template<class DataType>

void CirQueue<class DataType>::EnQueue(DataType x)

{

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

rear=(rear+1)%QueueSize;

data[rear]=x;

}

template<classDataType>

DataType CirQueue<class DataType>::DeQueue()

{

if(rear==front)throw"下溢";

front=(front+1)%QueueSize;

return data[front];

}

template<classDataType>

DataType CirQueue<classDataType>::GetQueue()

{if(rear==front)throw"下溢";

i=(front+1)%QueueSize;

return data[i];

}

链队列的相关算法

template<classs DataType>

LinkQueue<class DataType>::LinkQueue()

{

s=new node;s->next=NULL;

front=rear=s;

}

template<class DataType>

void LinkQueue<class DataType>::EnQueue()

{

s=new node;s->data=x;

s->next=NULL;

rear->next=s;

rear=s;

}

template<class DataType>

DataType LinkQueue<class DataType>::DeQueue()

{

if(rear==front)throw"下溢"

p=front->next;x=p->data;

front->next=p->next;

if(p->next==NULL)throw"下溢";

detele p;

return x;

}
关于队列实际是利用rear与front实现的.

以上是我对第三章的自我理解.























评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值