推荐阅读:
数据结构(C++)学习篇(三):线性表(链式表示和实现)
数据结构(C++)学习篇(四):线性表(循环链表)
数据结构(C++)学习篇(五):线性表(双向链表)-CSDN博客
================================================================
定义:线性表的子集,即是插入和删除位置受限的线性表。
特点:限定插入和删除只能在“端点”
#define MAXSIZE 100
typedef struct{
SElemType *base;
SElemType *top;
int stacksize;
}SqStack;
//构造一个空栈
Status InitStack(SqStack &S)
{
S.base = new SElemType[MAXSIZE];
//S.base = (SElemType *)malloc(MAXSIZE*sizeof(SEelmType)) C语言
if(!S.base)
return OVERFLOW;
S.top = S.base;
S.stacksize = MAXSIZE;
}
Status StackEmpty(SqStack S)
{
if(S.top == S.base) //空?
return TRUE;
esle
return FALSE;
}
int StackLength(SqStack S)
{
return S.top - S.base;
}
Status ClearStack(SqStack S)
{
if(S.base) S.top = S.base;
return OK;
}
Status DestroyStack(SqStack &S)
{
if(S.base)
{
delete S.base;
S.stacksize = 0;
S.base = S.top = NULL;
}
return OK;
}
Status Push(SqStack &S, SElemType e)
{
if(S.top-S.base == S.stacksize) //栈满
return ERROR;
*S.top++ = e; //*S.top=e;S.top++;
return OK;
}
Status Pop(SqStack &S, SElemType &e)
{
if(S.top==S.base) return ERROR;
e = *--S.top; //--S.top; e=*S.top;
return OK;
}
特性:
typedef struct StackNode{
SElemType data;
struct StackNode *next;
}StackNode, *LinkStack;
LinkStack S;
void InitStack(LinkStack &S)
{
S = NULL;
return OK;
}
Status StackEmpty(LinkStack S)
{
if(S == NULL)
return TRUE;
else
return FALSE;
}
Status Push(LinkStack &S, SElemType e)
{
p = new StackNode; //生成新结点
p->data = e;
p->next = S; //插入栈顶
S = p; //修改栈顶指针
return OK;
}
Status Pop(LinkStack &S, SElemType &e)
{
LinkStack p=NULL;
if(S==NULL) return ERROR;
e = S->data;
p = S;
S=S->next;
delete p;
return OK;
}
SElemType GetTop(LinkStack S)
{
if(S != NULL)
return S->data;
}
=====================================================
#define MAXQSIZE 100
typedef struct {
QElemType *base; //初始化动态内存分配
int front; //头
int rear; //尾
}SqQueue;
常用:循环队列,使用求模运算实现。
循环队列解决队满的判断方法之一---少用一个元素空间。
Status InitQueue(SqQueue &Q)
{
Q.base = new QElemType[MAXQSIZE];
if(!Q.base)
return OVERFLOW;
Q.front = Q.rear = 0;
return OK;
}
int QueueLength(SqQueue Q)
{
return ( (Q.rear-Q.front+MAXQSIZE) % MAXQSIZE);
}
Status EnQueue(SqQueue &Q, QElemType e)
{
if((Q.rear+1)%MAXQSIZE == Q.front) return ERROR; //队满
Q.base[Q.rear] = e; //新元素加入队尾
Q.rear = (Q.rear+1) % MAXQSIZE; //队尾指针+1
return OK;
}
Status DeQueue(SqQueue &Q, QElemType &e)
{
if(Q.rear == Q.front)
return ERROR; //队空
e = Q.base[Q.front];
Q.front = (Q.front+1) % MAXQSIZE; //队头指针+1
return OK;
}
SElemType GetHead(SqQueue Q)
{
if(Q.front != Q.rear)
return Q.base[Q.front];
}
#define MAXQSIZE 100
typedef struct Qnode{
QElmType data;
struct Qnode *next;
}QNode, *QueuePtr;
typedef struct{
QueuePtr front;//队头指针
QueuePtr rear;//队尾指针
}LinkQueue;
Status InitQueue(LinkQueue &Q)
{
Q.front = Q.rear = (QueuePtr)malloc(sizeof(QNode));
if(!Q.front) return OVERFLOW;
Q.front->next = NULL;
return OK;
}
Status DestroyQueue(LinkQueue &Q)
{
QueuePtr p;
while(Q.front){
p = Q.front->next; //Q.rear = Q.front->next;
free(Q.front);
Q.front = p; //Q.front = Q.rear;
}
return OK;
}
Status EnQueue(LinkQueue &Q, QElmType e)
{
QueuePtr p = NULL;
p = (QueuePtr)malloc(sizeof(QNode));
if(!p)
return OVERFLOW;
p->data = e;
p->next = NULL;
Q.rear->next = p;
Q.rear = p;
return OK;
}
Status DeleteQueue(LinkQueue &Q, QElemType &e)
{
QueuePtr p = NULL;
if(Q.front == Q.rear) return ERROR;
p = Q.front->next;
e = p->data;
Q.front->next = p->next;
if(Q.rear == p)
Q.rear = Q.front;
delete p;
return OK;
}
Status GetHead(LinkQueue Q, QElemType &e)
{
if(Q.front == Q.rear)
return ERROR;
e = Q.front->next->data;
return OK;
}
文中代码均为手动输入,如有错误,欢迎指正!
视频:戳这里_B站大学