最近写了4个头文件,并且在ubuntu10.10下用测试函数测试成功,记录如下
//数组栈头文件a_stack.h
#define STACK_SIZE 100
#define STACK_TYPE int
typedef struct{
STACK_TYPE data[STACK_SIZE];
int top_element;
}*stack,st;
stack s;
void initstack( stack s );
int push( stack s, STACK_TYPE value );
int pop( stack s );
STACK_TYPE top( stack s );
int is_empty( stack s );
int is_full( stack s );
void initstack( stack s ){
s = malloc(sizeof(stack));
s->top_element = -1;
}
int push( stack s, STACK_TYPE value ){
if(!is_full(s)){
s->top_element++;
s->data[s->top_element] = value;
}else
return 0;
return 1;
}
int pop( stack s ){
if(!is_empty(s)){
s->top_element --;
}else{
return 0;
}
return 1;
}
STACK_TYPE top( stack s ){
return s->data[s->top_element];
}
int is_empty( stack s ){
return s->top_element == -1;
}
int is_full( stack s ){
return s->top_element == STACK_SIZE - 1;
}
//链表栈l_stack.h
#define STACK_TYPE int
typedef struct node{
STACK_TYPE element;
struct node *next;
struct node *pior;
}*node;
typedef struct ls{
node stack;
node top;
}*stack;
stack s;
void initstack( stack s );
int push( stack s, STACK_TYPE value );
int pop( stack s );
STACK_TYPE top( stack s );
void initstack( stack s ){
s = malloc(sizeof(struct ls));
s->top = NULL;
}
int push( stack s, STACK_TYPE value ){
node n = malloc(sizeof(struct ls));
n->element = value;
if(s->top == NULL)
s->top = n;
else{
s->top->next = n;
n->pior = s->top;
s->top = s->top->next;
}
return 1;
}
int pop( stack s ){
node t = s->top;
s->top = s->top->pior;
free(t);
return 1;
}
STACK_TYPE top( stack s ){
return s->top->element;
}
//数组队列a_queue.h
#define QUEUE_TYPE int
#define QUEUE_SIZE 100
typedef struct{
QUEUE_TYPE queue[QUEUE_SIZE];
int rear;
int front;
}*queue, qu;
queue q;
void initqueue( queue q );
int enqueue( queue q, QUEUE_TYPE value );
int outqueue( queue q );
int is_emptyq( queue q );
int is_fullq( queue q );
void initqueue( queue q ){
q = malloc(sizeof(queue));
q->rear = q->front = -1;
}
int enqueue( queue q, QUEUE_TYPE value ){
if(!is_fullq(q)){
q->rear = (q->rear + 1)%QUEUE_SIZE;
q->queue[q->rear] = value;
}else{
return 0;
}
return 1;
}
int outqueue( queue q ){
if(!is_emptyq(q)){
q->front = (q->front + 1)%QUEUE_SIZE;
}else{
return 0;
}
return 1;
}
int is_fullq( queue q ){
return (q->rear - q->front + QUEUE_SIZE)%QUEUE_SIZE == QUEUE_SIZE;
}
int is_emptyq( queue q ){
return q->rear == q->front;
}
//链表队列 l_queue.h
#define STACK_TYPE int
typedef struct node{
STACK_TYPE element;
struct node *next;
struct node *pior;
}*node;
typedef struct ls{
node stack;
node top;
}*stack;
stack s;
void initstack( stack s );
int push( stack s, STACK_TYPE value );
int pop( stack s );
STACK_TYPE top( stack s );
void initstack( stack s ){
s = malloc(sizeof(struct ls));
s->top = NULL;
}
int push( stack s, STACK_TYPE value ){
node n = malloc(sizeof(struct ls));
n->element = value;
if(s->top == NULL)
s->top = n;
else{
s->top->next = n;
n->pior = s->top;
s->top = s->top->next;
}
return 1;
}
int pop( stack s ){
node t = s->top;
s->top = s->top->pior;
free(t);
return 1;
}
STACK_TYPE top( stack s ){
return s->top->element;
}
#define STACK_TYPE int
typedef struct node{
STACK_TYPE element;
struct node *next;
struct node *pior;
}*node;
typedef struct ls{
node stack;
node top;
}*stack;
stack s;
void initstack( stack s );
int push( stack s, STACK_TYPE value );
int pop( stack s );
STACK_TYPE top( stack s );
void initstack( stack s ){
s = malloc(sizeof(struct ls));
s->top = NULL;
}
int push( stack s, STACK_TYPE value ){
node n = malloc(sizeof(struct ls));
n->element = value;
if(s->top == NULL)
s->top = n;
else{
s->top->next = n;
n->pior = s->top;
s->top = s->top->next;
}
return 1;
}
int pop( stack s ){
node t = s->top;
s->top = s->top->pior;
free(t);
return 1;
}
STACK_TYPE top( stack s ){
return s->top->element;
}
在这过程中出现一个问题描述如下:
下面是一个编译没有问题的C程序,但是执行的时候显示段错误:
#include<malloc.h>
#include<stdio.h>
typedef struct{
int a[10];
}snode;
snode *s;
void init(snode *s){
s = malloc(sizeof(snode));
}
void push(snode *s, int a){
s->a[0] = a;
}
int main(){
init(s);
push(s,20);
printf("OK/n");
return 0;
}
但是只要作一个小小的修改,问题立即就得到了解决:
将main()中的"init(s)”用“s = malloc(sizeof(snode))”代替:
#include<malloc.h>
#include<stdio.h>
typedef struct{
int a[10];
}snode;
snode *s;
void init(snode *s){
s = malloc(sizeof(snode));
}
void push(snode *s, int a){
s->a[0] = a;
}
int main(){
//init(s);
s = malloc(sizeof(snode));
push(s,20);
printf("OK/n");
return 0;
}
说明:这里的4个头文件,只是初稿,还有很多需要简化的地方,并且可能有BUG存在,以后将会进一步修改。