Stack.h
#ifndef _STACK_H_
#define _STACK_H_
#include<stdio.h>
#include<assert.h>
#include<string.h>
#include<stdlib.h>
#include<windows.h>
typedef int DataType;
typedef struct Stack{
DataType *_arr;
size_t top;
size_t end;
}Stack;
void StackInit(Stack *st);
void StackExpand(Stack *st);
void StackPush(Stack *st,DataType x);
void StackPop(Stack *st);
void printSatck(Stack st);
int StackEmpty(Stack st);
DataType StackTop(Stack *st);
size_t StackSize(Stack st);
void test1();
#endif
Stack.c
#include "Stack.h"
void StackInit(Stack *st){
assert(st);
st->top=0;
st->end=1;
st->_arr=(DataType *)malloc(sizeof(DataType)*st->end);
}
void StackExpand(Stack *st){
assert(st);
st->_arr=(DataType *)realloc(st->_arr,sizeof(DataType)*st->end*2);
st->end*=2;
}
int StackEmpty(Stack st){
if(st.top==0)
return 0;
return 1;
}
size_t StackSize(Stack st){
return st.top;
}
void StackPush(Stack *st,DataType x){
assert(st);
if(st->top < st->end){
st->_arr[st->top]=x;
++st->top;
}
else{
StackExpand(st);
st->_arr[st->top]=x;
++st->top;
}
}
void StackPop(Stack *st){
if(st->top>0)
--st->top;
else{
printf("Stack is empty\n");
}
}
void printSatck(Stack st){
while(st.top){
printf("%d\n",st._arr[--st.top]);
}
}
DataType StackTop(Stack *st){
if(st->top>0)
return st->_arr[st->top-1];
return NULL;
}
void test1(){
Stack st;
StackInit(&st);
StackPush(&st,1);
StackPush(&st,2);
StackPush(&st,3);
StackPush(&st,4);
StackPush(&st,5);
printSatck(st);
printf("size :%d\n",StackSize(st));
StackPop(&st);
StackPop(&st);
printSatck(st);
printf("size :%d\n",StackSize(st));
printf("%d \n",StackTop(&st));
StackPop(&st);
printf("%d \n",StackTop(&st));
StackPop(&st);
printf("%d \n",StackTop(&st));
StackPop(&st);
printf("%d \n",StackTop(&st));
StackPop(&st);
printf("%d \n",StackTop(&st));
StackPop(&st);
printf("%d \n",StackTop(&st));
}
Queue.h
#ifndef _QUEUE_H_
#define _QUEUE_H_
#include<stdio.h>
#include<assert.h>
#include<string.h>
#include<stdlib.h>
#include<windows.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(DataType x);
void QueuePush(Queue* q, DataType x);
void QueuePop(Queue* q);
DataType QueueFront(Queue* q);
DataType QueueBack(Queue* q);
size_t QueueSize(Queue* q);
int QueueEmpty(Queue* q);
void TestQueue();
#endif
Queue.c
#include"Queue.h"
void QueueInit(Queue* q){
assert(q);
q->_head = q->_tail =(QueueNode*)malloc(sizeof(QueueNode));
q->_tail->_next=NULL;
}
QueueNode* BuyQueueNode(DataType x){
QueueNode *qNode;
qNode = (QueueNode*)malloc(sizeof(QueueNode));
qNode->_data=x;
qNode->_next=NULL;
return qNode;
}
void QueuePush(Queue* q, DataType x){
QueueNode *newNode;
assert(q);
newNode = BuyQueueNode(x);
q->_tail->_next = newNode;
q->_tail = q->_tail->_next;
}
void QueuePop(Queue* q){
QueueNode *head;
assert(q);
head = q->_head;
if(q->_head==q->_tail){
printf("队空\n");
return;
}
q->_head=q->_head->_next;
free(head);
}
void printQueue(Queue q){
QueueNode *cur;
cur=q._head->_next;
while(cur!=q._tail->_next){
printf("%d\n",cur->_data);
cur=cur->_next;
}
}
DataType QueueFront(Queue* q){
assert(q);
if(QueueEmpty(q))
return q->_head->_next->_data;
return NULL;
}
DataType QueueBack(Queue* q){
assert(q);
if(QueueEmpty(q))
return q->_tail->_data;
return NULL;
}
size_t QueueSize(Queue* q){
int n=0;
QueueNode *cur;
assert(q);
cur = q->_head->_next;
while(cur){
n++;
cur = cur->_next;
}
return n;
}
int QueueEmpty(Queue* q){
assert(q);
if(q->_head==q->_tail)
return 0;
return 1;
}
void TestQueue(){
Queue q;
QueueInit(&q);
QueuePush(&q,1);
QueuePush(&q,2);
QueuePush(&q,3);
QueuePush(&q,4);
printQueue(q);
printf("front %d \n",QueueFront(&q));
printf("back %d \n",QueueBack(&q));
printf("size %d \n",QueueSize(&q));
QueuePop(&q);
QueuePop(&q);
QueuePop(&q);
QueuePop(&q);
QueuePop(&q);
QueuePop(&q);
printQueue(q);
printf("front %d \n",QueueFront(&q));
printf("back %d \n",QueueBack(&q));
printf("size %d \n",QueueSize(&q));
}
main.c
#include"Stack.h"
#include"Queue.h"
int main(){
// test1();
TestQueue();
system("pause");
return 0;
}