# 1.栈

## 1.2栈的顺序存储结构及实现

typedef int SElemType;
typedef struct
{
SElemType data[MAXSIZE];
int top;        /*用于栈顶指针*/
} SqStack;

## 1.3栈的顺序存储结构——进栈操作

#define MAXSIZE 5
#define OK 1
#define ERROR 0

/*插入元素e为新的栈顶元素*/
Status Push(SqStack *S, SElemType e)
{
if (S->top == MAXSIZE - 1)  /*栈满*/
{
return ERROR;
}
S->top++;
S->data[S->top] = e;
return OK;
}

int main()
{
SqStack stack = { {1,2},1 }; /*初始化栈内有两个元素，top=1*/
Push(&stack, 3);
}

## 1.4栈的顺序存储结构——出栈操作

#define MAXSIZE 5
#define OK 1
#define ERROR 0

/*若栈不为空，则删除S的栈顶元素，用e返回其值*/
Status Pop(SqStack *S, SElemType *E)
{
if (S->top == -1)
{
return ERROR;
}
*E = S->data[S->top];
S->data[S->top] = NULL;
S->top--;
return OK;
}

int main()
{
SElemType e;
SqStack stack = { {1,2},1 }; /*初始化栈内有两个元素，top=1*/
Push(&stack, 3);
Pop(&stack, &e);
}

## 1.5栈的链式存储结构及实现

typedef int SElemType;
typedef struct StackNode
{
SElemType data;
struct StackNode  *next;
} StackNode;
typedef struct StackNode *LinkStackPtr;

{
int count;
}LinkStatck;

## 1.6栈的链式存储结构——进栈操作

#define OK 1
#define ERROR 0
typedef int Status;
typedef int SElemType;

/*插入元素e为新的栈顶元素*/
Status Push(LinkStatck *S, SElemType e)
{
s->data = e;
s->next = S->top;
S->top = s; /*将新的节点s赋值给栈顶指针*/
S->count++;
return OK;
}

int main()
{
LinkStatck stack = { NULL ,0}; /*初始化一个空链栈*/
Push(&stack, 1);
Push(&stack, 2);
Push(&stack, 3);
}

## 1.7栈的链式存储结构——出栈操作

#define OK 1
#define ERROR 0
typedef int Status;
typedef int SElemType;

/*若栈不为空，则删除S的栈顶元素，用e返回其值*/
Status Pop(LinkStatck *S, SElemType *e)
{
if (S->count == 0)
{
return ERROR;
}
*e = S->top->data;
p = S->top; /*将栈顶节点赋值给p*/
S->top = S->top->next;  /*使栈顶指针下移一位，指向后一节点*/
free(p);        /*释放节点p*/
S->count--;
return OK;
}

int main()
{
LinkStatck stack = { NULL ,0}; /*初始化一个空链栈*/
Push(&stack, 1);
Push(&stack, 2);
Push(&stack, 3);
SElemType e;
Pop(&stack, &e);
Pop(&stack, &e);
Pop(&stack, &e);
}

# 2.队列

## 2.3队列的链式存储结构及实现

typedef int QElemType;
typedef struct QNode /*结点结构*/
{
QElemType data;
struct QNode *next;
}QNode,*QueuePtr;

typedef struct /*队列的链表结构*/
{
QueuePtr front, rear;   /*队头、队尾指针*/
} LinkQueue;

## 2.4队列的链式存储结构——入队操作

#define OK 1
#define ERROR 0

typedef int Status;
/*插入元素e为Q的新的队尾元素*/
Status EnQueue(LinkQueue *Q, QElemType e)
{
QueuePtr s = (QueuePtr)malloc(sizeof(QNode));
s->data = e;
s->next = NULL;

Q->rear->next = s;      /*把拥有元素e新节点s赋值给原队尾结点的后继*/
Q->rear = s;        /*把s设置为队尾结点，rear指向s*/

return OK;
}

int main()
{
/*头结点*/
QueuePtr head = (QueuePtr)malloc(sizeof(QNode));

EnQueue(&q, 1);
EnQueue(&q, 2);
}

## 2.4队列的链式存储结构——出队操作

#define OK 1
#define ERROR 0

typedef int Status;
/*若队列不为空，删除Q的队头元素，用e返回其值*/
Status DeQueue(LinkQueue *Q, QElemType *e)
{
QueuePtr p;
if (Q->front == Q->rear)
{
return ERROR;
}
p = Q->front->next;  /*将欲删除的队头节点暂存给p*/
*e = p->data;
Q->front->next = p->next;    /*将原队头结点后继赋值给头结点后继*/

if (Q->rear == p) /*若队头是队尾，则删除后将rear指向头结点*/
{
Q->rear = Q->front;
}

free(p);
return OK;
}

int main()
{
/*头结点*/
QueuePtr head = (QueuePtr)malloc(sizeof(QNode));

EnQueue(&q, 1);
EnQueue(&q, 2);
QElemType e;
DeQueue(&q, &e);
}

posted @ 2018-04-16 22:30 CoderFocus 阅读(...) 评论(...) 编辑 收藏