一、栈
1. 顺序栈
声明栈结构
#define StackMaxSize 16
#define TRUE 1
#define FALSE 0
#define ERROR -1
#define OK 1
typedef struct Stack{
int Data[StackMaxSize];
int top;
}Stack;
初始化
void InitStack(Stack *S)
{
if(S == NULL)
return;
S->top = -1;
}
判空
int isEmpty(Stack *S)
{
if(S->top == -1)
{
return TRUE;
}else
{
return FALSE;
}
}
Push栈
int PushStack(Stack *S, int e)
{
if(S->top >= (StackMaxSize - 1))
{
printf("Stack is full.\n");
return ERROR;
}
S->Data[++(S->top)] = e;
return OK;
}
Pop栈
int PopStack(Stack *S)
{
int e = 0;
if(isEmpty(S))
{
printf("Stack is empty.\n");
return ERROR;
}
e = S->Data[(S->top)--];
return e;
}
2.链栈
声明链栈结构
typedef struct Node{
int data;
struct Node *next;
}Node;
typedef struct LStack{
Node *top;
}LStack;
初始化
void InitStack(LStack *S)
{
S->top = (Node *)malloc(sizeof(Node));
S->top->next = NULL;
}
判空
int isEmpty(LStack *S)
{
if(S->top->next == NULL)
return 1;
else
return 0;
}
Push栈
int PushStack(LStack *S, int e)
{
Node *node_tmp;
S->top->data = e;
node_tmp = (Node *)malloc(sizeof(Node));
if(!node_tmp)
exit(-1);
node_tmp->next = S->top;
S->top = node_tmp;
}
Pop栈
int PopStack(LStack *S)
{
Node *node_tmp;
int e = 0;
if(isEmpty(S))
{
printf("stack is empty.\n");
return -1;
}
node_tmp = S->top->next;
e = node_tmp->data;
if(node_tmp->next != NULL)
{
S->top->next = node_tmp->next;
}else
{
S->top->next = NULL;
}
free(node_tmp);
return e;
}
二、队列
1. 顺序队列-环形结构
声明队列结构
#define QueueMaxSize 16
typedef struct Queue{
int Data[QueueMaxSize];
int front;
int rear;
}Queue;
初始化
void InitQueue(Queue *q)
{
q->front = 0;
q->rear = 0;
}
判空
int isQueueEmpty(Queue *q)
{
if (q->front == q->rear)
{
return TRUE;
}else{
return FALSE;
}
}
判满
int isQueueFull(Queue *q)
{
if ((q->rear + 1) % QueueMaxSize == q->front)
{
return TRUE;
}else{
return FALSE;
}
}
Push队列
int PushQueue(Queue *q, int e)
{
if (isQueueFull(q))
{
printf("Queue is full\n");
return FALSE;
}
q->Data[q->rear] = e;
q->rear = (q->rear + 1) % QueueMaxSize;
return TRUE;
}
Pop队列
int PopQueue(Queue *q)
{
int e = 0;
if (isQueueEmpty(q))
{
printf("Queue is empty\n");
return FALSE;
}
e = q->Data[q->front];
q->front = (q->front + 1) % QueueMaxSize;
return e;
}
2. 链队列
声明队列结构
typedef struct LNode{
int Data;
struct LNode *next;
}Node;
typedef struct LQueue{
Node *front;
Node *rear;
}LQueue;
初始化
void InitQueue(LQueue *q)
{
q->front = q->rear = (Node *)malloc(sizeof(Node));
if(q->front == NULL)
{
exit(-1);
}
q->front->next = NULL;
}
判空
int isEmpty(LQueue *q)
{
if(q->front == q->rear)
return TRUE;
else
return FALSE;
}
Push队列
int PushQueue(LQueue *q, int e)
{
Node *q_tmp;
q_tmp = (Node *)malloc(sizeof(Node));
if(q_tmp == NULL)
{
printf("malloc error\n");
return FALSE;
}
q_tmp->Data = e;
q_tmp->next = NULL;
q->rear->next = q_tmp;
q->rear = q_tmp;
return TRUE;
}
Pop队列
int PopQueue(LQueue *q)
{
int e = 0;
Node *q_tmp = NULL;
if(isEmpty(q))
{
printf("Queue is empty!\n");
return FALSE;
}
q_tmp = q->front->next;
e = q_tmp->Data;
q->front->next = q_tmp->next;
if(q_tmp == q->rear)
q->rear = q->front;
free(q_tmp);
return e;
}
2336

被折叠的 条评论
为什么被折叠?



