20150130 【 数据结构 - 顺序链表 】 Makefile和模块分离

来源:互联网 发布:java调用三汇语音卡 编辑:程序博客网 时间:2024/05/22 15:36

主要是使用写程序的方式来写,所以文件比较多。。。





首先,通用 Makefile 文件

CC=gccCONFIG=-g -lpthread -lmTARGET=mainSRCS=$(wildcard *.c)OBJS=$(patsubst %.c, %.o, $(SRCS))$(TARGET):$(OBJS)$(CC)$^-o$@$(CONFIG)%.o:%.c$(CC)$<-c -o$@$(CONFIG)clean:rm -r *.o $(TARGET)





顺序表头文件 sqlist.h

#ifndef SQLIST_H#define SQLIST_H#include <stdio.h>#include <stdlib.h>#include <stdbool.h>#include <string.h>#define DEBUG(x)printf("%s:%d:%s",__FILE__,__LINE__,x)#define PRINT_DATA(d)printf("%d", d);#define PRINTS(s)printf("%s\n", s);#define SIZE100enum SqlistError{SQ_NULL, SQ_EMPTY, SQ_OVER, SQ_NOLIVE, SQ_LIVE};enum SqlistError SqState;typedef int Data;typedef struct Sqlist{Data data[SIZE];int length;}Sqlist;Sqlist * create_sqlist(void);/**/bool push_sqlist(Sqlist *sq, Data d);int delete_sqlist(Sqlist *sq, Data d);bool display_sqlist(Sqlist *sq);int find_sqlist(Sqlist *sq, Data d);void clear_sqlist(Sqlist *sq);#endif//SQLIST_H





头文件 sqlist.h 实现文件 sqlist.c

#include "sqlist.h"Sqlist * create_sqlist(void){Sqlist *sq = (Sqlist *)malloc(sizeof(Sqlist));if( sq == NULL ){SqState = SQ_NULL;return NULL;}else{SqState = SQ_EMPTY;sq->length = 0;return sq;}}bool push_sqlist(Sqlist *sq, Data d){if( sq->length == SIZE ){SqState = SQ_OVER;return false;}sq->data[sq->length] = d;sq->length++;SqState = SQ_LIVE;return true;}int delete_sqlist(Sqlist *sq, Data d){if( sq == NULL ){SqState = SQ_NULL;return -3;}else if( sq->length == 0 ){SqState = SQ_EMPTY;return -2;}else{int i=0;for(i=0; i<sq->length; i++){if( sq->data[i] == d ){for(i=i+1; i<sq->length; i++){sq->data[i-1] = sq->data[i];}sq->length--;SqState = SQ_LIVE;return i;}}SqState = SQ_NOLIVE;return -1;}}bool display_sqlist(Sqlist *sq){if( sq == NULL ){PRINTS("不存在...");SqState = SQ_NOLIVE;return false;}else if( sq->length == 0 ){PRINTS("0 : 空无一人");SqState = SQ_EMPTY;return true;}else{int i=0;printf("%d: ", sq->length);for(i=0; i<sq->length; i++){PRINT_DATA(sq->data[i]);putchar(' ');}PRINTS("");SqState = SQ_LIVE;return true;}}int find_sqlist(Sqlist *sq, Data d){if( sq == NULL ){SqState = SQ_NULL;return -3;}else if( sq->length == 0 ){SqState = SQ_EMPTY;return -2;}else{int i=0;for(i=0; i<sq->length; i++){if( sq->data[i] == d ){SqState = SQ_LIVE;return i;}}SqState = SQ_NOLIVE;return -1;}}void clear_sqlist(Sqlist *sq){memset(sq->data, 0, sizeof(sq->data));sq->length = 0;free(sq);SqState = SQ_NULL;}





主方法 main.c

#include <stdio.h>#include "sqlist.h"#include <stdlib.h>#include <time.h>int main(int argc, int argv){Sqlist *sq = create_sqlist();int data=0;for( ; ; ){scanf("%d", &data);if( data > 0 ){push_sqlist(sq, data);}else if( data < 0 ){delete_sqlist(sq, -data);}else{clear_sqlist(sq);display_sqlist(sq);break;}display_sqlist(sq);}return 0;}


0 0