数据结构与算法整理6——队列与栈的相互操作&栈的回文(C语言)

数据结构与算法整理6——队列与栈的相互操作&栈的回文(C语言)

 

1、队列与栈的相互操作

/* 顺序栈表示:函数定义 */

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


enum { MAXNUM = 20    /* 栈中最大元素个数,应根据需要定义 */
};
     
typedef int DataType; /* 栈中元素类型,应根据需要定义 */

struct SeqStack {	  /* 顺序栈类型定义 */
    int  t; 		  /* 栈顶位置指示 */
    DataType  s[MAXNUM];
};

typedef  struct SeqStack  *PSeqStack;	/* 顺序栈类型的指针类型 */ 


struct  SeqQueue {	/* 顺序队列类型定义 */
    int f, r;
    DataType q[MAXNUM];
};

typedef struct SeqQueue *PSeqQueue;	/* 顺序队列类型的指针类型 */


/*创建一个空栈;为栈结构申请空间,并将栈顶变量赋值为-1*/
PSeqStack  createEmptyStack_seq( void ) {  
    PSeqStack pastack = (PSeqStack)malloc(sizeof(struct SeqStack));
    if (pastack==NULL)
        printf("Out of space!! \n");
    else
        pastack->t = -1;
    return pastack;
}

/*判断pastack所指的栈是否为空栈,当pastack所指的栈为空栈时,则返回1,否则返回0*/
int  isEmptyStack_seq( PSeqStack pastack ) {
    return pastack->t == -1;
}

/* 在栈中压入一元素x */
void  push_seq( PSeqStack pastack, DataType x ) {  
    if( pastack->t >= MAXNUM - 1  )
        printf( "Stack Overflow! \n" );
    else {  
        pastack->t++;
        pastack->s[pastack->t] = x;
    }
}

/* 删除栈顶元素 */
void  pop_seq( PSeqStack pastack ) {  	
    if (pastack->t == -1 )
        printf( "Underflow!\n" );
    else
        pastack->t--;
}

/* 当pastack所指的栈不为空栈时,求栈顶元素的值 */
DataType top_seq( PSeqStack pastack ) {
    return pastack->s[pastack->t];
}



/*创建一个空队列*/
PSeqQueue  createEmptyQueue_seq( void ) {  
    PSeqQueue paqu = (PSeqQueue)malloc(sizeof(struct SeqQueue));
    if (paqu==NULL)
        printf("Out of space!! \n");
    else
        paqu->f = paqu->r = 0;
    return paqu;
}

/*判队列是否为空队列*/
int  isEmptyQueue_seq( PSeqQueue paqu ) {
    return paqu->f == paqu->r;
}

/* 在队列中插入一元素x */
void  enQueue_seq( PSeqQueue paqu, DataType x ) {
    if( (paqu->r + 1) % MAXNUM == paqu->f  )
        printf( "Full queue.\n" );
    else 	{
        paqu->q[paqu->r] = x;
        paqu->r = (paqu->r + 1) % MAXNUM;
    }
}

/* 删除队列头部元素 */
void  deQueue_seq( PSeqQueue paqu ) {
    if( paqu->f == paqu->r )
        printf( "Empty Queue.\n" );
    else
        paqu->f = (paqu->f + 1) % MAXNUM;
}

/* 对非空队列,求队列头部元素 */
DataType  frontQueue_seq( PSeqQueue paqu ) {
    return paqu->q[paqu->f];
}

//实现从一个栈创建一个队列
main()

{   PSeqStack S;
	PSeqQueue Q=createEmptyQueue_seq();
	int i;
	for( i=0;i<5;i++)
	{enQueue_seq(Q, i+1);
	}
	
  //输入元素值,使之入队
   S= createEmptyStack_seq();
   int n,j;
   while(isEmptyQueue_seq(Q)==0)
   { 
	   n=frontQueue_seq(Q); 
	   push_seq( S, n );
	   deQueue_seq(Q);
  // 取队头,将其入栈
  // 出队
   }

  while(isEmptyStack_seq(S)==0)
  {
	 j=top_seq( S);
	j=top_seq( S);
	 pop_seq(S);
   //取栈顶,将其入队
   //出栈
  }
printf("队列中现在的元素为:");
	int i1;
  	for(i1=0;i1<5;i1++)
	{
		int m;
		m=frontQueue_seq(Q);
		printf("%d",m);
		deQueue_seq(Q);
	}
	printf("\n");
}

//实现从一个队列创建一个栈 
//main()
//
//{   PSeqStack S;
//    S= createEmptyStack_seq();
//	PSeqQueue Q=createEmptyQueue_seq();
//	int i;
//	for( i=0;i<5;i++)
//	{push_seq(S, i+1);
//	}
//  //输入元素值,使之入栈
//   
//   int n,j;
//   while(isEmptyStack_seq(S)==0)
//   { 
//	   n=top_seq(S); 
//	  enQueue_seq(Q,n);
//	    pop_seq(S);
//  // 取栈顶,将其入队
//  // 出栈
//   }
//
//  while(isEmptyQueue_seq(Q)==0)
//  {
//	 j=frontQueue_seq(Q);
//	 push_seq(S,j);
//	 deQueue_seq(Q);
//   //取队头,将其入栈
//   //出队
//  }
//	int i1;
//	printf("栈中现在的元素为:");
//  	for(i1=0;i1<5;i1++)
//	{
//		int m;
//		m=top_seq(S);
//		printf("%d",m);
//		pop_seq(S);
//	}
//	printf("\n");
//}

 

2、栈的回文

  • 什么是回文字符串,一句话概括就是关于中心左右对称的字符串。例如:ABCBA或者AACCAA是回文字符串;ABCCA或者AABBCC不是回文字符串。

  • 判断方法就是,依次看两端的字符是否相等。例如:ABCBA,第一个字符与最后一个字符相等,第二个字符与倒数第二个字符相等,第三个字符是中心字符,无需判断。如果是AACCAA就要判断。所以上面的字符串是回文字符串。

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

#define maxsize 20 
typedef struct SeqStack {	  /* 顺序栈类型定义 */
    int  t; 		  /* 栈顶位置指示 */
    char s[maxsize];
}*PSeqStack;

//创建一个空栈
PSeqStack  createastack() {  
    PSeqStack S = (PSeqStack)malloc(sizeof(struct SeqStack));
    if (S==NULL)
        printf("Out of space!! \n");   //如果没有分配到空间则返回溢出错误 
    else
        S->t = -1;              //把指向栈顶的top指针设为-1 
    return S;
}

//栈是否为空栈
int ifemptystack(PSeqStack S) {
	if( S->t == -1)
		return 1;
	else
		return 0;
}

//在栈中压入元素 
void pushstack(PSeqStack S,char n){
	if( S->t >= maxsize-1  )
        printf( "Stack Overflow! \n" );
    else
    S->t++;
    S->s[S->t]=n;
} 

//取出栈顶
char gettop(PSeqStack S){
	return S->s[S->t];	
} 
//删除栈顶元素
void deletetop(PSeqStack S){
	if (S->t == -1 )
        printf( "Underflow!\n" );
    else
    S->t--;
}
 
void main(){
	PSeqStack S;
	S=createastack();
	int flag=1;   
	char n,b;   
	int i=0;        //设置一个标签表示是否为回文 
	char *c=NULL;
	c= (char *)malloc(30 * sizeof(char));
	printf("请输入要判断的字符,以@结束(最多30个字符):");
	gets(c);
	printf("%s\n",c);
	while (c[i]!=0){
		n=c[i];
		pushstack(S,n);
		i++;
	} 
	i=0;
	while(ifemptystack(S)==0){
		b=gettop(S);
		deletetop(S);
		if(b!=c[i]){ 
			flag=0;
			break;
		} 
		i++; 
	}
	if(flag==0)
	printf("您输入的字符串不是一段回文!") ;	
	else
	printf("您输入的字符串是一段回文!") ;
	
}
 

 

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值