Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use stack and queue to realize palindrome detection in C language

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

In this article, Xiaobian introduces in detail the "C language how to use stack and queue to achieve palindrome detection", with detailed content, clear steps and proper handling of details. I hope that this article "C language how to use stack and queue to achieve palindromes detection" article can help you solve your doubts, following the editor's ideas slowly in-depth, together to learn new knowledge.

The specific code is as follows:

# include#include// memory allocation header file # include// defines in math.h the structure {char an of OVERFLOW with the value of 3#define SIZE 100#define STACKINCREMENT 10#define OK 1#define ERROR 0#define TRUE 1#define FALSE 0typedef int Status;typedef struct / / stack;} SElemType;typedef struct {SElemType * base; SElemType * top; int stacksize;} SqStack;typedef struct / / QNode / / queue structure {char b; struct QNode * next;} QNode,*QueuePtr Typedef struct / / chain queue type {QueuePtr front; / / head-of-line pointer QueuePtr rear; / / end-of-line pointer} LinkQueue;// defines the global variable SqStack SElemType eTracterLinkQueue QTX QueuePtr psitchar malloc / stack operation 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 * Sjournal 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) / / whether the stack is empty {if (S.top==S.base) return TRUE; else return FALSE } Status Pop (SqStack * top==S- SElemType * e) {if (S-> top==S- > base) return ERROR; * estranged wafers S-> top; return OK;} Status StackLength (SqStack S) / / find the stack length {return (S.top-S.base);} / / queue operation 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 rear- char f) {p = (QueuePtr) malloc (sizeof (QNode)); if (! p) exit (OVERFLOW); p-> baffle; p-> next=NULL; Q-> rear- > next=p; Q-> rear=p; return OK;} Status DeQueue (LinkQueue * Q dagger char * f) {if (Q-> front==Q- > rear) return ERROR; packs Q-> front- > next; * fagger-> b; Q-> front- > next=p- > next; if (Q-> rear==p) Q-> rear=Q- > front; free (p) Return OK;} Status QueueLength (LinkQueue Q) {int ionome0; pairQ.front; while (Q.rearman Q.front) {iLifex; pairp-> next;} return I;} Status QueueEmpty (LinkQueue Q) {if (Q.front==Q.rear) return TRUE; else return FALSE;} void main () {int ivoid main (); char njue a [20]; InitStack (& S); InitQueue (& Q); gets (a); for (ionom0; a [I]! ='&'' The data before entering the stack {e.a=a [I]; Push (& Spene);} for (iScript1; a [I]! ='\ 0mm; iScribe +) /'& enter queue EnQueue (& QCoA [I]) If (StackLength (S)! = QueueLength (Q)) / the number of data in the stack and queue is not the same as printf ("Notification!"); else while (! Stackempty (S) & &! QueueEmpty (Q)) / there is data {Pop (& S) in the stack and queue; m=e.a DeQueue (& Q m==n&&Stackempty (S) & & QueueEmpty (Q)) printf ("Yeshan!"); break;}} if (m==n&&Stackempty (S) & & QueueEmpty (Q))

Running result:

After reading this, the article "how C language uses stacks and queues to achieve palindrome detection" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it before you can understand it. If you want to know more about related articles, welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report