栈是一种特殊的线性表,只能在一端进行插入或者删除。表中允许进行插入或者删除的一端成为栈顶,表的另一端叫做栈底。当栈中没有元素时称为空栈,栈的插入操作称为入栈,栈的删除操作称为出栈。
栈主要的特点就是后进先出。即就是后进栈的元素先出栈,每次进栈的数据元素都放在原来栈顶元素之前成为新的栈顶元素,每次出栈的元素都是当前栈顶的元素。
如上图所示就是栈的插入(进栈)和删除(出栈),大家可以看到,我在图中标记了一个top的位置,这个位置其实表示的就是栈顶元素的下一个位置,因为我们习惯性定义一个变量把它的初始化置为0,但是又要和栈里面有一个元素区分,所以我们将top指向的位置表示成栈顶元素的下一个,就可以可以理解为:top一开始为0,当进栈一个元素时,插入在top的位置,top++。
void STInit(ST* pst)//初始化
{
assert(pst);
pst->a = NULL;
pst->capacity = 0;
pst->top = 0;//栈顶元素下一个位置;
}
void STDestroy(ST* pst)//销毁
{
assert(pst);
free(pst->a);
pst->a = NULL;
pst->capacity = 0;
pst->top = 0;
}
void STPush(ST* pst, STDatatype x)//入栈;
{
assert(pst);
if (pst->top == pst->capacity)
{
int newcapacity = pst->capacity == 0 ? 4 : 2 * pst->capacity;
STDatatype* tmp =(STDatatype *) realloc(pst->a, sizeof(STDatatype) * newcapacity);
if (tmp == NULL)
{
perror("realloc");
return;
}
pst->a = tmp;
pst->capacity = newcapacity;
}
pst->a[pst->top] = x;
pst->top++;
}
void STPop(ST* pst)//出栈;
{
assert(pst);
assert(pst->top>0);
pst->top--;
}
STDatatype STTop(ST* pst)
{
assert(pst);
return pst->a[pst->top-1];
}
bool STEmpty(ST* pst)//判断是否为空
{
assert(pst);
return pst->top == 0;
}
队列,它也是一种操作受限制的线性表,只允许它在表的一端进行插入,而在表的另一端删除操作。把进行插入一端的称为队尾,把进行删除一端的称为对首。向队列中插入新元素称为进队,先元素进队后就成为新的队尾元素;从队列中删除元素称为出队,元素出队后,他后面的元素就成为新的队首元素。
由于队列的插入和删除操作分别是在各自一端操作的,每个元素必然按照进入的次序出队,所以队列最显著的特点就是先进先出。
队列的进队出队如上图所示
void QueueInit(Queue* pq)//初始化;
{
assert(pq);
pq->phead = NULL;
pq->ptail = NULL;
pq->Queuesize = 0;
}
void QueueDestroy(Queue* pq)//销毁
{
assert(pq);
QNode* cur = pq->phead;
while (cur)
{
QNode* Next = cur->next;
free(cur);
cur = Next;
}
pq->phead = NULL;
pq->ptail = NULL;
pq->Queuesize = 0;
}
void QueuePush(Queue* pq, QDatatype x)//进队
{
QNode* newnode = (QNode*)malloc(sizeof(QNode));
if (newnode == NULL)
{
perror("malloc");
return;
}
newnode->data = x;
newnode->next = NULL;
if (pq->ptail == NULL)
{
pq->ptail = pq->phead = newnode;
}
else
{
pq->ptail->next=newnode;
pq->ptail = newnode;
}
pq->Queuesize++;
}
void QueuePop(Queue* pq)//出队
{
assert(pq);
assert(pq->phead);
QNode* cur = pq->phead;
if (cur->next == NULL)
{
free(cur);
cur = NULL;
pq->phead = pq->ptail = NULL;
}
else
{
pq->phead = pq->phead->next;
free(cur);
cur = NULL;
}
pq->Queuesize--;
}
QDatatype QueueFront(Queue* pq)//返回队首元素;
{
assert(pq);
assert(pq->phead);
return pq->phead->data;
}
QDatatype QueueBack(Queue* pq)//队尾元素
{
assert(pq);
assert(pq->ptail);
return pq->ptail->data;
}
bool QueueEmpty(Queue* pq)//判断是否空
{
assert(pq);
return pq->phead == NULL;
}
int QueueSize(Queue* pq)//队里面个数
{
assert(pq);
return pq->Queuesize;
}
#include
#include
#include
#include
typedef int STDatatype;
typedef struct stack
{
STDatatype* a;
int top;
int capacity;
}ST;
void STInit(ST* pst);//初始化
void STDestroy(ST* pst);//销毁
void STPush(ST* pst,STDatatype x);//入栈;
void STPop(ST* pst);//出栈;
STDatatype STTop(ST* pst);//栈顶元素;
bool STEmpty(ST* pst);//判断是否为空
void STInit(ST* pst)//初始化
{
assert(pst);
pst->a = NULL;
pst->capacity = 0;
pst->top = 0;//栈顶元素下一个位置;
}
void STDestroy(ST* pst)//销毁
{
assert(pst);
free(pst->a);
pst->a = NULL;
pst->capacity = 0;
pst->top = 0;
}
void STPush(ST* pst, STDatatype x)//入栈;
{
assert(pst);
if (pst->top == pst->capacity)
{
int newcapacity = pst->capacity == 0 ? 4 : 2 * pst->capacity;
STDatatype* tmp =(STDatatype *) realloc(pst->a, sizeof(STDatatype) * newcapacity);
if (tmp == NULL)
{
perror("realloc");
return;
}
pst->a = tmp;
pst->capacity = newcapacity;
}
pst->a[pst->top] = x;
pst->top++;
}
void STPop(ST* pst)//出栈;
{
assert(pst);
assert(pst->top>0);
pst->top--;
}
STDatatype STTop(ST* pst)
{
assert(pst);
return pst->a[pst->top-1];
}
bool STEmpty(ST* pst)//判断是否为空
{
assert(pst);
return pst->top == 0;
}
以下为测试代码
void test1()
{
ST s;
STInit(&s);
STPush(&s, 1);
STPush(&s, 2);
STPush(&s, 3);
STPush(&s, 4);
STPush(&s, 5);
while (!STEmpty(&s))
{
printf("%d ", STTop(&s));
STPop(&s);
}
STDestroy(&s);
}
int main()
{
test1();
return 0;
}
#include
#include
#include
#include
typedef int QDatatype;
typedef struct QueueNode
{
QDatatype data;
struct QueueNode* next;
}QNode;
typedef struct Queue
{
QNode* phead;
QNode* ptail;
int Queuesize;
}Queue;
void QueueInit(Queue* pq);//初始化;
void QueueDestroy(Queue* pq);//销毁
void QueuePush(Queue* pq, QDatatype x);//进队
void QueuePop(Queue* pq);//出队
QDatatype QueueFront(Queue* pq);//返回队首元素;
QDatatype QueueBack(Queue* pq);//队尾元素
bool QueueEmpty(Queue* pq);//判断是否空
int QueueSize(Queue* pq);//队里面个数
void QueueInit(Queue* pq)//初始化;
{
assert(pq);
pq->phead = NULL;
pq->ptail = NULL;
pq->Queuesize = 0;
}
void QueueDestroy(Queue* pq)//销毁
{
assert(pq);
QNode* cur = pq->phead;
while (cur)
{
QNode* Next = cur->next;
free(cur);
cur = Next;
}
pq->phead = NULL;
pq->ptail = NULL;
pq->Queuesize = 0;
}
void QueuePush(Queue* pq, QDatatype x)//进队
{
QNode* newnode = (QNode*)malloc(sizeof(QNode));
if (newnode == NULL)
{
perror("malloc");
return;
}
newnode->data = x;
newnode->next = NULL;
if (pq->ptail == NULL)
{
pq->ptail = pq->phead = newnode;
}
else
{
pq->ptail->next=newnode;
pq->ptail = newnode;
}
pq->Queuesize++;
}
void QueuePop(Queue* pq)//出队
{
assert(pq);
assert(pq->phead);
QNode* cur = pq->phead;
if (cur->next == NULL)
{
free(cur);
cur = NULL;
pq->phead = pq->ptail = NULL;
}
else
{
pq->phead = pq->phead->next;
free(cur);
cur = NULL;
}
pq->Queuesize--;
}
QDatatype QueueFront(Queue* pq)//返回队首元素;
{
assert(pq);
assert(pq->phead);
return pq->phead->data;
}
QDatatype QueueBack(Queue* pq)//队尾元素
{
assert(pq);
assert(pq->ptail);
return pq->ptail->data;
}
bool QueueEmpty(Queue* pq)//判断是否空
{
assert(pq);
return pq->phead == NULL;
}
int QueueSize(Queue* pq)//队里面个数
{
assert(pq);
return pq->Queuesize;
}
//以下为测试代码
void test1()
{
Queue st;
QueueInit(&st);
QueuePush(&st, 1);
QueuePush(&st, 2);
QueuePush(&st, 3);
QueuePush(&st, 4);
QueuePush(&st, 5);
while (!QueueEmpty(&st))
{
printf("%d ", QueueFront(&st));
QueuePop(&st);
}
QueueDestroy(&st);
}
int main()
{
test1();
return 0;
}