热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

数据结构栈和队列的一些应用

#include<stdio.h>#include<stdlib.h>#include<math.h>#includeconio.h
#include
#include
#include
#include "conio.h"
#define MAX 50
typedef struct Node
{
int data;
struct Node * next;
}linkqueuenode;
typedef struct 
{
linkqueuenode *front;
linkqueuenode *rear;
}linkqueue;


typedef struct 
{
int a[MAX];
int front;
int rear;
}setqueue;


typedef struct DNode
{
int data;
struct DNode *prior,*next;
}DLNode,*Doublelist;
typedef struct Polynode
{
int coef;
int exp;
Polynode *next;
}Polynode, * Polylist;
typedef struct node
{
char ch;
struct node *next;
}LinkStackNode;
typedef LinkStackNode *LinkStack;
void delay(int n) 

int i,j; 
for(j=0;jfor(i=0;i<125;i++){;}
}


int intlinkqueue(linkqueue *Q)
{
Q->frOnt=(linkqueuenode *)malloc(sizeof(linkqueuenode));
if(Q->front!=NULL)
{
Q->rear=Q->front;
Q->front->next=NULL;
return true;
}
else
return false;
}
int enterlinkqueue(linkqueue *Q,int x)
{
   linkqueuenode *p;
   p=(linkqueuenode *)malloc(sizeof(linkqueuenode));
   if(p!=NULL)
   {
  p->data=x;
  //p=Q->rear->next;
  p->next=NULL;
  Q->rear->next=p; // ??? Q->rear->NEXT 指向p
  Q->rear=p;
   }
   return true;
}
int deletelinkqueue(linkqueue *Q,int * x)
{
   linkqueuenode *p;
   p=(linkqueuenode *)malloc(sizeof(linkqueuenode));
   if(p!=NULL)
   {
 *x=Q->front->next->data;  //注意Q->front->next->data 才是队列的第一个值;
  p=Q->front->next;
  Q->front->next=p->next;
  if(Q->rear==p)
  Q->frOnt=Q->rear;
  //  *x=p->data;
  free(p);
  return true;
   }
}
int getlinkhead(linkqueue *Q,int * x)
{
   *x=Q->front->next->data;
   return true;
}
void intqueue(setqueue *Q)
{
Q->frOnt=Q->rear=0;
}
int queueisempty(setqueue *Q)
{
if(Q->frOnt==Q->rear)
return false;
else
return true;
}
int enterqueue(setqueue *Q,int x)
{
if((Q->rear+1)%MAX==Q->front)
return false;
Q->a[Q->rear]=x;
Q->rear=(Q->rear+1)%MAX;
return true;
}
int gethead(setqueue *Q,int * x)
{
*x=Q->a[Q->front];
return true;
}
int deletequeue(setqueue *Q,int * x)
{
if(Q->frOnt==Q->rear)
return false;
*x=Q->a[Q->front];
     Q->frOnt=(Q->front+1)%MAX;
return true;

}
void yanghui()
{
setqueue Q;
intqueue(&Q);
int temp,x;
enterqueue(&Q,1);
for(int n=2;n<=5;n++)
{
enterqueue(&Q,1);
for(int i=1;i<=n-2;i++)
{
deletequeue(&Q,&temp);
printf("%d ",temp);
gethead(&Q,&x);
temp+=x;
enterqueue(&Q,temp);
}
deletequeue(&Q,&x);
printf("%d\n",x);
enterqueue(&Q,1);
}


};
void linkyunsuan()
{
linkqueue Q;

int temp,x,a;
a=intlinkqueue(&Q);
// printf("%d\n",a);
enterlinkqueue(&Q,1);
for(int n=2;n<=5;n++)
{
       enterlinkqueue(&Q,1);
  for(int i=1;i<=n-2;i++)
  {
  deletelinkqueue(&Q,&temp);
  printf("%d ",temp);
  getlinkhead(&Q,&x);
  temp+=x;
  enterlinkqueue(&Q,temp);
  }
  deletelinkqueue(&Q,&x);
  printf("%d\n",x);
  enterlinkqueue(&Q,1);


}
}
void IntStack(LinkStack top)
{
top=(LinkStackNode *)malloc(sizeof(LinkStackNode));
top->next=NULL;
}
int push(LinkStack top,char c)
{
LinkStackNode *temp;
   temp=(LinkStackNode *)malloc(sizeof(LinkStackNode));
   temp->ch=c;
   temp->next=top->next;
   top->next=temp;
  //free(temp); 不能用free 应为temp还在链栈之中,free之后top->next就找不到下个目标了
return true;
}
int pop(LinkStack top,char *ch)
{
   LinkStackNode *temp;
    temp=(LinkStackNode *)malloc(sizeof(LinkStackNode));
temp=top->next;
top->next=temp->next;
*ch=temp->ch;
free(temp);
return true;
}
int getpop(LinkStack top)
{
   LinkStackNode *temp;
   char ch;
    temp=(LinkStackNode *)malloc(sizeof(LinkStackNode));
temp=top->next;
// top->next=temp->next;
ch=temp->ch;
//free(temp);
return ch;
}
int Isempty(LinkStack top)
{
if(top->next)
return true;
else
return false;
}
int Match(char *str)
{
//LinkStack top;
LinkStackNode top;//可以用上面那个,上面那个是地址,需要下面那句给指针初始化,下面的是链栈,不需要初始化
int i;
char ch,c;
  //top=(LinkStackNode *)malloc(sizeof(LinkStackNode));
for(i=0;str[i]!='\0';i++)
{
  switch(str[i])
  {
  case '[':
  case '{':push(&top,str[i]);//如果用地址方式就是上面注释的方法,需要将&top改成top,要不是会报错
      break;
  case ']':
  case '}':ch=getpop(&top);
// printf("%c\n",c);
 if((ch+2)==str[i])//asii码+2;
  pop(&top,&c);
 else 
 return false;
 break;
  }
  
}


return true;
}
int yunsuan()
{
LinkStackNode OVS,OPTR;
char ch,c,o,a,b;
char v;
push(&OPTR,'=');


       ch=getchar();
  while(ch!='='||getpop(&OPTR)!='=')
  {
          if(ch!='*'&&ch!='/'&&ch!='+'&&ch!='-'&&ch!='=')//需要让ch不等于#
 {
 push(&OVS,ch-48);//可能需要转换成int格式
 ch=getchar();
 }
 else
 {  if(getpop(&OPTR)=='=')
  c='<';
else if((getpop(&OPTR)=='+'||getpop(&OPTR)=='-')&&(ch=='*'||ch=='/'))
 c='<';
 
 else
 c='>';
 switch(c)
 {case '<':push(&OPTR,ch);
          ch=getchar();
  break;
 case '>':pop(&OPTR,&o);


     pop(&OVS,&a);
 pop(&OVS,&b);
 if(o=='+')
 v=a+b;
 else if(o=='-')
 v=a-b;
 else if(o=='*')
 v=a*b;
 else 
 v=a/b;
 push(&OVS,v);
 break;


      
              }
 }
  }
  v=getpop(&OVS);
  return v;
 


}
int huiwen(char *str)
{
LinkStackNode top;
char ch,ch1;
int i=0;
int j;
while(str[i]!='&')
{
push(&top,str[i]);
i++;
}
//  printf("%d\n",sizeof(&top));
 // sizeof(&top)--;
  j=sizeof(&top)-1;
while(j--)
{  pop(&top,&ch);
i++;
if(ch!=str[i])
return false;

}
return true;
}
Polynode *create()
{
Polynode *p,*h,*m;
int c,e;
h=(Polynode *)malloc(sizeof(Polynode));
p=h;
scanf("%d,%d",&c,&e);


while(c)
{
      m=(Polynode *)malloc(sizeof(Polynode));
 m->coef=c;
 m->exp=e;
 p->next=m;
 p=m;
 scanf("%d,%d",&c,&e);
}
p->next=NULL;
return h;
}
void zhanshi(Polynode *h)
{
   Polynode *p;
   p=h;
   p=p->next;
   while(p)
   {
  printf("%dX%d+",p->coef,p->exp);
  p=p->next;
   }
}
Polynode *hebing(Polynode *a,Polynode *b)
{
Polynode *p,*q,*h,*m;
int sum;
m=(Polynode *)malloc(sizeof(Polynode));
p=a;
p=p->next;
q=b;
q=q->next;
m=a;
    while(p!=NULL&&q!=NULL)
{
if(p->expexp)
{   
m->next=p;
m=p;
p=p->next;
}
else if(p->exp==q->exp)
{
sum=p->coef+q->coef;
p->coef=sum;
m->next=p;
m=p;
p=p->next;
q=q->next;
}
else
{
m->next=q;
m=q;
q=q->next;
}
if(p!=NULL)
m->next=p;
else
            m->next=q;
}
   return a;
}
DLNode *chuangjian(int i,int len)
{
  DLNode *n,*h,*p;
  h=(DLNode *)malloc(sizeof(DLNode));
  h->prior=h;
  h->next=h;
  p=h;
  while(len--)
  {
 n=(DLNode *)malloc(sizeof(DLNode));
 n->data=i;
 p->next=n;
 n->prior=p;


 p=n;
 i++;


  }
   n->next=h;
 h->prior=n;
  return(h);
};
void xianshi(DLNode *h)
{
DLNode *p;
p=h;
    p=p->next;
while(p!=h)
{
printf("%d",p->data);
p=p->next;
// p->next=p;  等号左边会执行,我们希望的就是让他一个一个排下去,所以左边是p->next。
}


};
DLNode * charu(DLNode *h,int i,int a)
{
DLNode *s,*p,*l,*m;
s=h;
m=h;
p=(DLNode *)malloc(sizeof(DLNode));
l=(DLNode *)malloc(sizeof(DLNode));
while(i--)
s=s->next;




p->data=a;
    p->next=s->next->next;
p->prior=s->prior;
    p->prior->next=s;
s->next=p;
p->prior=s;


    return h;
}
DLNode *shanchu(DLNode *h,int i,int a)
{
DLNode *s,*p;
s=h;
p=(DLNode *)malloc(sizeof(DLNode));
while(i--)
{
s=s->next;
}
//p->prior=s->prior;
s->next=s->next->next;
    
return h;
}


void main()
{
DLNode *p,*h,*m;
Polynode *n,*l,*o;
LinkStackNode top;
char s[5]="{[]}";
char b[10]="12+3&3+21";
char ch1,ch2;
int i,v,f,a;
setqueue Q;
p=(DLNode *)malloc(sizeof(DLNode));
p=chuangjian(1,10);
xianshi(p);
printf("\n");
h=charu(p,6,11);
xianshi(h);
printf("\n");
m=shanchu(h,5,10);
xianshi(m);
printf("\n");//以上为循环链表的操作
// n=create();
// zhanshi(n);
 //   printf("\n");
// l=create();
// zhanshi(n);
 //   printf("\n");
// o=hebing(l,n);
// zhanshi(o);//以上为单链表表示多项式
 // i=Match(s);
 // printf("%d\n",i);//以上为用链栈处理括号匹配问题
//  v=yunsuan();
// printf("%d\n",v); //以上为链栈处理一元表达式
yanghui();// 杨辉三角 循环队列
linkyunsuan();// 杨辉三角   链队列
    intqueue(&Q);
for(;;)
{
for(;;)
{
printf("A ");
delay(40);
if(_kbhit())
{
ch1=getch();
if(ch1==';'||ch1==',') break;
f=enterqueue(&Q,int(ch1));

}
}


while(queueisempty(&Q))
{  int(ch2);
deletequeue(&Q,&ch2);
putchar(ch2);

}
delay(80);
if(ch1==';') break;

}//以上,队列键盘输入循环缓存;
a=huiwen(b);
printf("%d",a);//以上 链栈测试回文数


}

推荐阅读
  • Ihaveaworkfolderdirectory.我有一个工作文件夹目录。holderDir.glob(*)>holder[ProjectOne, ... [详细]
  • PHP图片截取方法及应用实例
    本文介绍了使用PHP动态切割JPEG图片的方法,并提供了应用实例,包括截取视频图、提取文章内容中的图片地址、裁切图片等问题。详细介绍了相关的PHP函数和参数的使用,以及图片切割的具体步骤。同时,还提供了一些注意事项和优化建议。通过本文的学习,读者可以掌握PHP图片截取的技巧,实现自己的需求。 ... [详细]
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
  • eclipse学习(第三章:ssh中的Hibernate)——11.Hibernate的缓存(2级缓存,get和load)
    本文介绍了eclipse学习中的第三章内容,主要讲解了ssh中的Hibernate的缓存,包括2级缓存和get方法、load方法的区别。文章还涉及了项目实践和相关知识点的讲解。 ... [详细]
  • Python正则表达式学习记录及常用方法
    本文记录了学习Python正则表达式的过程,介绍了re模块的常用方法re.search,并解释了rawstring的作用。正则表达式是一种方便检查字符串匹配模式的工具,通过本文的学习可以掌握Python中使用正则表达式的基本方法。 ... [详细]
  • 本文讨论了如何使用IF函数从基于有限输入列表的有限输出列表中获取输出,并提出了是否有更快/更有效的执行代码的方法。作者希望了解是否有办法缩短代码,并从自我开发的角度来看是否有更好的方法。提供的代码可以按原样工作,但作者想知道是否有更好的方法来执行这样的任务。 ... [详细]
  • 本文讨论了编写可保护的代码的重要性,包括提高代码的可读性、可调试性和直观性。同时介绍了优化代码的方法,如代码格式化、解释函数和提炼函数等。还提到了一些常见的坏代码味道,如不规范的命名、重复代码、过长的函数和参数列表等。最后,介绍了如何处理数据泥团和进行函数重构,以提高代码质量和可维护性。 ... [详细]
  • Android自定义控件绘图篇之Paint函数大汇总
    本文介绍了Android自定义控件绘图篇中的Paint函数大汇总,包括重置画笔、设置颜色、设置透明度、设置样式、设置宽度、设置抗锯齿等功能。通过学习这些函数,可以更好地掌握Paint的用法。 ... [详细]
  • 上图是InnoDB存储引擎的结构。1、缓冲池InnoDB存储引擎是基于磁盘存储的,并将其中的记录按照页的方式进行管理。因此可以看作是基于磁盘的数据库系统。在数据库系统中,由于CPU速度 ... [详细]
  • 本文介绍了在Android开发中使用软引用和弱引用的应用。如果一个对象只具有软引用,那么只有在内存不够的情况下才会被回收,可以用来实现内存敏感的高速缓存;而如果一个对象只具有弱引用,不管内存是否足够,都会被垃圾回收器回收。软引用和弱引用还可以与引用队列联合使用,当被引用的对象被回收时,会将引用加入到关联的引用队列中。软引用和弱引用的根本区别在于生命周期的长短,弱引用的对象可能随时被回收,而软引用的对象只有在内存不够时才会被回收。 ... [详细]
  • 深入解析Linux下的I/O多路转接epoll技术
    本文深入解析了Linux下的I/O多路转接epoll技术,介绍了select和poll函数的问题,以及epoll函数的设计和优点。同时讲解了epoll函数的使用方法,包括epoll_create和epoll_ctl两个系统调用。 ... [详细]
  • linux进阶50——无锁CAS
    1.概念比较并交换(compareandswap,CAS),是原⼦操作的⼀种,可⽤于在多线程编程中实现不被打断的数据交换操作࿰ ... [详细]
  • Commit1ced2a7433ea8937a1b260ea65d708f32ca7c95eintroduceda+Clonetraitboundtom ... [详细]
  • 开发笔记:加密&json&StringIO模块&BytesIO模块
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了加密&json&StringIO模块&BytesIO模块相关的知识,希望对你有一定的参考价值。一、加密加密 ... [详细]
  • [翻译]PyCairo指南裁剪和masking
    裁剪和masking在PyCairo指南的这个部分,我么将讨论裁剪和masking操作。裁剪裁剪就是将图形的绘制限定在一定的区域内。这样做有一些效率的因素࿰ ... [详细]
author-avatar
mobiledu2502877493
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有