203
![](https://i-blog.csdnimg.cn/blog_migrate/09630ab7fa85bc206a873160df743a53.png)
#include "allinclude.h" //DO NOT edit this line
Status StackEmpty_Sq(SqStack S)
{
// Add your code here
if(S.top==NULL)
{
return TRUE;
}
else {
return FALSE;
}
}
205
![](https://i-blog.csdnimg.cn/blog_migrate/75304674f86a2776e63020329075120d.png)
#include "allinclude.h" //DO NOT edit this line
Status GetTop_Sq(SqStack S, ElemType &e)
{// Add your code here
if(S.top==NULL)
{
return ERROR;
}
else{
e=S.elem[S.top-1];
return OK;
}
}
207
![](https://i-blog.csdnimg.cn/blog_migrate/b21b2d45e3cf02d9adc409191bc2912f.png)
#include "allinclude.h" //DO NOT edit this line
Status Pop_Sq(SqStack &S, ElemType &e) {
// Add your code here
if(S.top==NULL)
{
return ERROR;
}
else
{
e=S.elem[S.top-1];
--S.top;
return OK;
}
}
211
![](https://i-blog.csdnimg.cn/blog_migrate/4ffb2f6dd26acec5742ccd2b8163f5eb.png)
#include "allinclude.h" //DO NOT edit this line
Status InitStack_Sq2(SqStack2 &S, int size, int inc) {
// Add your code here
S.elem=(ElemType*)malloc(size*sizeof(ElemType));//分配存储空间
if(S.elem==NULL||inc<=0||size<=0)
{
return ERROR;//不成功的情况
}
S.top=S.elem;//置S为空栈
S.size=size;//初始容量值
S.increment=inc;//初始增量值
return OK;
}
213
![](https://i-blog.csdnimg.cn/blog_migrate/cdc65b8fc56592d0ae7a0225c14634c7.png)
#include "allinclude.h" //DO NOT edit this line
Status StackEmpty_Sq2(SqStack2 S)
{
// Add your code here
if(S.top==S.elem)
{
return TRUE;
}
else
{
return FALSE;
}
}
215
![](https://i-blog.csdnimg.cn/blog_migrate/22ec8c2f47d71d988420345fbfe7df01.png)
#include "allinclude.h" //DO NOT edit this line
Status Push_Sq2(SqStack2 &S, ElemType e) {
// Add your code here
/*if(S.top>=S.elem+S.size)
{
S.elem=(ElemType*)realloc(S.elem,(S.size+1)*sizeof(ElemType));
if(NULL==S.elem)
{
return ERROR;
}
S.size+=1;
}
*(S.top++)=e;
return OK;*/
if(S.top>=S.elem+S.size)
{
S.elem=(ElemType*)realloc(S.elem,(S.size+S.increment)*sizeof(ElemType));
if(NULL==S.elem)
{
return ERROR;
}
S.size+=S.increment;
}
*(S.top++)=e;
return OK;
}
217
![](https://i-blog.csdnimg.cn/blog_migrate/5f44e11aef14c29813f4912af3ee234d.png)
#include "allinclude.h" //DO NOT edit this line
Status Pop_Sq2(SqStack2 &S, ElemType &e) {
// Add your code here
//S.elem=(ElemType*)malloc(S.size*sizeof(ElemType));
if(S.elem==S.top)
{
return ERROR;
}
e=*(S.top-1);
S.top--;
return OK;
}
219
![](https://i-blog.csdnimg.cn/blog_migrate/754ff8807b61f9bc92abff78258a148d.png)
#include "allinclude.h" //DO NOT edit this line
Status CopyStack_Sq(SqStack S1, SqStack &S2) {
// Add your code here
S2.elem=(ElemType*)malloc(S1.size*sizeof(ElemType));
if(StackEmpty_Sq(S1))
{
S2.top=0;
return TRUE;
}
else {
S2.elem=S1.elem;
S2.top=S1.top;
}
return TRUE;
}
220
![](https://i-blog.csdnimg.cn/blog_migrate/7dd479c6f6605885e7b04378261af21e.png)
#include "allinclude.h"
void Conversion(int N, int rd)
{ // Add your code here
SqStack S;
ElemType e;
InitStack_Sq(S,10,5);//栈S的初始容量为10
while(N!=0){
Push_Sq(S,N%rd);//将N除以8的余数入栈
N/=rd;//N取值为其除以8的商
}
while(FALSE==StackEmpty_Sq(S)){//依次输出栈的余数
Pop_Sq(S,e);
printf("%d",e);
}
}
221
![](https://i-blog.csdnimg.cn/blog_migrate/16d97eaf06c8d0ea005c83b904495345.png)
#include "allinclude.h"
Status matchBracketSequence(char* exp, int n)
{ // Add your code here
int i=0;
ElemType e;//基址
SqStack S;//设置栈S
InitStack_Sq(S,n,INCREMENT);//设置S为空栈
while(i<n)//循环
{
switch(exp[i])//分支语句判断括号类型
{
case '(':
case '[':
case '{':
Push_Sq(S,exp[i]);//将前括号放入栈
i++;
break;
case ')':
case ']':
case '}':
if(TRUE==StackEmpty_Sq(S))//判断是否有前括号
{
return ERROR;
}
else
{
GetTop_Sq(S,e);//取出最后放入的前括号
if((exp[i]==')'&&e=='(')||(exp[i]==']'&&e=='[')||(exp[i]=='}'&&e=='{'))//判断是否匹配
{
Pop_Sq(S,e);//将匹配上的前括号出栈
i++;
}
else
{
return ERROR;
}
}
break;
}
}
if(TRUE==StackEmpty_Sq(S))//判断栈是否为空栈,是则全部括号对应上
{
return OK;
}
else//否则存在前括号没有对应的后括号,使得前括号无法出栈
{
return ERROR;
}
}
223
![](https://i-blog.csdnimg.cn/blog_migrate/04f127dcc72b399de3ff0fe832a5f79e.png)
#include "allinclude.h" //DO NOT edit this line
int QueueLength_Sq(SqQueue Q) {
// Add your code here
int i=0;
//①
/*while(Q.rear!=Q.front)
{
if(Q.front==Q.maxSize-1)
{
Q.front=0;
}
else
{
Q.front++;
}
i++;
}*/
//②
/*while(Q.rear!=Q.front)
{
Q.front=(Q.front+1)%Q.maxSize;
i++;
}*/
//③
/*while(Q.front!=Q.rear)
{
Q.front++;
if(Q.front==Q.maxSize)
{
Q.front=0;
}
i++;
}*/
return i;
}
225
![](https://i-blog.csdnimg.cn/blog_migrate/ad1131c3e69eef1ec39622f2711aeb15.png)
#include "allinclude.h" //DO NOT edit this line
Status EnCQueue(CTagQueue &Q, ElemType x) {
// Add your code here
if(Q.front==Q.rear&&Q.tag==1)
{
return ERROR;//队列为空报错
}
else
{
Q.rear[Q.elem]=x;
Q.rear=(Q.rear+1)%MAXSIZE;
}
if(Q.rear==Q.front)//判断加完后是满
{
Q.tag=1;
}
return OK;
}
Status DeCQueue(CTagQueue &Q, ElemType &x){
// Add your code here
if(Q.front==Q.rear&&Q.tag==0)
{
return ERROR; //队列为空报错
}
else
{
x=Q.front[Q.elem];
Q.front=(Q.front+1)%MAXSIZE;
}
if(Q.rear==Q.front)//判断出队后是否为空
{
Q.tag=0;
}
return OK;
}
227
![](https://i-blog.csdnimg.cn/blog_migrate/7c90bb1e9b8c96bcf74a82120003ac34.png)
#include "allinclude.h" //DO NOT edit this line
Status EnCQueue(CLenQueue &Q, ElemType x) { //加入队列
// Add your code here
if(Q.length==MAXQSIZE)
{
return ERROR;//队满报错
}
else
{
Q.rear=(Q.rear+1)%MAXQSIZE;
Q.elem[Q.rear]=x;
Q.length++;
return OK;
}
}
Status DeCQueue(CLenQueue &Q, ElemType &x){//取队头元素
// Add your code here
if(Q.length==0)
{
return ERROR;//队空报错
}
else
{
x=Q.elem[(Q.rear+MAXQSIZE-Q.length+1)%MAXQSIZE];
Q.length--;
}
return OK;
}
232
![](https://i-blog.csdnimg.cn/blog_migrate/64eb825c41d2557fd8b38e9d2958daea.png)
#include "allinclude.h" //DO NOT edit this line
long Fib(int k, int n) {
// Add your code here
SqQueue S;
if(k<2||n<0)//不存在的情况
{
return ERROR;
}
if(n<k-1)//k-1前面的项都是0
{
return 0;
}
else if(n==k-1)//第k-1项时为1
{
return 1;
}
else {
S.base=(ElemType*)malloc((n+1)*sizeof(ElemType));//开辟空间
S.base[k-1]=1;
int i,j,sum;
for(i=0;i<k-1;i++)
{
S.base[i]=0;
}
for(i=k;i<n+1;i++)
{
sum=0;
for(j=i-k;j<i;j++)
{
sum+=S.base[j];
}
S.base[i]=sum;
}
}
return S.base[n];
}
233
![](https://i-blog.csdnimg.cn/blog_migrate/7a0071074cb9cd73c5160c0348b52947.png)
#include "allinclude.h" //DO NOT edit this line
char Compare(SqList A, SqList B)
{ // Add your code here
int i=0,j=0;
if(A.elem==NULL&&B.elem==NULL)//两个均为空表
{
return '=';
}
else if(A.elem==NULL&&B.elem!=NULL)//A为空表,B不为空表
{
return '<';
}
else if(A.elem!=NULL&&B.elem==NULL)//A不为空表,B为空表
{
return '>';
}
else //均不为空表
{
while(i<A.length&&j<A.length)
{
if(A.elem[i]==B.elem[j])//相等比较两个的下一项
{
i++;
j++;
}
else if(A.elem[i]>B.elem[j])//A的元素大于B的元素
{
return '>';
}
else if(B.elem[j]>A.elem[i])//B的元素大于A的元素
{
return '<';
}
}
if(i<A.length)//B已经没有元素
{
return '>';
}
else if(j<B.length)//A已经没有元素
{
return '<';
}
else
{
return '=';//两个均没有元素
}
}
}
235
![](https://i-blog.csdnimg.cn/blog_migrate/0a1bd76e5eb4ffb7c635156c468a64a5.png)
#include "allinclude.h" //DO NOT edit this line
void Inverse(SqList &L)
{ // Add your code here
int i=0,t;
for(i=0;i<L.length/2;i++)
{
t=L.elem[i];
L.elem[i]=L.elem[L.length-i-1];
L.elem[L.length-i-1]=t;
}
}
245
![](https://i-blog.csdnimg.cn/blog_migrate/d9a594e5d58ac30f0a7d97ce2c0b9243.png)
#include "allinclude.h" //DO NOT edit this line
void Union(SqList &La, List Lb)
{ // Add your code here
int count = 0;
for(int i=0;i<Lb.length;i++)
{
if(Search_Sq(La,Lb.elem[i])==-1)//判断B中的元素是否在A中,在A中查找
{
count++;//找到多的个数
}
La.elem=(ElemType*)realloc(La.elem,(La.size+count)*sizeof(ElemType));//重新分配空间
if(NULL==La.elem)//重新分配失败的情况
{
return ;
}
for(int i=0;i<Lb.length;i++)
{
if(Search_Sq(La,Lb.elem[i])==-1)//判断B中的元素是否在A中,在A中查找
{
Append_Sq(La,Lb.elem[i]);//添加元素
}
}
}
}
253
![](https://i-blog.csdnimg.cn/blog_migrate/c503a543f181bb5c0dcbb1721cabf457.png)
#include "allinclude.h" //DO NOT edit this line
Status StackEmpty_L(LStack S)
{ // Add your code here
if(S==NULL)
{
return TRUE;
}
else {
return FALSE;
}
}
255
![](https://i-blog.csdnimg.cn/blog_migrate/86d417849b2d1b860974c0f11868357d.png)
#include "allinclude.h" //DO NOT edit this line
Status GetTop_L(LStack S, ElemType &e)
{ // Add your code here
if(S==NULL)
{
return ERROR;
}
else
{
e=S->data;
return OK;
}
}
261
![](https://i-blog.csdnimg.cn/blog_migrate/aa761fe2a4f532b88745bbf02cc996d9.png)
#include "allinclude.h" //DO NOT edit this line
Status QueueEmpty_LQ(LQueue Q)
{ // Add your code here
if(Q.front==NULL)
{
return TRUE;
}
else{
return FALSE;
}
}
263
![](https://i-blog.csdnimg.cn/blog_migrate/b7afe51e9fb1568595daa04e17182fe9.png)
#include "allinclude.h" //DO NOT edit this line
int QueueLength_LQ(LQueue Q)
{ // Add your code here
LQNode* p;
p=Q.front;
int length=1;
if(Q.front==NULL)//判空
{
return 0;
}
while(p!=Q.rear)
{
p=p->next;
length++;
}
return length;
}
268
![](https://i-blog.csdnimg.cn/blog_migrate/c974b6cb17dcdf5db37e3f624f586cbc.png)
#include "allinclude.h" //DO NOT edit this line
Status InitCLQueue(CLQueue &rear)
{ // Add your code here
rear=(CLQueue)malloc(sizeof(CLQNode));
if(NULL==rear)
{
return ERROR;
}
else{
rear->next=rear;
return OK;
}
}
Status EnCLQueue(CLQueue &rear, ElemType x)
{ // Add your code here
CLQueue p;
p=(CLQueue)malloc(sizeof(CLQNode));
if(p==NULL)
{
return ERROR;
}
p->data=x;
p->next=rear->next;
rear->next=p;
rear=p;
}
Status DeCLQueue(CLQueue &rear, ElemType &x)
{ // Add your code here
CLQueue p,q;
q=rear->next;
if(rear->next==rear)//空队列
{
return ERROR;
}
p=q->next;
x=p->data;
q->next=p->next;
free(p);
return OK;
}
271
![](https://i-blog.csdnimg.cn/blog_migrate/643420573c0dbe188a3354f13b863c32.png)
#include "allinclude.h" //DO NOT edit this line
Status ListEmpty_L(LinkList L)
{ // Add your code here
if(L->next==NULL)
{
return TRUE;
}
else
{
return FALSE;
}
}
273
![](https://i-blog.csdnimg.cn/blog_migrate/e784ea3b992755edb8eca3c576477cea.png)
#include "allinclude.h" //DO NOT edit this line
Status DestroyList_L(LinkList &L)
{ // Add your code here
LNode *p;
while(L!=NULL)
{
p=L->next;
free(L);
L=p;
}
return OK;
}
275
![](https://i-blog.csdnimg.cn/blog_migrate/3749a9e84b6accb6e82b76ce23fcd8ab.png)
#include "allinclude.h" //DO NOT edit this line
Status ClearList_L(LinkList &L)
{ // Add your code here
LNode *p,*q;
if(L==NULL)
{
return ERROR;
}
else
{
p=L->next;
while(p!=NULL)
{
q=p->next;
free(p);
p=q;
}
}
L->next=NULL;
return OK;
}
277
![](https://i-blog.csdnimg.cn/blog_migrate/2a5131b9bce774599ec84508fcdf5e13.png)
#include "allinclude.h" //DO NOT edit this line
int ListLength_L(LinkList L)
{ // Add your code here
LNode *p;
if(L==NULL)
{
return -1;
}
p=L->next;
int length=0;
while(p)
{
length++;
p=p->next;
}
return length;
}
283
![](https://i-blog.csdnimg.cn/blog_migrate/edc000f2b8bca458174a635fb596d737.png)
#include "allinclude.h" //DO NOT edit this line
Status Insert_L(LinkList L, int i, ElemType e)
{ // Add your code here
LNode *q,*p;
q=(LNode*)malloc(sizeof(LNode));
q->data=e;
p=L;
int j=1;
while(i>j&&p)
{
p=p->next;
j++;
}
if(NULL==p||q==NULL||i<1)
{
return ERROR;
}
q->next=p->next;
p->next=q;
return OK;
}
284
![](https://i-blog.csdnimg.cn/blog_migrate/3d309e534ee9eff414b3fc346f989e07.png)
#include "allinclude.h" //DO NOT edit this line
Status Delete_L(LinkList L, int i, ElemType &e)
{ // Add your code here
LNode*p,*q;
p=L;
int j=1;
if(i<1)
{
return ERROR;
}
while(i>j&&p)
{
p=p->next;
j++;
}
if(NULL==p||p->next==NULL)
{
return ERROR;
}
q=p->next;
p->next=q->next;
e=q->data;
free(q);
return OK;
}
286
![](https://i-blog.csdnimg.cn/blog_migrate/921fd56057b069688a736bb2d80ad9e5.png)
#include "allinclude.h" //DO NOT edit this line
Status Split_L(LinkList L, LinkList &Li, int i)
{ // Add your code here
LNode*p,*q;
q=(LNode*)malloc(sizeof(LNode));
q->data=NULL;
p=L;
int j=1;
while(j<i&&p)
{
p=p->next;
j++;
}
if(p==NULL||p->next==NULL||i<1)
{
Li=NULL;
return ERROR;
}
q->next=p->next;
Li=q->next;
Li=q;
p->next=NULL;
return OK;
}
288
#include "allinclude.h" //DO NOT edit this line
Status Cut_L(LinkList L, int i)
{ // Add your code here
LNode*p,*q;
if(NULL==L||L->next==NULL||i<1)
{
return ERROR;
}
p=L;
int j=1;
while(j<i&&p)
{
p=p->next;
j++;
if(p->next==NULL)
{
return ERROR;
}
}
p->next=NULL;
while(p)
{
q=p->next;
p=q;
}
return OK;
}
290
![](https://i-blog.csdnimg.cn/blog_migrate/cc5394043daec9be08c35dde5d430afc.png)
#include "allinclude.h" //DO NOT edit this line
Status DeleteX_L(LinkList L, ElemType x)
{ // Add your code here
LNode *p,*q;
int i=0;
p=L;
while(p)
{
if(p==NULL||p->next==NULL)
{
return i;
}
if(p->next->data==x)
{
q=p->next->next;
p->next=q;
i++;
}
p=p->next;
}
return i;
}
291
![](https://i-blog.csdnimg.cn/blog_migrate/dfa9ce522bb2fce5cafd83c5365705b4.png)
#include "allinclude.h" //DO NOT edit this line
Status DeleteSome_L(LinkList L, ElemType x)
{ // Add your code here
LNode *p,*q,*t;
int i=0;
p=L->next;
q=L;
while(p)
{
if(p->data<x)
{
t=p;
p=p->next;
free(t);
q->next=p;
i++;
}
else
{
q=p;
p=p->next;
}
}
return i;
}
293
![](https://i-blog.csdnimg.cn/blog_migrate/6b7dedde29319608cc3c5d533ad125cd.png)
#include "allinclude.h" //DO NOT edit this line
DuLinkList delDuplicateDuLNodes(DuLinkList L)
{ // Add your code here
if (L == NULL)
{
return ERROR;
}
DuLinkList p, q;
for (p=L->next;p!=NULL;p=p->next)
{
for (q=p->next;q!=NULL;q=q->next)
{
if (p->data==q->data)
{
q->prior->next=q->next;
if (q->next!=NULL)
{
q->next->prior=q->prior;
}
free(q);
}
}
}
return L;
}
294
#include "allinclude.h" //DO NOT edit this line
void reverseDuLinkList(DuLinkList L)
{ // Add your code here
if(L == NULL || L->next == NULL)
{
return;
}
DuLinkList p = L->next; // p指向第一个结点
L->next = NULL;
while(p!= NULL)
{
DuLinkList q = p->next; // q指向p的后继结点
p->next = L->next;
if(L->next != NULL)
{
L->next->prior = p;
}
L->next = p;
p->prior = L;
p = q;
}
}
295
![](https://i-blog.csdnimg.cn/blog_migrate/816b55f197b4698ac77e5db96e3f7690.png)
#include "allinclude.h" //DO NOT edit this line
void reverseDuCirLinkList(DuCirLinkList L)
{ // Add your code here
if(L==NULL||L->next==L) // 空链表或只有头结点
{
return;
}
DuLinkList p=L->next; // p指向第一个结节点
L->next=L; // 头结点的next指针指向自己
while(p!=L)
{
DuLinkList q=p->next; // q指向p的后继结点
p->next=L->next;
p->prior=L;
L->next->prior=p;
L->next=p;
p=q;
}
}
297
![](https://i-blog.csdnimg.cn/blog_migrate/52a0a5ebaf62b0d13b2e87e754f4b093.png)
#include "allinclude.h" //DO NOT edit this line
void InterleavedTravelDuCirLinkList(DuCirLinkList L, DuLNode* p)
{ // Add your code here
DuLNode *q,*t;
if(L==NULL)
{
return ;
}
if(p==L&&p->next==L)
{
return ;
}
q=p->next;
t=p->prior;
while(1)
{
if(t==L&&q==L)
{
break;
}
if(t==q)
{
printf("%c",q->data);
break;
}
if(L==q)
{
q=q->next;
if(t==q)
{
printf("%c",q->data);
break;
}
}
if(L==t)
{
t=t->prior;
if(t==q)
{
printf("%c",q->data);
break;
}
}
printf("%c",q->data);
printf("%c",t->data);
if(t->prior==q)
{
break;
}
q=q->next;
t=t->prior;
}
printf("%c",p->data);
}
298
![](https://i-blog.csdnimg.cn/blog_migrate/86ebf3e2eba342d82b5589d2f0a71a2c.png)
#include "allinclude.h" //DO NOT edit this line
Status isLeagalDuCirLinkList(DuCirLinkList L)
{ // Add your code here
// 判断是否为空链表
if (L == NULL)
{
return TRUE;
}
DuLinkList p = L; // 当前节点
DuLinkList q;
do {
while (q != L) {
q = p->next;
if(p==NULL||p->next==NULL)
{
return FALSE;
}
if (p!=q->prior) {
return FALSE;
}
p=p->next;
}
p = p->next;
} while (p != L);
return TRUE;
}