服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - C语言用栈和队列实现的回文检测功能示例

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

2021-05-14 14:11PHP开发学习门户 C/C++

这篇文章主要介绍了C语言用栈和队列实现的回文检测功能,结合具体实例形式分析了C语言栈和队列的定义及使用栈和队列进行回文检测的操作技巧,需要的朋友可以参考下

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#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 0
typedef 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]!='\0'; 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语言程序设计有所帮助。

延伸 · 阅读

精彩推荐