stack.h和stack.c部分是void *即万能模板部分
main.c
#include <stdio.h>
#include <stdlib.h>
#include "stack.h"
#include "SeqStack.h"
void show_menu(void){
printf("******栈功能测试界面******\n");
printf("*** 1.栈压入元素 \n");
printf("*** 2.栈弹出元素 \n");
printf("*** 3.查看栈顶元素 \n");
printf("*** 4.栈是否为空 \n");
printf("*** 5.栈是否满了 \n");
printf("*** 6.栈里面元素遍历 \n");
printf("*** 0.销毁 \n");
printf(">>>>");
}
void print(const void *pv){
const int *p = (const int *)pv;
printf("%d ",*p);
}
void test_stack(void){
printf("请输入要创建栈的容量大小:");
size_t cap = 0;
scanf("%u",&cap);
Stack s;
stack_init(&s,cap,sizeof(int));
int ret = 0;
int data = 0;
while(true){
show_menu();
int opt = 0;
scanf("%d",&opt);
switch(opt){
case 1:
printf("请输入要压入到栈的元素:");
scanf("%d",&data);
ret = stack_push(&s,&data);
if(ret == -1){
printf("压入失败! 栈满了!\n");
}else{
printf("压入成功!\n");
}
break;
case 2:
data = 0;
ret = stack_pop(&s,&data);
if(ret == -1){
printf("弹出失败! 栈已空!\n");
}else{
printf("弹出成功,元素为:%d\n",data);
}
break;
case 3:
data = 0;
ret = stack_peek(&s,&data);
if(ret == -1){
printf("查看栈顶元素失败! 栈已空!\n");
}else{
printf("栈顶元素为:%d\n",data);
}
break;
case 4:
if(stack_empty(&s)){
printf("栈空空如也!\n");
}else{
printf("栈中有数据!\n");
}
break;
case 5:
if(stack_full(&s)){
printf("栈满!\n");
}else{
printf("栈没满,还有空间存储数据!\n");
}
break;
case 6:
stack_travel(&s,print);
printf("\n");
break;
case 0:
stack_destroy(&s);
return;
}
}
}
void show(int col){
printf("%d ",col);
}
#define QUEENS 8
int eigth_queen(void){
SeqStack stack;
seq_stack_init(&stack,QUEENS);
int col = 0;
size_t cnt = 0;
while(true){
if(!attack(&stack,col)){
seq_stack_push(&stack,col);
if(seq_stack_full(&stack)){
cnt++;
seq_stack_travel(&stack,show);
printf("\n");
do{
seq_stack_pop(&stack,&col);
}while(col+1>=QUEENS);
col++;
continue;
}
col = 0;
}else{
while(col+1>=QUEENS){
seq_stack_pop(&stack,&col);
if(seq_stack_empty(&stack) && col==QUEENS-1){
seq_stack_destroy(&stack);
return cnt;
}
}
++col;
}
}
}
SeqStack gs;
void fj(int num,int cnt){
if(num < cnt){
return;
}
if(cnt == 1){
int top = 0;
seq_stack_top(&gs,&top);
if(seq_stack_empty(&gs)||num>=top){
seq_stack_push(&gs,num);
printf("%d = ",sum(&gs));
seq_stack_travel(&gs,show);
printf("\n");
seq_stack_pop(&gs,&top);
}
return;
}
int x;
for(x=1;num-x>=cnt-1;x++){
int top = 0;
seq_stack_top(&gs,&top);
if(seq_stack_empty(&gs)||x>=top){
seq_stack_push(&gs,x);
fj(num-x,cnt-1);
seq_stack_pop(&gs,&top);
}
}
}
void fenjie(int num){
seq_stack_init(&gs,num);
int i;
for(i=num;i>1;i--){
fj(num,i);
}
seq_stack_destroy(&gs);
}
int main(int argc, char *argv[]) {
printf("请输入一个正整数:");
int num = 0;
scanf("%d",&num);
fenjie(num);
return 0;
}
SeqStack.h
#ifndef _SEQ_STACK_H__
#define _SEQ_STACK_H__
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
typedef struct SeqStack{
int *base;
size_t cap;
size_t size;
}SeqStack;
int seq_stack_init(SeqStack* ps,size_t cap);
bool seq_stack_empty(SeqStack *ps);
bool seq_stack_full(SeqStack *ps);
int seq_stack_push(SeqStack *ps,int data);
int seq_stack_pop(SeqStack *ps,int *pdata);
int seq_stack_top(SeqStack *ps,int *pdata);
void seq_stack_travel(SeqStack *ps,void (*travel)(int));
void seq_stack_destroy(SeqStack *ps);
bool attack(SeqStack *ps,int col);
int sum(SeqStack *ps);
int size(SeqStack *ps);
#endif
SeqStack.c
#include "SeqStack.h"
int seq_stack_init(SeqStack* ps,size_t cap){
ps->base = malloc(cap * sizeof(int));
if(ps->base==NULL)
return -1;
ps->cap = cap;
ps->size = 0;
return 0;
}
bool seq_stack_empty(SeqStack *ps){
return ps->size == 0;
}
bool seq_stack_full(SeqStack *ps){
return ps->size >= ps->cap;
}
int seq_stack_push(SeqStack *ps,int data){
if(seq_stack_full(ps)){
return -1;
}
ps->base[ps->size] = data;
ps->size++;
return 0;
}
int seq_stack_pop(SeqStack *ps,int *pdata){
if(seq_stack_empty(ps)){
return -1;
}
*pdata = ps->base[ps->size];
--ps->size;
return 0;
}
int seq_stack_top(SeqStack *ps,int *pdata){
if(seq_stack_empty(ps)){
return -1;
}
*pdata = ps->base[ps->size-1];
return 0;
}
void seq_stack_travel(SeqStack *ps,void (*travel)(int)){
size_t i;
for(i=0;i<ps->size;i++){
travel(ps->base[i]);
}
}
void seq_stack_destroy(SeqStack *ps){
if(ps->base!=NULL){
free(ps->base);
ps->base = NULL;
ps->cap = 0;
ps->size = 0;
}
}
bool attack(SeqStack *ps,int col){
size_t i;
for(i=0;i<ps->size;i++){
if(col == ps->base[i]){
return true;
}
if(ps->size - i == col-ps->base[i] || ps->size -i == ps->base[i]-col){
return true;
}
}
return false;
}
int sum(SeqStack *ps){
int s = 0;
size_t i;
for(i=0;i<ps->size;i++){
s+=ps->base[i];
}
return s;
}
int size(SeqStack *ps){
return ps->size;
}
stack.h
#ifndef _STACK_H__
#define _STACK_H__
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
typedef struct Stack{
void *base;
size_t cap;
size_t size;
size_t elemSize;
}Stack;
int stack_init(Stack *ps,size_t cap,size_t elemSize);
int stack_push(Stack *ps,const void *pdata);
int stack_pop(Stack *ps,void *pdata);
bool stack_empty(Stack *ps);
bool stack_full(Stack *ps);
int stack_peek(Stack *ps,void *pdata);
void stack_travel(Stack *ps,void (*travel)(const void *));
void stack_destroy(Stack *ps);
#endif
stack.c
#include "stack.h"
int stack_init(Stack *ps,size_t cap,size_t elemSize){
ps->base = malloc(cap * elemSize);
if(ps->base == NULL){
return -1;
}
ps->cap = cap;
ps->size = 0;
ps->elemSize = elemSize;
return 0;
}
bool stack_empty(Stack *ps){
return ps->size == 0;
}
bool stack_full(Stack *ps){
return ps->size >= ps->cap;
}
int stack_push(Stack *ps,const void *pdata){
if(stack_full(ps)){
return -1;
}
memcpy(ps->base+ps->size*ps->elemSize,pdata,ps->elemSize);
++(ps->size);
return 0;
}
int stack_pop(Stack *ps,void *pdata){
if(stack_empty(ps)){
return -1;
}
--(ps->size);
memcpy(pdata,ps->base+ps->size*ps->elemSize,ps->elemSize);
return 0;
}
int stack_peek(Stack *ps,void *pdata){
if(ps->size == 0){
return -1;
}
memcpy(pdata,ps->base+(ps->size-1)*ps->elemSize,ps->elemSize);
return 0;
}
void stack_travel(Stack *ps,void (*travel)(const void *)){
size_t i;
for(i=0;i<ps->size;i++){
travel(ps->base+i*ps->elemSize);
}
}
void stack_destroy(Stack *ps){
if(ps->base != NULL){
free(ps->base);
ps->base = NULL;
ps->elemSize = 0;
ps->size = 0;
}
}