# 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;

{
int count;

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

```#define OK 1
#define ERROR 0
typedef int Status;
typedef int 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返回其值*/
{
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;   /*队头、队尾指针*/

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

```#define OK 1
#define ERROR 0

typedef int Status;
/*插入元素e为Q的新的队尾元素*/
{
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()
{
/*头结点*/

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

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

```#define OK 1
#define ERROR 0

typedef int Status;
/*若队列不为空，删除Q的队头元素，用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()
{
/*头结点*/

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

68 篇文章23 人订阅

0 条评论

## 相关文章

### Shapes and line types for R

Solution ? ? Note that with bitmap output, the filled symbols 15-18 may render ...

3908

1.3K2

4248

2707

8667

### 通过写Java代码来对MyEclipse进行注册

import java.io.BufferedReader; import java.io.IOException; import java.io...

1324

61012

2279

### 数据结构(三):栈与队列

3.1❶若按教科书3.1.1节中图3.1(b)所示铁道进行车厢调度（注意：两侧铁道均为单向行驶道），则请回答： ? (1) 如果进站的车厢序列为123，则可能...

3737

2106