数据结构----栈和队列的综合应用

来源:互联网 发布:安卓去广告软件 编辑:程序博客网 时间:2024/05/23 11:52

一.实验目的

熟悉掌握数据结构中队列的基本操作,能够结合栈与队列的结构灵活解决一些实际中问题。

二.实验题目

在许多语言现象中,常见到一种形如abcba的文字,这种文字从左到右读和从右到左读结果是一样的,这种文字就是常说的回文。设计一个程序可以判断给定的一个文字是否是回文。

考虑到栈的先进后出以及队列的后进先出,可以结合这两种结构来实现需要的功能,即将文字分别入队和入栈,然后依次输出判断是否有不相同的字符,一旦发现就证明文字不是一个回文。

三.实现提示

1.队列的主要数据结构定义如下:

[cpp] view plain copy
  1. typedef struct QNode{  
  2. char  data;  
  3. struct QNode *next;  
  4. }QNode,*Queueptr;  
  5. typedef struct{  
  6. Queueptr front;  
  7. Queueptr rear;  
  8. }linkQueue;  


   2.程序的主框架如下:

[cpp] view plain copy
  1. scanf("%c",&c);  
  2. while(c!='#') {push(&s,c); EnQueue(&Q,c);       scanf("%c",&c);  }  
  3. while(!(s.top==s.base))  
  4. {  pop(&s,&a); DeQueue(&Q,&b);  
  5. if(a!=b) {printf("This isn't acycle\n");exit(0);   }  
  6. }  
  7. printf("This is acycle\n");  return OK;  


四 .  实现

[cpp] view plain copy
  1. #include<stdio.h>  
  2. #include<stdlib.h>  
  3.   
  4. /**********************数据结构定义**********************/  
  5. /* 
  6.   队列和栈的数据结构的定义  
  7. */  
  8. typedef struct queue  
  9. {    //队列的结构体定义  
  10.     int data;  
  11.     struct queue *next;  
  12. }queue,*que;  
  13.   
  14. typedef struct   
  15. {  
  16.      que front;   //队列的头指针  
  17.      que rear;    //队列的尾指针  
  18. }Q;  
  19.   
  20.   
  21. typedef struct    
  22. {  //栈结构的定义  
  23.     int *top;  
  24.     int *base;  
  25.     int stacksize;  
  26. }sqstack;  
  27.   
  28.   
  29. /**********************功能函数**********************/  
  30. /* 
  31.   初始化一个空栈。50个空间。  
  32. */  
  33. void initStack(sqstack &s)  
  34. {  
  35.      s.base=(int *)malloc(50*sizeof(sqstack));  
  36.      if(!s.base)  
  37.          printf("存储空间分配失败!\n");  
  38.      s.top=s.base;  
  39.      s.stacksize=50;  
  40. }  
  41.   
  42. /* 
  43.   判断栈是否为空,返回bool类型 
  44. */  
  45. bool isEmpty(sqstack &s)  
  46. {  
  47.      bool result=true;  
  48.      if(s.top==s.base)  
  49.         result=false;  
  50.      else  
  51.         result=true;  
  52.      return result;  
  53. }  
  54.   
  55. /* 
  56.   入栈 
  57. */  
  58. void push(sqstack &s,int e)  
  59. {  
  60.      if(s.top-s.base>=s.stacksize)//如果栈满, 则增加新空间   
  61.      {  
  62.            s.base=(int *)realloc(s.base,(s.stacksize+10)*sizeof(sqstack));  
  63.            s.top=s.base+s.stacksize; //将top指向原来的头   
  64.            s.stacksize+=10;  
  65.      }  
  66.      *s.top++=e;  
  67. }  
  68.   
  69. /* 
  70.   出栈,返回char类型 
  71. */  
  72. char pop(sqstack &s,int &e)  
  73. {  
  74.     if(s.top==s.base)  
  75.         printf("栈是空的!");   
  76.     e=*--s.top;  
  77.     return e;  
  78. }  
  79.   
  80. /* 
  81.   初始化一个空队列 
  82. */  
  83. void initQ(Q &q)  
  84. {  
  85.      q.front=q.rear=(queue *)malloc(sizeof(queue));  
  86.      if(!q.front)  
  87.         printf("存储空间分配失败!");  
  88.      q.front->next=NULL;  
  89. }  
  90.   
  91. /* 
  92.   入队 
  93. */  
  94. void EnQueue(Q &q,int e)  
  95. {  
  96.     queue *p=(queue *)malloc(sizeof(queue));  
  97.     p->data=e;  
  98.     p->next=NULL;  
  99.       
  100.     q.rear->next=p; //链接上   
  101.     q.rear=p;       //尾指针指向   
  102. }  
  103.   
  104. /* 
  105.   出队,返回char类型. 
  106. */  
  107. char DeQueue(Q &q,int &e)  
  108. {  
  109.      queue *p=(queue *)malloc(sizeof(queue));  
  110.      if(q.front==q.rear)  
  111.         printf("队列为空!");  
  112.      p = q.front->next;  //将不需要删除的移解到t  
  113.      e = p->data;          
  114.      q.front->next=p->next;//头指针后移  
  115.      if(q.rear==p)//如果尾指针指向t,那么就将尾指针指向头指针( 因为t要删除)  
  116.         q.rear = q.front;  
  117.      free(p);  
  118.      return e;  
  119.        
  120. }  
  121.   
  122. /**********************主函数**********************/  
  123. int main()  
  124. {  
  125.     char c;  
  126.     int e,k;  
  127.     bool ss=true;   
  128.     sqstack s;  
  129.     Q q;  
  130.       
  131.       
  132.     initStack(s);  
  133.     initQ(q);  
  134.     printf("请输入字符,以 # 结束:\n");  
  135.     scanf("%c",&c);  
  136.     while(c!='#')   
  137.     {  
  138.      push(s,c);     //压入栈   
  139.      EnQueue(q,c);   //进队   
  140.      scanf("%c",&c);    
  141.     }  
  142.     while(!(s.top==s.base))//当栈不为空时   
  143.     {     
  144.         pop(s,e);  
  145.         DeQueue(q,k);  
  146.         if(e!=k)   
  147.         {  
  148.          printf("This isn't a cycle!\n");  
  149.          exit(0);  
  150.         }  
  151.     }  
  152.     printf("This is a cycle!\n");    
  153.     system("PAUSE");  
  154.     return 0;  
  155. }