1. 程式人生 > >用資料結構的棧和佇列 寫 迴文判斷

用資料結構的棧和佇列 寫 迴文判斷

假設稱正讀和反讀都相同的字元序列為“迴文”,例如,‘abba’和‘abcba’是迴文,‘abcde’和‘ababab’則不是迴文。試寫一個演算法判別讀入的一個以‘@’為結束符的字元序列是否是“迴文”。程式設計實現該程式。


#include <stdio.h>
#include <stdlib.h>

typedef struct node{
 char data;
 struct node *next;
}node;

typedef struct {
 node *top;
 unsigned int size;
} stack;

typedef struct{
 node *front;
 node *back;
 unsigned int size;
} queue;

char pops(stack *a)
{
 node *tf=NULL;
 char rt=0;
 if (a->size) {
  tf=a->top;
  a->top=a->top->next;
  --a->size;
  rt=tf->data;
  free(tf);
 }

 return rt;
}

char popq(queue *a)
{
 node *tf=NULL;
 char rt=0;
 if (a->size) {
  tf=a->front;
  a->front=a->front->next;
  --a->size;
  rt=tf->data;
  free(tf);
 }
 return rt;
}

void push(stack *a,const char c)
{
 node *t=(node*)malloc(sizeof(node));
 if (t) {
  t->data=c;
  t->next=a->top;
  a->top=t;
  ++a->size;
 }
}
void push_back(queue *a,const char c)
{
 node *t=(node*)malloc(sizeof(node));
 if (t) {
  t->data=c;
  t->next=NULL;
  if (!a->size) {
   a->front=a->back=t;
  }
  else
  {
   a->back->next=t;
   a->back=t;
  }

  ++a->size;
 }
}

int isempty(void *a,int tp)
{
 if (tp==1) return !(((stack *)a)->size);
 else return !(((queue *)a)->size);
}
void initqs(void *a,int tp)
{
 if (tp==1) {
  ((stack*)a)->top=NULL;
  ((stack*)a)->size=0;
 }
 else{
  ((queue*)a)->front=((queue*)a)->back=NULL;
  ((queue*)a)->size=0;
 }
}

void del(void *a,int tp)
{
 node *t;
 if (tp==1) {
   while (((stack*)a)->top){
    t= ((stack*)a)->top;
    ((stack*)a)->top=((stack*)a)->top->next;
    free(t);
   }
   free(a);
 }
 else {
   while (((queue*)a)->front){
    t= ((queue*)a)->front;
    ((queue*)a)->front=((queue*)a)->front->next;
    free(t);
   }
   free(a);
 }
}
int chk(void)
{
 char ch;
 int fg=1,rt=0;

 stack *a=(stack*)malloc(sizeof(stack));
 queue *b=(queue*)malloc(sizeof(queue));
 if (!a||!b) {
  fprintf(stderr,"MEMORY ERROR");
  exit(-1);
 }
 initqs(a,1);
 initqs(b,0);

 puts("Enter a string ,end with @");
 while ((ch=getchar())!='@')
 {
  push(a,ch);
  push_back(b,ch);
 }

 while (!isempty(a,1)&&!isempty(b,0))
  {
   if (pops(a)!=popq(b)) {
    fg=0;
    break;
   }
  }
 if (fg&&isempty(a,1)&&isempty(b,0)) rt= 1;
 del(a,1);
 del(b,0);
 return rt;

}
int main(void)
{
 if (chk()) puts("YES");
 else puts("NO");

 return 0;
}
//---------------------------------------------------------------------------
用棧實現了判斷迴文數的操作,即把字串依次入棧,然後出棧並依次和字元陣列比較是否相等,從而判斷字元序列是否迴文數,程式碼如下:


#include "stdio.h"
#include "stdlib.h"
#include "string.h"

#define EMPTY 0
#define FULL 10000
#define MAX 10000

typedef char data;

typedef struct elem {
 data d;
 struct elem *next;
}elem;

typedef struct stack {
 int cnt;
 elem *top;
}stack;

void initialize(stack *stk);
void push(data d, stack *stk);
data pop(stack *stk);
bool empty(const stack *stk);
bool full(const stack *stk); //棧操作函式

void initialize(stack *stk)
{
 stk->cnt = 0;
 stk->top = NULL;
}

bool empty(const stack *stk)
{
 return stk->cnt == EMPTY;
}

bool full(const stack *stk)
{
 return stk->cnt == FULL;
}

void push(data d, stack *stk)
{
 elem *p;
 
 if (!full(stk))
 {
  p = (elem *)malloc(sizeof(elem));
  p->d = d;
  p->next = stk->top;
  stk->top = p;
  stk->cnt++;
 }
 
}

data pop(stack *stk)
{
 data d;
 elem *p;

 if(!empty(stk))
 {
  d = stk->top->d;
  p = stk->top;
  stk->top = stk->top->next;
  stk->cnt--;
  free(p);
 }
 
 return d;
}

int main(void)
{
 data input[MAX];
 stack temp;
 int i = 0;
 int flag = 0;

 initialize(&temp); //初始化臨時棧
 scanf("%s", &input); //輸入字串

 while (input[i] != '@')
 {//字串入棧
  push(input[i], &temp);
  i++;
 }

 while (!empty(&temp))
 {//字元依次出棧和字元陣列比較,判斷是否迴文數
  if (temp.top->d == input[flag])
  {
   pop(&temp);
   flag++;
  }
  else
  {
   printf("此字元序列不是迴文數!/n");
   break;
  }
 }
 
 if (empty(&temp))
  printf("此字元序列是迴文數!/n");

 return 1;
}