#include<stdlib.h>
//定义栈
typedef int data_t;
typedef struct stack{
data_t *data;
int max;
int top;
}sstack_t;
//创建
sstack_t *creat_stack(int max)
{
sstack_t *stack = malloc(sizeof(sstack_t));
if(NULL == stack)
return NULL;
stack->data = malloc(max * sizeof(data_t));
if(NULL == stack->data)
{
free(stack);
return NULL;
}
stack->top = -1;
stack->max = max;
printf("create success!\n");
return stack;
}
//打印
void print_stack(sstack_t *stack)
{
int i;
printf(":top:\n");
for(i = stack->top;i >= 0;i --){
printf(" %d \n",stack->data[i]);
}
printf(":end:\ntop = %d\n",stack->top);
}
//入栈
int push_stack(sstack_t *stack,const data_t *data)
{
if(NULL == data || NULL == stack)
return -1;
if(stack->top + 1 >= stack->max)
return -2;
stack->data[++stack->top] = *data;
return 0;
}
//出栈
int pop_stack(sstack_t *stack,data_t *databuf)
{
if(NULL == stack || NULL == databuf)
return -1;
if(stack->top == -1)
return -2;
*databuf = stack->data[stack->top--];
return 0;
}
//获取头部
int get_top_stack(sstack_t *stack,data_t *databuf)
{
*databuf = stack->data[stack->top];
return 0;
}
//是否为空
int empty_stack(sstack_t *stack)
{
if(NULL == stack)
return -1;
return stack->top == -1;
}
//是否满
int full_stack(sstack_t *stack)
{
if(NULL == stack)
return -1;
return stack->top == stack->max - 1;
}
//清空栈
int clean_stack(sstack_t *sp)
{
sp->top = -1;
return 0;
}
int main()
{
data_t buf = 10;
sstack_t *sp = creat_stack(8);
for(buf = 0; buf < 10; buf++)
push_stack(sp,&buf);
print_stack(sp);
get_top_stack(sp,&buf);
printf("stack_top: %d\n",buf);
buf = full_stack(sp);
printf("full: %d\n",buf);
for(buf = 0; buf < 10; buf++)
{
pop_stack(sp,&buf);
}
print_stack(sp);
return 0;
}
//顺序表实现队列
#include<stdio.h>
#include<stdlib.h>
#include<strings.h>
//定义队列
typedef int data_t;
typedef struct node{
data_t *data;
int front;
int rear;
int max;
}queue_t;
//创建队列
queue_t *creat_queue(int max)
{
queue_t *sp = malloc(sizeof(queue_t));
memset(sp,0,sizeof(queue_t));
sp->max = max;
sp->front = 0;
sp->rear = 0;
sp->data = malloc(max*sizeof(data_t));
return sp;
}
//入队列
int input_queue(queue_t *sp,data_t *data)
{
int front = sp->front;
int rear = sp->rear;
int max = sp->max;
data_t *temp = sp->data;
if((rear+1) % max == front)
return -1;
temp[rear] = *data;
rear = (rear+1) % max;//注意出问题的地方
sp->rear =rear;
return 0;
}
//出队列
int output_queue(queue_t *sp,data_t *databuf)
{
int front = sp->front;
int rear = sp->rear;
int max = sp->max;
data_t *temp = sp->data;
if(rear == front)
return -1;
*databuf = temp[front];
front = (front + 1) % max;
sp->front = front;
return 0;
}
//打印
void print_queue(queue_t *sp)
{
int front = sp->front;
int rear = sp->rear;
int max = sp->max;
data_t *temp = sp->data;
while(front != rear)
{
printf(" %d ",temp[front]);
front = (front + 1) % max;
}
printf("\n");
return;
}
//队列长度
int length_queue(queue_t *sp)
{
int front = sp->front;
int rear = sp->rear;
int len = (rear - front + sp->max) % sp->max;
return len;
}
int main(int argc, const char *argv[])
{
queue_t *sp = creat_queue(10);
int data = 20;
for(data = 1; data < 11; data++)
input_queue(sp,&data);
print_queue(sp);
int temp;
for(data = 1; data <12; data++)
{
int result = output_queue(sp,&temp);
if(result == 0)
printf("pop: %d\n",temp);
}
printf("\n");
print_queue(sp);
return 0;
}