#include<stdio.h>
#include<Windows.h>
#include<assert.h>
typedef int DataType;
typedef struct Stack
{
DataType* _array;
size_t _top;
size_t _end;
}Stack;
void StackInit(Stack*s,size_t top);
void StackPush(Stack* s, DataType x);
void StackPop(Stack* s);
DataType StackTop(Stack* s);
size_t StackSize(Stack* s);
int StackEmpty(Stack* s);
void Stackcheck(Stack*s);
void StackPrint(Stack*s);
void StackPrint(Stack*s)
{
size_t i = 0;
while (i < s->_top)
{
printf("%d ", s->_array[i]);
i++;
}
}
void Stackcheck(Stack*s)
{
if (s->_top >= s->_end)
{
DataType*array = (DataType*)realloc(s,sizeof(DataType)*2);
assert(array);
s->_array = array;
s->_end = s->_end * 2;
}
}
void StackInit(Stack*s,size_t end)
{
assert(s&&end > 0);
s->_array = (DataType*)malloc(end*sizeof(DataType));
assert(s->_array);
s->_top = 0;
s->_end = end;
}
void StackPush(Stack* s, DataType x)
{
Stackcheck(s);
s->_array[s->_top] = x;
s->_top++;
}
void StackPop(Stack* s)
{
s->_array[s->_top - 1] = 0;
s->_top--;
}
DataType StackTop(Stack* s)
{
return s->_array[s->_top-1];
}
size_t StackSize(Stack* s)
{
return s->_top;
}
int StackEmpty(Stack* s)
{
if (s->_top == 0)
return 1;
else
return 0;
}
#ifndef queue_h
#define queue_h
#include<stdio.h>
#include<Windows.h>
#include<assert.h>
typedef int DataType;
typedef struct QueueNode
{
DataType _data;
struct QueueNode* _next;
}QueueNode;
typedef struct Queue
{
QueueNode* _head;
QueueNode* _tail;
}Queue;
void QueueInit(Queue* q);
QueueNode*BuyQueueNode(Queue*q,DataType x);
void QueuePush(Queue* q, DataType x);
void QueuePrint(Queue*q);
void QueuePop(Queue* q);
DataType QueueFront(Queue* q);
DataType QueueBack(Queue* q);
size_t QueueSize(Queue* q);
int QueueEmpty(Queue* q);
QueueNode* BuyQueueNode(Queue*q,DataType x)
{
QueueNode*newnode = (QueueNode*)malloc(sizeof(QueueNode));
assert(newnode);
newnode->_data = x;
newnode->_next = NULL;
return newnode;
}
void QueueInit(Queue* q)
{
assert(q);
q->_tail = NULL;
q->_head = NULL;
}
void QueuePush(Queue* q, DataType x)
{
if (q->_tail == NULL)
{
q->_head = q->_tail = BuyQueueNode(q,x);
}
else
{
q->_tail->_next = BuyQueueNode(q, x);
q->_tail = q->_tail->_next;
}
}
void QueuePrint(Queue*q)
{
QueueNode*cur = q->_head;
while (cur!=q->_tail->_next)
{
printf("%d ", cur->_data);
cur = cur->_next;
}
}
void QueuePop(Queue* q)
{
QueueNode*next = q->_head->_next;
if (q->_head = NULL)
return;
else
{
free(q->_head);
q->_head = next;
}
}
DataType QueueFront(Queue* q)
{
return q->_head->_data;
}
DataType QueueBack(Queue* q)
{
return q->_tail->_data;
}
size_t QueueSize(Queue* q)
{
QueueNode*cur = q->_head;
int count = 0;
while (cur->_next != q->_tail)
count++;
return count;
}
int QueueEmpty(Queue* q)
{
if (q->_head == q->_tail &&q->_tail == NULL)
return 1;
else
return 0;
}
#endif
#include"queue.h"
void test()
{
Queue q;
QueueInit(&q);
QueuePush(&q, 1);
QueuePush(&q, 2);
QueuePush(&q, 3);
QueuePush(&q, 4);
QueuePop(&q);
QueuePrint(&q);
}
int main()
{
test();
system("pause");
return 0;
}