顺序栈基本运算算法
#include <stdio.h>
#include <malloc.h>
#define MaxSize 100
typedef char ElemType;
typedef struct
{
ElemType data[MaxSize];
int top;
} SqStack;
void InitStack(SqStack *&s)
{
s=(SqStack *)malloc(sizeof(SqStack));
s->top=-1;
}
void DestroyStack(SqStack *&s)
{
free(s);
}
bool StackEmpty(SqStack *s)
{
return(s->top==-1);
}
bool Push(SqStack *&s,ElemType e)
{
if (s->top==MaxSize-1)
return false;
s->top++;
s->data[s->top]=e;
return true;
}
bool Pop(SqStack *&s,ElemType &e)
{
if (s->top==-1)
return false;
e=s->data[s->top];
s->top--;
return true;
}
bool GetTop(SqStack *s,ElemType &e)
{
if (s->top==-1)
return false;
e=s->data[s->top];
return true;
}
顺序队列(非环形队列)基本运算算法
#include <stdio.h>
#include <malloc.h>
#define MaxSize 100
typedef char ElemType;
typedef struct
{
ElemType data[MaxSize];
int front,rear;
} SqQueue;
void InitQueue(SqQueue *&q)
{ q=(SqQueue *)malloc (sizeof(SqQueue));
q->front=q->rear=-1;
}
void DestroyQueue(SqQueue *&q)
{
free(q);
}
bool QueueEmpty(SqQueue *q)
{
return(q->front==q->rear);
}
bool enQueue(SqQueue *&q,ElemType e)
{ if (q->rear==MaxSize-1)
return false;
q->rear++;
q->data[q->rear]=e;
return true;
}
bool deQueue(SqQueue *&q,ElemType &e)
{ if (q->front==q->rear)
return false;
q->front++;
e=q->data[q->front];
return true;
}
顺序队列(环形队列)基本运算算法
#include <stdio.h>
#include <malloc.h>
#define MaxSize 100
typedef char ElemType;
typedef struct
{
ElemType data[MaxSize];
int front,rear;
} SqQueue;
void InitQueue(SqQueue *&q)
{ q=(SqQueue *)malloc (sizeof(SqQueue));
q->front=q->rear=0;
}
void DestroyQueue(SqQueue *&q)
{
free(q);
}
bool QueueEmpty(SqQueue *q)
{
return(q->front==q->rear);
}
bool enQueue(SqQueue *&q,ElemType e)
{ if ((q->rear+1)%MaxSize==q->front)
return false;
q->rear=(q->rear+1)%MaxSize;
q->data[q->rear]=e;
return true;
}
bool deQueue(SqQueue *&q,ElemType &e)
{ if (q->front==q->rear)
return false;
q->front=(q->front+1)%MaxSize;
e=q->data[q->front];
return true;
}
求解报数问题
#include <stdio.h>
#include <malloc.h>
#define MaxSize 100
typedef int ElemType;
typedef struct
{ ElemType data[MaxSize];
int front,rear;
} SqQueue;
void InitQueue(SqQueue *&q)
{ q=(SqQueue *)malloc (sizeof(SqQueue));
q->front=q->rear=0;
}
void DestroyQueue(SqQueue *&q)
{
free(q);
}
bool QueueEmpty(SqQueue *q)
{
return(q->front==q->rear);
}
bool enQueue(SqQueue *&q,ElemType e)
{ if ((q->rear+1)%MaxSize==q->front)
return false;
q->rear=(q->rear+1)%MaxSize;
q->data[q->rear]=e;
return true;
}
bool deQueue(SqQueue *&q,ElemType &e)
{ if (q->front==q->rear)
return false;
q->front=(q->front+1)%MaxSize;
e=q->data[q->front];
return true;
}
void number(int n)
{
int i; ElemType e;
SqQueue *q;
InitQueue(q);
for (i=1;i<=n;i++)
enQueue(q,i);
printf("报数出列顺序:");
while (!QueueEmpty(q))
{
deQueue(q,e);
printf("%d ",e);
if (!QueueEmpty(q))
{
deQueue(q,e);
enQueue(q,e);
}
}
printf("\n");
DestroyQueue(q);
}
int main()
{
int i,n=8;
printf("初始序列:");
for (i=1;i<=n;i++)
printf("%d ",i);
printf("\n");
number(n);
return 1;
}
链栈基本运算算法
#include <stdio.h>
#include <malloc.h>
typedef char ElemType;
typedef struct linknode
{
ElemType data;
struct linknode *next;
} LinkStNode;
void InitStack(LinkStNode *&s)
{
s=(LinkStNode *)malloc(sizeof(LinkStNode));
s->next=NULL;
}
void DestroyStack(LinkStNode *&s)
{
LinkStNode *p=s->next;
while (p!=NULL)
{
free(s);
s=p;
p=p->next;
}
free(s);
}
bool StackEmpty(LinkStNode *s)
{
return(s->next==NULL);
}
void Push(LinkStNode *&s,ElemType e)
{ LinkStNode *p;
p=(LinkStNode *)malloc(sizeof(LinkStNode));
p->data=e;
p->next=s->next;
s->next=p;
}
bool Pop(LinkStNode *&s,ElemType &e)
{ LinkStNode *p;
if (s->next==NULL)
return false;
p=s->next;
e=p->data;
s->next=p->next;
free(p);
return true;
}
bool GetTop(LinkStNode *s,ElemType &e)
{ if (s->next==NULL)
return false;
e=s->next->data;
return true;
}
链队运算算法
#include <stdio.h>
#include <malloc.h>
typedef char ElemType;
typedef struct DataNode
{
ElemType data;
struct DataNode *next;
} DataNode;
typedef struct
{
DataNode *front;
DataNode *rear;
} LinkQuNode;
void InitQueue(LinkQuNode *&q)
{
q=(LinkQuNode *)malloc(sizeof(LinkQuNode));
q->front=q->rear=NULL;
}
void DestroyQueue(LinkQuNode *&q)
{
DataNode *p=q->front,*r;
if (p!=NULL)
{ r=p->next;
while (r!=NULL)
{ free(p);
p=r;r=p->next;
}
}
free(p);
free(q);
}
bool QueueEmpty(LinkQuNode *q)
{
return(q->rear==NULL);
}
void enQueue(LinkQuNode *&q,ElemType e)
{ DataNode *p;
p=(DataNode *)malloc(sizeof(DataNode));
p->data=e;
p->next=NULL;
if (q->rear==NULL)
q->front=q->rear=p;
else
{ q->rear->next=p;
q->rear=p;
}
}
bool deQueue(LinkQuNode *&q,ElemType &e)
{ DataNode *t;
if (q->rear==NULL)
return false;
t=q->front;
if (q->front==q->rear)
q->front=q->rear=NULL;
else
q->front=q->front->next;
e=t->data;
free(t);
return true;
}
求简单表达式的值
#include <stdio.h>
#include <stdlib.h>
#define MaxSize 100
typedef struct
{ char data[MaxSize];
int top;
} SqStack;
void InitStack(SqStack *&s)
{ s=(SqStack *)malloc(sizeof(SqStack));
s->top=-1;
}
void DestroyStack(SqStack *&s)
{
free(s);
}
bool StackEmpty(SqStack *s)
{
return(s->top==-1);
}
bool Push(SqStack *&s,char e)
{ if (s->top==MaxSize-1)
return false;
s->top++;
s->data[s->top]=e;
return true;
}
bool Pop(SqStack *&s,char &e)
{ if (s->top==-1)
return false;
e=s->data[s->top];
s->top--;
return true;
}
bool GetTop(SqStack *s,char &e)
{ if (s->top==-1)
return false;
e=s->data[s->top];
return true;
}
void trans(char *exp,char postexp[])
{
char e;
SqStack *Optr;
InitStack(Optr);
int i=0;
while (*exp!='\0')
{ switch(*exp)
{
case '(':
Push(Optr,'(');
exp++;
break;
case ')':
Pop(Optr,e);
while (e!='(')
{
postexp[i++]=e;
Pop(Optr,e);
}
exp++;
break;
case '+':
case '-':
while (!StackEmpty(Optr))
{
GetTop(Optr,e);
if (e!='(')
{
postexp[i++]=e;
Pop(Optr,e);
}
else
break;
}
Push(Optr,*exp);
exp++;
break;
case '*':
case '/':
while (!StackEmpty(Optr))
{
GetTop(Optr,e);
if (e=='*' || e=='/')
{
postexp[i++]=e;
Pop(Optr,e);
}
else
break;
}
Push(Optr,*exp);
exp++;
break;
default:
while (*exp>='0' && *exp<='9')
{ postexp[i++]=*exp;
exp++;
}
postexp[i++]='#';
}
}
while (!StackEmpty(Optr))
{
Pop(Optr,e);
postexp[i++]=e;
}
postexp[i]='\0';
DestroyStack(Optr);
}
typedef struct
{ double data[MaxSize];
int top;
} SqStack1;
void InitStack1(SqStack1 *&s)
{ s=(SqStack1 *)malloc(sizeof(SqStack1));
s->top=-1;
}
void DestroyStack1(SqStack1 *&s)
{
free(s);
}
bool StackEmpty1(SqStack1 *s)
{
return(s->top==-1);
}
bool Push1(SqStack1 *&s,double e)
{ if (s->top==MaxSize-1)
return false;
s->top++;
s->data[s->top]=e;
return true;
}
bool Pop1(SqStack1 *&s,double &e)
{ if (s->top==-1)
return false;
e=s->data[s->top];
s->top--;
return true;
}
bool GetTop1(SqStack1 *s,double &e)
{ if (s->top==-1)
return false;
e=s->data[s->top];
return true;
}
double compvalue(char *postexp)
{
double d,a,b,c,e;
SqStack1 *Opnd;
InitStack1(Opnd);
while (*postexp!='\0')
{
switch (*postexp)
{
case '+':
Pop1(Opnd,a);
Pop1(Opnd,b);
c=b+a;
Push1(Opnd,c);
break;
case '-':
Pop1(Opnd,a);
Pop1(Opnd,b);
c=b-a;
Push1(Opnd,c);
break;
case '*':
Pop1(Opnd,a);
Pop1(Opnd,b);
c=b*a;
Push1(Opnd,c);
break;
case '/':
Pop1(Opnd,a);
Pop1(Opnd,b);
if (a!=0)
{
c=b/a;
Push1(Opnd,c);
break;
}
else
{
printf("\n\t除零错误!\n");
exit(0);
}
break;
default:
d=0;
while (*postexp>='0' && *postexp<='9')
{
d=10*d+*postexp-'0';
postexp++;
}
Push1(Opnd,d);
break;
}
postexp++;
}
GetTop1(Opnd,e);
DestroyStack1(Opnd);
return e;
}
int main()
{
char exp[]="(56-20)/(4+2)";
char postexp[MaxSize];
trans(exp,postexp);
printf("中缀表达式:%s\n",exp);
printf("后缀表达式:%s\n",postexp);
printf("表达式的值:%g\n",compvalue(postexp));
return 1;
}