//顺序栈定义及实现
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
//函数结果状态代码
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
typedef int Status;
typedef char SElemType;
typedef struct {
SElemType*base;
SElemType*top;
int stacksize;
}Stack;
void CopySElemType(SElemType&s,SElemType e)
{
s=e;
}
Status InitStack(Stack &S)
{
//建立一个空栈;
S.base=(SElemType*)malloc(sizeof(SElemType));
if(!S.base)exit(OVERFLOW);
S.top=S.base;S.stacksize=STACK_INIT_SIZE;return OK;
}
Status DestroyStack(Stack&S)
{
free(S.base);S.top=NULL;S.base=NULL;S.stacksize=0;return OK;
}
void ClearStack(Stack&S)
{
S.top=S.base;S.stacksize=STACK_INIT_SIZE;
}
Status EmptyStack(Stack S)
{
if(S.top-S.base)return FALSE;
return OK;
}
Status Push(Stack &S,SElemType e)
{
if(S.top-S.base==S.stacksize)
{
Stack SS;SS.top=(SElemType*)realloc(S.base,sizeof(S.stacksize+STACKINCREMENT));
if(!SS.base)exit(OVERFLOW);
free(S.base);S.base=SS.base;S.top=S.base+S.stacksize;S.stacksize+=STACKINCREMENT;
}
CopySElemType(*S.top,e);S.top++;return OK;
}
Status Pop(Stack &S,SElemType &e)
{
if(S.top==S.base)return ERROR;
CopySElemType(e,*(S.top-1));--S.top;
return OK;
}
Status GetTop(Stack S,SElemType&e)
{
if(S.top==S.base)return FALSE;
CopySElemType(e,*(S.top-1));return OK;
}
Status SetTop(Stack&S,SElemType e)
{
if(EmptyStack(S))return FALSE;
CopySElemType(*(S.top-1),e);return OK;
}
对于链式栈可以用如下的定义方式
typedef struct SNode{
SElemType data;
struct SNode *next;
}SNode, *LinkStack;
具体实现,以后有时间再加!2、队列
概念: 队列类似线性表和栈,也是定义在线性结构上的ADT,与线性表和栈的区别在于,元素的插入和删除分别在表的两端进行。类似日常生活中排队,允许插入的一端为队尾(rear),允许删除端称队头(front)
特性:First In First Out先进先出,如操作系统中的作业队列和打印任务队列、日常生活中各类排队业务等均可用队列模拟
队列一般也有两种存储形式,链式队列和顺序队列(循环队列),这里我只写了链式队列的,另一种以后补充。
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#include"stack_sq.h"
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int Status;
typedef char QElemType;
typedef struct QNode{
QElemType data;
struct QNode*next;
}QNode,*QueuePtr;
typedef struct{
QueuePtr front;
QueuePtr rear;
}LinkQueue;
Status InitQueue(LinkQueue&Q)
{
Q.front=(QNode*)malloc(sizeof(QNode));
if(!Q.front)exit(OVERFLOW);
Q.rear=Q.front;Q.front->next=NULL;
return OK;
}
Status DestroyQueue(LinkQueue&Q)
{
QNode*p=Q.front->next;
while(p)
{
Q.front->next=p->next;free(p);
p=Q.front->next;
}
free(Q.front );Q.front=Q.rear=NULL;
return OK;
}
Status ClearQueue(LinkQueue&Q)
{
QueuePtr p=Q.front->next;
while(p)
{
Q.front->next=p->next;
free(p);p=Q.front->next;
}
Q.rear=Q.front;return OK;
}
Status EnQueue(LinkQueue&Q,QElemType e)
{
QueuePtr p;p=(QNode*)malloc(sizeof(QNode));
if(!p)exit(OVERFLOW);
p->next=NULL;p->data=e;
Q.rear->next=p;Q.rear=p;return OK;
}
Status DeQueue(LinkQueue&Q,QElemType&e)
{
if(Q.rear==Q.front)return FALSE;
QueuePtr p=Q.front->next;e=p->data;
Q.front->next=p->next;
if(p==Q.rear)Q.rear=Q.front;
return OK;
}
#include"queue_L.h"
Status HuiWen(QElemType *p)
{
LinkQueue Q;Stack S;SElemType se;QElemType qe;
if(InitQueue(Q));if(InitStack(S));
while(*p!='@'&&*p!='\n')
{
if(EnQueue(Q,*p));if(Push(S,*p));++p;
}
while(Pop(S,se)&&DeQueue(Q,qe))
{
if(se!=qe){printf("不是回文。");return FALSE;}
}
printf("是回文。");return OK;
}
int main()
{
char s[100]="3445abaaba5443@";
if(HuiWen(s));
return 0;
}
ps:虽然简单,但也是我当年一个一个代码敲上去的,还望各位用心指教。