Error[8]: Undefined offset: 1, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 121
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

概述本文实例讲述了C语言用栈和队列实现的回文功能。分享给大家供大家参考,具体如下:

本文实例讲述了C语言用栈和队列实现的回文功能。分享给大家供大家参考,具体如下:

#include<stdio.h>#include<malloc.h>//内存分配头文件#include<math.h>//在math.h中已定义OVERFLOW的值为3#define SIZE 100#define STACKINCREMENT 10#define OK 1#define ERROR 0#define TRUE 1#define FALSE 0typedef int Status;typedef struct   //栈的结构体{  char a;} SElemType;typedef struct{  SElemType *base;  SElemType *top;  int stacksize;} SqStack;typedef struct //QNode //队列的结构体{  char b;  struct QNode * next;} QNode,*QueuePtr;typedef struct // 链队列类型{  QueuePtr front;  // 队头指针  QueuePtr rear;  // 队尾指针} linkQueue;//定义全局变量SqStack S;SElemType e;linkQueue Q;QueuePtr p;char f;//栈 *** 作Status InitStack(SqStack *S){  S->base=(SElemType *)malloc(SIZE*sizeof(SElemType));  if(!S->base) exit(OVERFLOW);  S->top=S->base;  S->stacksize=SIZE;  return OK;}Status Push(SqStack *S,SElemType e){  if(S->top-S->base>=S->stacksize)  {    S->base=(SElemType *)malloc((S->stacksize+STACKINCREMENT)*sizeof(SElemType));    if(!S->base) exit(OVERFLOW);    S->top=S->base+S->stacksize;    S->stacksize+=STACKINCREMENT;  }  *S->top++=e;  return OK;}Status Stackempty(SqStack S)//栈是否为空{  if(S.top==S.base)    return TRUE;  else    return FALSE;}Status Pop(SqStack *S,SElemType *e){  if(S->top==S->base) return ERROR;  *e=*--S->top;  return OK;}Status StackLength(SqStack S)//求栈的长度{  return (S.top-S.base);}//队列 *** 作Status InitQueue(linkQueue *Q){  Q->front=(QueuePtr)malloc(sizeof(QNode));  Q->rear=Q->front;  if(!Q->front) exit(OVERFLOW);  Q->front->next=NulL;  return OK;}Status EnQueue(linkQueue *Q,char f){  p=(QueuePtr)malloc(sizeof(QNode));  if(!p) exit(OVERFLOW);  p->b=f;  p->next=NulL;  Q->rear->next=p;  Q->rear=p;  return OK;}Status DeQueue(linkQueue *Q,char *f){  if(Q->front==Q->rear) return ERROR;  p=Q->front->next;  *f=p->b;  Q->front->next=p->next;  if(Q->rear==p)    Q->rear=Q->front;  free(p);  return OK;}Status QueueLength(linkQueue Q){  int i=0;  p=Q.front;  while(Q.rear!=p)  {    i++;    p=p->next;  }  return i;}Status QueueEmpty(linkQueue Q){  if(Q.front==Q.rear)    return TRUE;  else    return FALSE;}voID main(){  int i,m;  char n,a[20];  InitStack(&S);  InitQueue(&Q);  gets(a);  for(i=0; a[i]!='&'; i++) ///////////    &前的数据进栈  {    e.a=a[i];    Push(&S,e);  }  for(i=i+1; a[i]!='[+++]'; i++) //////////   ‘ &'后的数据进入队列    EnQueue(&Q,a[i]);  if( StackLength(S)!=QueueLength(Q))    /////栈和队列的数据个数不一样    printf("NO!!!!!!!!!!!!!!!!!!!!!!!!!!!!");  else    while(!Stackempty(S)&&!QueueEmpty(Q))///////栈和队列里还有数据    {      Pop(&S,&e);      m=e.a;      DeQueue(&Q,&f);      n=f;      if(m!=n)      {        printf("NO!!!!!!!!!!!!!!!!!!!!!!");        break;      }    }  if(m==n&&Stackempty(S)&&QueueEmpty(Q))    printf("YES!!!!!!!!!!!!!!!!!!!!!!");}

运行结果:

希望本文所述对大家C语言程序设计有所帮助。

总结

以上是内存溢出为你收集整理的C语言用栈和队列实现的回文检测功能示例全部内容,希望文章能够帮你解决C语言用栈和队列实现的回文检测功能示例所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 166, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
C语言用栈和队列实现的回文检测功能示例_C_内存溢出

C语言用栈和队列实现的回文检测功能示例

C语言用栈和队列实现的回文检测功能示例,第1张

概述本文实例讲述了C语言用栈和队列实现的回文功能。分享给大家供大家参考,具体如下:

本文实例讲述了C语言用栈和队列实现的回文功能。分享给大家供大家参考,具体如下:

#include<stdio.h>#include<malloc.h>//内存分配头文件#include<math.h>//在math.h中已定义OVERFLOW的值为3#define SIZE 100#define STACKINCREMENT 10#define OK 1#define ERROR 0#define TRUE 1#define FALSE 0typedef int Status;typedef struct   //栈的结构体{  char a;} SElemType;typedef struct{  SElemType *base;  SElemType *top;  int stacksize;} SqStack;typedef struct //QNode //队列的结构体{  char b;  struct QNode * next;} QNode,*QueuePtr;typedef struct // 链队列类型{  QueuePtr front;  // 队头指针  QueuePtr rear;  // 队尾指针} linkQueue;//定义全局变量SqStack S;SElemType e;linkQueue Q;QueuePtr p;char f;//栈 *** 作Status InitStack(SqStack *S){  S->base=(SElemType *)malloc(SIZE*sizeof(SElemType));  if(!S->base) exit(OVERFLOW);  S->top=S->base;  S->stacksize=SIZE;  return OK;}Status Push(SqStack *S,SElemType e){  if(S->top-S->base>=S->stacksize)  {    S->base=(SElemType *)malloc((S->stacksize+STACKINCREMENT)*sizeof(SElemType));    if(!S->base) exit(OVERFLOW);    S->top=S->base+S->stacksize;    S->stacksize+=STACKINCREMENT;  }  *S->top++=e;  return OK;}Status Stackempty(SqStack S)//栈是否为空{  if(S.top==S.base)    return TRUE;  else    return FALSE;}Status Pop(SqStack *S,SElemType *e){  if(S->top==S->base) return ERROR;  *e=*--S->top;  return OK;}Status StackLength(SqStack S)//求栈的长度{  return (S.top-S.base);}//队列 *** 作Status InitQueue(linkQueue *Q){  Q->front=(QueuePtr)malloc(sizeof(QNode));  Q->rear=Q->front;  if(!Q->front) exit(OVERFLOW);  Q->front->next=NulL;  return OK;}Status EnQueue(linkQueue *Q,char f){  p=(QueuePtr)malloc(sizeof(QNode));  if(!p) exit(OVERFLOW);  p->b=f;  p->next=NulL;  Q->rear->next=p;  Q->rear=p;  return OK;}Status DeQueue(linkQueue *Q,char *f){  if(Q->front==Q->rear) return ERROR;  p=Q->front->next;  *f=p->b;  Q->front->next=p->next;  if(Q->rear==p)    Q->rear=Q->front;  free(p);  return OK;}Status QueueLength(linkQueue Q){  int i=0;  p=Q.front;  while(Q.rear!=p)  {    i++;    p=p->next;  }  return i;}Status QueueEmpty(linkQueue Q){  if(Q.front==Q.rear)    return TRUE;  else    return FALSE;}voID main(){  int i,m;  char n,a[20];  InitStack(&S);  InitQueue(&Q);  gets(a);  for(i=0; a[i]!='&'; i++) ///////////    &前的数据进栈  {    e.a=a[i];    Push(&S,e);  }  for(i=i+1; a[i]!=''; i++) //////////   ‘ &'后的数据进入队列    EnQueue(&Q,a[i]);  if( StackLength(S)!=QueueLength(Q))    /////栈和队列的数据个数不一样    printf("NO!!!!!!!!!!!!!!!!!!!!!!!!!!!!");  else    while(!Stackempty(S)&&!QueueEmpty(Q))///////栈和队列里还有数据    {      Pop(&S,&e);      m=e.a;      DeQueue(&Q,&f);      n=f;      if(m!=n)      {        printf("NO!!!!!!!!!!!!!!!!!!!!!!");        break;      }    }  if(m==n&&Stackempty(S)&&QueueEmpty(Q))    printf("YES!!!!!!!!!!!!!!!!!!!!!!");}

运行结果:

希望本文所述对大家C语言程序设计有所帮助。

总结

以上是内存溢出为你收集整理的C语言用栈和队列实现的回文检测功能示例全部内容,希望文章能够帮你解决C语言用栈和队列实现的回文检测功能示例所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/langs/1245006.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存