数据结构 栈和队列的一些应用
2014-12-30 22:16
369 查看
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#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;j<n*1000;j++)
for(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
4000
*)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->exp<q->exp)
{
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);//以上 链栈测试回文数
}
#include <stdlib.h>
#include <math.h>
#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;j<n*1000;j++)
for(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
4000
*)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->exp<q->exp)
{
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);//以上 链栈测试回文数
}
相关文章推荐
- C#数据结构和算法学习系列七----队列、队列的实现和应用
- 数据结构: 优先队列的应用
- 数据结构复习_栈和队列,应用_括号匹配&文件目录递归拷贝,
- 第六周--数据结构--队列的应用之报数问题
- 并发队列ConcurrentLinkedQueue和阻塞队列LinkedBlockingQueue用法 在Java多线程应用中,队列的使用率很高,多数生产消费模型的首选数据结构就是队列(先进先出)。
- 数据结构之队列应用
- C语言数据结构之栈与队列的应用(2)
- 【数据结构】队列的典型应用
- 数据结构_队列应用举例_划分子集问题
- 第六周--数据结构--队列的应用之后缀表达式(栈)
- 关于STL优先级队列的一些应用
- 数据结构----栈和队列的综合应用
- 【学习点滴-数据结构-栈&队列】 链式队列的实现及应用
- 关于GRAPH数据结构在经济管理学科中的应用的一些感慨
- 【数据结构】 链式队列的一些操作
- 数据结构学习笔记 --- 队列的应用举例(离散事件模拟)
- 【学习点滴-数据结构-栈&队列】 栈的应用--递归的实现-汉诺塔
- C++数据结构--队列的应用--基数排序
- 集合框架四:LinkedList基本应用(用LinkedList模拟堆栈或队列的数据结构)
- C语言数据结构之栈与队列的应用(1)进制转换