系统栈
大小:8MB
1、局部变量
2、未经初始化为随机值
3、代码执行到变量定义时为变量开辟空间
4、当变量的作用域结束时回收空间
5、函数的形参和返回值
6、函数的调用关系、保护现场和恢复现场
7、栈的增长方向,自高向低增长
栈 FILO
只允许从一端进行删除和插入的线性存储结构(线性表)
插入:入栈、压栈 — 栈顶(允许操作)
删除:出栈、弹栈 — 栈底(不允许操作)
顺序栈(栈顶是可以移动的)
1)满增栈
2)满减栈
3)空增栈
4)空减栈
满、空栈:栈顶所在位置是否存有元素
空栈、满栈:栈顶所在位置是否存有元素
增、减栈:栈增长的方向
增:栈顶由低地址向高地址
减:栈顶由高地址向低地址
链式栈
操作:
main.c
#include <stdio.h>
#include "stack.h"
int main(int argc, const char *argv[])
{
STACK_LIST *pstack = create_link_stack();
if (NULL == pstack)
{
return -1;
}
STACK_NODE *pnode = NULL;
for (int i = 0; i < 5; i++)
{
pnode = create_node(i);
push_stack(pstack, pnode);
}
stack_for_each(pstack);
DATA_TYPE data;
pop_stack(pstack, &data);
printf("pop data = %d\n", data);
destroy_stack(pstack);
return 0;
}
.h文件
#ifndef _STACK_H_
#define _STACK_H_
#include <stdio.h>
#include <stdlib.h>
typedef int DATA_TYPE;
typedef struct node
{
DATA_TYPE data;
struct node *pnext;
}STACK_NODE;
typedef struct stack
{
STACK_NODE *ptop;
int clen;
}STACK_LIST;
extern STACK_LIST *create_link_stack();
extern STACK_NODE *create_node(DATA_TYPE data);
extern int push_stack(STACK_LIST *pstack, STACK_NODE *pnode);
extern void stack_for_each(STACK_LIST *pstack);
extern int pop_stack(STACK_LIST *pstack, DATA_TYPE *pdata);
extern int get_stack_top(STACK_LIST *pstack, DATA_TYPE *pdata);
extern void clear_stack(STACK_LIST *pstack);
extern void destroy_stack(STACK_LIST *pstack);
#endif
创建栈
STACK_LIST *create_link_stack()
{
STACK_LIST *pstack = malloc(sizeof(STACK_LIST));
if (NULL == pstack)
{
perror("fail malloc");
return NULL;
}
pstack->ptop = NULL;
pstack->clen = 0;
return pstack;
}
STACK_NODE *create_node(DATA_TYPE data)
{
STACK_NODE *pnode = malloc(sizeof(STACK_NODE));
if (NULL == pnode)
{
perror("fail malloc");
return NULL;
}
pnode->data = data;
pnode->pnext = NULL;
return pnode;
}
入栈
int push_stack(STACK_LIST *pstack, STACK_NODE *pnode)
{
if (NULL == pstack || NULL == pnode)
{
return -1;
}
pnode->pnext = pstack->ptop;
pstack->ptop = pnode;
pstack->clen++;
return 0;
}
出栈
int is_empty_stack(STACK_LIST *pstack)
{
return NULL == pstack->ptop;
}
int pop_stack(STACK_LIST *pstack, DATA_TYPE *pdata)
{
if (NULL == pstack)
{
return -1;
}
if (is_empty_stack(pstack))
{
return -1;
}
STACK_NODE *pnode = pstack->ptop;
pstack->ptop = pnode->pnext;
if (pdata != NULL)
{
*pdata = pnode->data;
}
free(pnode);
pstack->clen--;
return 0;
}
获取栈顶的元素
int get_stack_top(STACK_LIST *pstack, DATA_TYPE *pdata)
{
if (is_empty_stack(pstack))
{
return -1;
}
if (pdata != NULL)
{
*pdata = pstack->ptop->data;
return 0;
}
return -1;
}
遍历
void stack_for_each(STACK_LIST *pstack)
{
STACK_NODE *pnode = pstack->ptop;
while (pnode)
{
printf("%d ", pnode->data);
pnode = pnode->pnext;
}
printf("\n");
}
清空栈
void clear_stack(STACK_LIST *pstack)
{
while (!is_empty_stack(pstack))
{
pop_stack(pstack, NULL);
}
}
销毁栈
void destroy_stack(STACK_LIST *pstack)
{
clear_stack(pstack);
free(pstack);
}
队列(FIFO)
允许从一端进行数据插入,从另一端进行数据删除的线性表(先进先出、后进后出)
数据插入:入队-------对尾
数据删除:出队-------队头
队列的通途:高速设备和低速设备的交互中速度不配问题,进行数据的缓存或缓冲
顺序队列——>循环队列
链式队列:
.h文件
#ifndef __QUEUE_H_
#define __QUEUE_H_
typedef int DATA_TYPE;
typedef struct onde
{
DATA_TYPE data;
struct onde *pnext;
}QUEUE_ONDE;
typedef struct list
{
QUEUE_ONDE *phead;
QUEUE_ONDE *preal;
int clen;
}QUEUE_LIST;
extern QUEUE_LIST *create_linked(void); //创建
extern QUEUE_ONDE *create_node(DATA_TYPE data); //添加数据
extern int is_empty_link(QUEUE_LIST *plist); //判断队列是否为空
extern int Tail_Plug_List(QUEUE_LIST *plist, QUEUE_ONDE *pnode); //入列
extern int Ergodic_Linkde_List(QUEUE_LIST *plist); //遍历
extern int Head_Delete(QUEUE_LIST *plist); //出列
extern int Empty_Queue(QUEUE_LIST *plist); //清空队列
extern int Destroy_Queue(QUEUE_LIST *plist); //销毁
extern int Header_Data(QUEUE_LIST *plist); //获取队列头数据
#endif
1.创建队列
/* 创建空队列 */
QUEUE_LIST *create_linked(void)
{
QUEUE_LIST *plist = NULL;
plist = malloc(sizeof(QUEUE_LIST));
if (NULL == plist)
{
perror("fail to malloc");
return NULL;
}
plist->phead = NULL;
plist->preal = NULL;
plist->clen = 0;
return plist;
}
/* 给对列添加数据 */
QUEUE_ONDE *create_node(DATA_TYPE data)
{
QUEUE_ONDE *pnode = NULL;
pnode = malloc(sizeof(QUEUE_ONDE));
if (NULL == pnode)
{
perror("fail to malloc");
return NULL;
}
pnode->data = data;
pnode->pnext = NULL;
return pnode;
}
/* 判断队列是否为空 */
int is_empty_link(QUEUE_LIST *plist)
{
return NULL == plist->phead;
}
2.入列
/* 入列(尾插) */
int Tail_Plug_List(QUEUE_LIST *plist, QUEUE_ONDE *pnode)
{
QUEUE_ONDE *ptme = NULL;
if (is_empty_link(plist))
{
plist->phead = pnode;
plist->preal = pnode;
plist->clen++;
return 0;
}
else
{
ptme = plist->phead;
while(ptme->pnext != NULL)
{
ptme = ptme->pnext;
}
ptme->pnext = pnode;
plist->preal = pnode;
plist->clen++;
}
return 0;
}
3.出列
/* 出列 */
int Head_Delete(QUEUE_LIST *plist)
{
QUEUE_ONDE *ptme = NULL;
if (is_empty_link(plist))
{
return 0;
}
ptme = plist->phead;
if (plist->phead->pnext == NULL)
{
plist->phead = ptme->pnext;
plist->preal = ptme->pnext;
free(ptme);
}
else
{
plist->phead = ptme->pnext;
free(ptme);
}
plist->clen--;
return 0;
}
4.清空队列
/* 清空队列 */
int Empty_Queue(QUEUE_LIST *plist)
{
while(plist->phead != NULL)
{
Head_Delete(plist);
}
return 0;
}
5.列销毁队
/* 销毁 */
int Destroy_Queue(QUEUE_LIST *plist)
{
Empty_Queue(plist);
free(plist);
return 0;
}
6.获取队列头数据
/* 获取队列头数据 */
int Header_Data(QUEUE_LIST *plist)
{
QUEUE_ONDE *ptme = NULL;
if (is_empty_link(plist))
{
return 0;
}
ptme = plist->phead;
printf("%d\n", ptme->data);
return 0;
}
7.遍历
/* 遍历 */
int Ergodic_Linkde_List(QUEUE_LIST *plist)
{
QUEUE_ONDE *ptme = NULL;
if (is_empty_link(plist))
{
return -1;
}
ptme = plist->phead;
while(ptme != NULL)
{
printf("%d ", ptme->data);
ptme = ptme->pnext;
}
printf("\n");
return 0;
}