(这段时间学习了栈和队列的相关知识,我整理了相关代码,希望能够帮助大家学习,希望大讨论,指出不足。)
一、栈:
1:函数声明文件:
#ifndef __STACK_H
#define __STACK_H
#include <stdio.h>
#include <assert.h>
#include <malloc.h>
typedef int ElemType;
#define SIZE 10
#define TRUE 1
#define FALSE 0
typedef struct stack
{
ElemType *data;
int size;
int top;
}Stack, *Pstack;
//初始化栈
void InitStack(Pstack stack);
//将val插入栈内
int PushStack(Pstack stack, ElemType val);
//将栈顶的数值用res带出来后删掉
int PopStack(Pstack stack, ElemType *res );
//销毁栈
void Destroy(Pstack stack);
//输出栈
void ShowStack(Pstack stack);
#endif
2:函数实现文件:
#include "Stack.h"
//初始化栈
void InitStack(Pstack stack)
{
assert(stack != NULL);
stack->data = (ElemType *)malloc(sizeof(ElemType) * SIZE);
stack->size = SIZE;
stack->top = 0;
}
static int IsFull(Pstack stack)
{
assert(stack != NULL);
if(stack->top == SIZE)
{
return TRUE;
}
return FALSE;
}
static void Expand(Pstack stack)
{
assert(stack != NULL);
stack->size = 2 * SIZE;
ElemType *new_data = (ElemType *)malloc(sizeof(ElemType) * stack->size);
assert(new_data != NULL);
for( int i = 0; i < stack->top; i++)
{
new_data[i] = stack->data[i];
}
free(stack->data);
stack->data = new_data;
}
//将val插入栈内
int PushStack(Pstack stack, ElemType val)
{
assert(stack != NULL);
if(IsFull(stack))
{
Expand(stack);
if(IsFull(stack))
{
return FALSE;
}
}
stack->data[stack->top++] = val;
printf("top = %d\n",stack->top);
return TRUE;
}
static int IsEmpty(Pstack stack)
{
assert(stack != NULL);
if( stack->top == 0)
{
return TRUE;
}
return FALSE;
}
//将栈顶的数值用res带出来后删掉
int PopStack(Pstack stack, ElemType *res )
{
assert(stack != NULL);
if(IsEmpty(stack))
{
return FALSE;
}
*res = stack->data[--stack->top];
return TRUE;
}
//销毁栈
void Destroy(Pstack stack)
{
assert(stack != NULL);
free(stack->data);
stack->data = NULL;
stack->top = 0;
stack->size = 0;
}
二、队列:
函数声明文件:
#ifndef __QUEUE_H
#define __QUEUE_H
#include <stdio.h>
#include <malloc.h>
#include <assert.h>
typedef int ElemType;
#define SIZE 5
#define TRUE 1
#define FALSE 0
typedef struct Queue
{
ElemType *data;
int head;
int tail;
}Queue, *PQueue;
//初始化队列
void InitQueue(PQueue queue);
//在队列头插入数据
int PushQueue(PQueue queue, ElemType val);
//将头部的值用res带出来,然后删掉
int PopQueue(PQueue queue, ElemType *res);
//销毁队列
void Destroy(PQueue queue);
void Show(PQueue queue);
#endif
函数实现文件:
#include "Queue.h"
//初始化队列
void InitQueue(PQueue queue)
{
assert(queue != NULL);
queue->data = (ElemType *)malloc(sizeof(ElemType) * SIZE);
assert(queue->data);
queue->head = 0;
queue->tail = 0;
}
//判满
static int IsFull(PQueue queue)
{
assert(queue != NULL);
int tmp = queue->tail + 1;//注意这里不要改变queue->tail的值
if(tmp % SIZE == queue->head )
{
return TRUE;
}
return FALSE;
}
//判空
static int IsEmpty(PQueue queue)
{
assert(queue != NULL);
if(queue->head == queue->tail)
{
return TRUE;
}
return FALSE;
}
//在队列头插入数据
int PushQueue(PQueue queue, ElemType val)
{
assert(queue != NULL);
if(IsFull(queue))
{
printf("Full\n");
return FALSE;
}
// 在尾部插入数据后尾下标会向后走一步,(注意因为在插入数据时也有可能拿出数据,所以head也会后移
// 导致head前面为空,所以tail,知道head和tail相遇 )
queue->data[queue->tail++] = val;
queue->tail = queue->tail % SIZE;//让头尾相连
return TRUE;
}
//将头部的值用res带出来,然后删掉
int PopQueue(PQueue queue, ElemType *res)
{
assert(queue != NULL);
if(IsEmpty(queue))
{
return FALSE;
}
*res = queue->data[queue->head++];
queue->head %= SIZE;
return TRUE;
}
//销毁队列
void Destroy(PQueue queue)
{
assert(queue != NULL);
free(queue->data);
queue->data = NULL;
queue->head = 0;
queue->tail = 0;
}
void Show(PQueue queue)
{
assert(queue != NULL);
if(IsEmpty(queue))
{
printf("empty\n");
return ;
}
printf("QUEUE'DATA =");
int i = 0;
for( i = queue->head; i % SIZE != queue->tail ; i %= SIZE)
{
printf("%3d",queue->data[i]);
i++;
}
printf("\n");
printf("head= %d\n",queue->head);
printf("tail= %d\n",queue->tail);
}
三、测试文件
#include "Stack.h"
#include "Queue.h"
int main()
{
//测试队列
Queue queue;
InitQueue(&queue);
ElemType res;
for( int i = 0; i < 5; i++)
{
PushQueue(&queue, i * 10);
Show(&queue);
}
if(PopQueue(&queue, &res))
{
printf("res = %d\n", res);
}
Show(&queue);
//测试栈
/* Stack stack;
InitStack(&stack);
ElemType res = 0;
for( int i = 0; i < 10; i++)
{
PushStack(&stack, i);
}
if(PopStack(&stack, &res))
{
printf("the top of stack is %d\n", res);
}
*/
return 0;
}