C language uses queue and stack to realize palindrome judgment

Problem Description

\quad A palindrome refers to a sequence of characters based on the middle character and the characters on both sides are identical. The program is required to input a character string from the keyboard to determine the end mark of the palindrome that does not include the character string.

Algorithm idea

\quad Store the characters in the string into the queue and the stack one by one, and then compare the queued elements and the unstacked elements one by one. If they are all equal, the character is a palindrome, otherwise it is not a palindrome .

Function module

( 1 ) v o i d P a l i n d r o m e ( c h a r s t r [ ] ) (1) void Palindrome(char str[]) ( 1 ) v o i d P a l i n d r o m e ( c h a r s t r [ ] ) to determine whether the character sequence is a palindrome.
(2) void E nter S tr (charstr []) (2) void EnterStr(char str[])( 2 ) v o i d E n t e r S t r ( c h a r s t r [ ] ) , input the palindrome sequence from the keyboard.
(3) void main () (3) void main()( . 3 ) V O I D m A I n- ( ) , the main function, the cycle is calledP alindrome (charstr []),)P A L I n- D R & lt O m E ( C H A R & lt S T R & lt [ ] ) , E n- T E R & lt S T R & lt ( C H A R & lt S T R & lt [ ] ) , when the user is required to withdraw stop running .
(4) (4)( 4 ) Stack and queue respectively useS tack. h Stack.hS t a c k . hLQ ueue. h LQueue.hL Q u e u e . H files.

Test Data

( 1 ) A B C D E F E D C B A (1) ABCDEFEDCBA 1ABCDEFEDCBA
( 2 ) A B C D E F F E D C B A (2)ABCDEFFEDCBA 2ABCDEFFEDCBA
( 3 ) A B C D B C A (3)ABCDBCA 3ABCDBCA

C language program

head File

S t a c k . h Stack.h Stack.h

#ifndef SEQLIST_H_INCLUDED
#define SEQLIST_H_INCLUDED
#endif // SEQLIST_H_INCLUDED
//typedef int DataType;
typedef struct snode
{
    
    
    DataType data;
    struct snode *next;
}LSNode;
void StackInitiate(LSNode **head)
{
    
    
    *head=(LSNode *)malloc(sizeof(LSNode));
    (*head)->next=NULL;
}
int StackNotEmpty(LSNode *head)
{
    
    
    if(head->next==NULL)return 0;
    else return 1;
}

int StackPush(LSNode *head,DataType x)
{
    
    
    LSNode *p;
    p=(LSNode *)malloc(sizeof(LSNode));
    p->data=x;
    p->next=head->next;//新的结点插入在头结点后面
    head->next=p;//新结点成为栈顶
    return 1;
}
int StackPop(LSNode *head,DataType *x)
{
    
    
    LSNode *p=head->next;
    if(p==NULL)
    {
    
    
     printf("此栈已空!\n");
     return 0;
    }
    head->next=p->next;//删除原结点
    *x=p->data;
    free(p);
    return 1;
}
int StackTop(LSNode *head,DataType *x)
{
    
    
    LSNode *p=head->next;
    if(p==NULL)
    {
    
    
        printf("堆栈已空!\n");
        return 0;
    }
    *x=p->data;
    return 1;
}

void Destory(LSNode *head)
{
    
    
    LSNode *p,*s;
    p=head;
    while(p!=NULL)
    {
    
    
        s=p;
        p=p->next;
        free(s);
    }
}

The tail. h Lqueue.hL q u e u e . B

#ifndef LQUEUE_H_INCLUDED
#define LQUEUE_H_INCLUDED
#endif // LQUEUE_H_INCLUDED
//typedef int DataType;
typedef struct qnode
{
    
    
    DataType data;//为什么不在此处定义两个指针
    struct qnode *next;//此处需要使用qnode,因此typedef struct qnode
}LQNode;//定义结点

typedef struct
{
    
    //不带头结点
    LQNode *front;
    LQNode *rear;
}LQueue;//只是两个指针

void QueueInitiate(LQueue *Q)
{
    
    
    Q->front=NULL;
    Q->rear=NULL;
}
int QueueNotEmpty(LQueue Q)
{
    
    
    if(Q.front==NULL)return 0;
    else return 1;
}
void QueueAppend(LQueue *Q,DataType x)
{
    
    
    LQNode *p;
    p=(LQNode *)malloc(sizeof(LQNode));//申请结点空间
    p->data=x;
    p->next=NULL;
    //rear->a(n-1),为最后一个元素
    if(Q->rear!=NULL)Q->rear->next=p;//队列非空时,将结点p插入在队尾
    Q->rear=p;//修改队尾指针
    if(Q->front==NULL)Q->front=p;//队列为空时,修改队头指针
}
int QueueDelete(LQueue *Q,DataType *x)
{
    
    
    LQNode *p;
    if(Q->front==NULL)
    {
    
    
        printf("队列已空,无可删元素!\n");
        return 0;
    }
    else
    {
    
    
        *x=Q->front->data;//取队头元素数据
        p=Q->front;//记录队头指针
        Q->front=Q->front->next;//出队列,结点脱链
        if(Q->front==NULL)Q->rear=NULL;//队列中只有一个元素的情况
        free(p);
        return 1;
    }
}
int QueeuGet(LQueue *Q,DataType *x)
{
    
    
    if(Q->front==NULL)
    {
    
    
        printf("队列已空,无元素可取!\n");
        return 0;
    }
    else
    {
    
    
        *x=Q->front->data;
        return 1;
    }
}
void DestoryList(LQueue *Q)
{
    
    
    LQNode *p,*p1;//释放空间时必须要两个指针
    p=Q->front;
    while(p->next!=NULL)
    {
    
    
        p1=p;
        p=p->next;
        free(p1);
    }
}

Master file

#include <stdio.h>
#include <stdlib.h>
typedef char DataType;
#include"Stack.h"
#include"LQueue.h"
#include<string.h>
void Enterstr(char str[])
{
    
    
    printf("请输入字符串:\n");
    scanf("%s",str);
}

void Palindorme(char str[])
{
    
    
    int len,i,j;
    char p,q;
    len=strlen(str);
    LQueue Q;
    LSNode *S;
    QueueInitiate(&Q);
    StackInitiate(&S);
    for(i=0;i<len;i++)
    {
    
    
        StackPush(S,str[i]);
        QueueAppend(&Q,str[i]);
    }
    j=0;
    while(StackNotEmpty(S)==1&&QueueNotEmpty(Q)==1)
       {
    
    
        StackPop(S,&p);
        QueueDelete(&Q,&q);
        if(p==q)j++;
       }
       if(j==len)
        printf("此字符串是回文!\n");
       else
       printf("此字符串不是回文!!\n");

}

int main()
{
    
    
    void Enterstr(char str[]);
    void Palindorme(char str[]);
    char s1[100],s;
    while(1)
    {
    
    
    Enterstr(s1);
    Palindorme(s1);
    printf("输入N退出!\n");
    scanf("%s",&s);
    if(s=='N')break;
    }
    return 0;

}

Experimental results

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_41982200/article/details/109250894