Skip to content

Commit 7c06de1

Browse files
morgengcmorgengc
authored andcommitted
1 parent 252c0e1 commit 7c06de1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

87 files changed

+5686
-0
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#################################################
2+
# 针对C环境,一个目录下所有.c编译出一个可执行文件,
3+
# 编写的Makefile
4+
#
5+
# $@: 目标文件
6+
# $^: 所有的依赖文件
7+
# $<: 第一个依赖文件.
8+
#
9+
# author: gaochao
10+
# email: gaoc@dsp.ac.cn
11+
# date: 2009/03/24
12+
#################################################
13+
14+
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
15+
# need complete
16+
#
17+
# {exe(default), static, shared}
18+
BINARY_TYPE :=
19+
20+
MODULE := dlist
21+
22+
LFLAGS :=
23+
24+
CFLAGS := -Wall -MD -g
25+
CFLAGS +=
26+
#
27+
# done
28+
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
29+
30+
31+
ifeq ($(BINARY_TYPE), static)
32+
TARGET = $(addsuffix .a, $(addprefix lib, $(MODULE)))
33+
else
34+
ifeq ($(BINARY_TYPE), shared)
35+
TARGET = $(addsuffix .so, $(addprefix lib, $(MODULE)))
36+
endif
37+
endif
38+
39+
TARGET ?= $(MODULE)
40+
41+
.PHONY: all
42+
all: createdir $(TARGET)
43+
#chmod a+x $(TARGET)
44+
@echo "-------- $(TARGET) done ---------"
45+
46+
SOURCES := $(wildcard *.c)
47+
48+
OBJS := $(patsubst %.c, %.o, $(SOURCES))
49+
50+
OBJDIR := .objs
51+
52+
CROSS_COMPILE :=
53+
54+
AS := $(CROSS_COMPILE)as
55+
LD := $(CROSS_COMPILE)ld
56+
CC := $(CROSS_COMPILE)gcc
57+
58+
CXX := $(CROSS_COMPILE)g++
59+
AR := $(CROSS_COMPILE)ar
60+
61+
MKDIR := mkdir -p
62+
RM := rm -rf
63+
64+
.PHONY: createdir
65+
createdir:
66+
$(MKDIR) $(OBJDIR)
67+
68+
%.o: %.c
69+
$(CC) $(CFLAGS) -c $< -o $(OBJDIR)/$@
70+
71+
$(TARGET): $(OBJS)
72+
ifeq ($(BINARY_TYPE), static)
73+
$(AR) rc $@ $(addprefix $(OBJDIR)/, $^)
74+
else
75+
ifeq ($(BINARY_TYPE), shared)
76+
$(CC) -shared -o $@ $(addprefix $(OBJDIR)/, $^)
77+
else
78+
$(CC) -o $@ $(addprefix $(OBJDIR)/, $^) $(LFLAGS)
79+
endif
80+
endif
81+
82+
.PHONY: clean
83+
clean:
84+
$(RM) $(TARGET)
85+
$(RM) $(OBJDIR)
86+
Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
/****************************************************************
2+
* doubley linked list, looping from tail to head.
3+
*
4+
* +--+-+ +--+-+ +--+-+ +--+-+ +--+-+
5+
* <->| |-|<-->| |-|<-->| |-| ... <-->| |-|<--> | |-|<->
6+
* +--+-+ +--+-+| |+--+-+ +--+-+ +--+-+
7+
* | | | | |
8+
* head | | tail head
9+
* prev next
10+
*
11+
*
12+
* gaoc@dsp.ac.cn
13+
* 2009.09.11
14+
***************************************************************/
15+
16+
#include <stdio.h>
17+
#include <stdlib.h>
18+
#include <string.h>
19+
20+
#include "dlist.h"
21+
22+
pList CreateList()
23+
{
24+
pList list = (pList)malloc(sizeof(List));
25+
P_CHECK_MEMORY(list);
26+
list->count = 0;
27+
list->head = list->tail = NULL;
28+
29+
return list;
30+
}
31+
32+
/* NOTE:
33+
* Delete all the nodes from tail to head.
34+
* No need to update next or prev pointers.
35+
* */
36+
void DestroyList(pList list)
37+
{
38+
V_CHECK_VALID(list);
39+
40+
if (!(list->tail))
41+
goto very_clean;
42+
43+
pNode index = list->tail;
44+
pNode prev = NULL;
45+
do
46+
{
47+
prev = index->prev;
48+
printf("release %d\n", index->data);
49+
free(index);
50+
index = prev;
51+
}while(index != list->tail);
52+
53+
very_clean:
54+
free(list);
55+
list = NULL;
56+
printf("clearly.\n");
57+
}
58+
59+
static pNode CreateNode(int data)
60+
{
61+
pNode node = (pNode)malloc(sizeof(Node));
62+
P_CHECK_MEMORY(node);
63+
node->data = data;
64+
node->prev = node->next = node;
65+
return node;
66+
}
67+
68+
/* NOTE:
69+
* Checking order is from head to tail.
70+
* */
71+
pNode SearchNode(pList list, int data)
72+
{
73+
P_CHECK_VALID(list);
74+
75+
if (!(list->head))
76+
return NULL;
77+
78+
pNode index = list->head;
79+
do
80+
{
81+
if (index->data == data)
82+
{
83+
printf("find node %d\n", data);
84+
return index;
85+
}
86+
else
87+
index = index->next;
88+
}while(index != list->head);
89+
90+
printf("[WARN] Node %d not find.\n", data);
91+
return NULL;
92+
}
93+
94+
/* NOTE:
95+
* Append Node to the end of List. You must
96+
* update tail pointer.
97+
* */
98+
bool InsertNode(pList list, int data)
99+
{
100+
B_CHECK_VALID(list);
101+
102+
pNode node = CreateNode(data);
103+
B_CHECK_RETURN(node);
104+
105+
(list->count)++;
106+
107+
if (!(list->tail))
108+
{
109+
list->head = list->tail = node;
110+
node->prev = node->next = node;
111+
}
112+
else
113+
{
114+
list->tail->next = node;
115+
node->prev = list->tail;
116+
node->next = list->head;
117+
list->head->prev = node;
118+
list->tail = node;
119+
}
120+
121+
return true;
122+
}
123+
124+
/* NOTE:
125+
* Be careful while deleting head and tail elements.
126+
* Because you must update head or tail pointer after deleting.
127+
* */
128+
bool DeleteNode(pList list, int data)
129+
{
130+
B_CHECK_VALID(list);
131+
132+
pNode index = SearchNode(list, data);
133+
B_CHECK_RETURN(index);
134+
135+
(list->count)--;
136+
137+
if (index == list->head) /* case head */
138+
{
139+
if (list->head == list->tail)
140+
{
141+
list->head = list->tail = NULL;
142+
free(index);
143+
}
144+
else
145+
{
146+
list->head = index->next;
147+
148+
list->tail->next = list->head;
149+
list->head->prev = list->tail;
150+
151+
free(index);
152+
}
153+
}
154+
else if (index == list->tail) /* case tail */
155+
{
156+
list->tail = index->prev;
157+
158+
list->tail->next = list->head;
159+
list->head->prev = list->tail;
160+
161+
free(index);
162+
}
163+
else /* the simplest case is inner node */
164+
{
165+
index->prev->next = index->next;
166+
index->next->prev = index->prev;
167+
free(index);
168+
}
169+
170+
return true;
171+
}
172+
173+
void PrintList(pList list)
174+
{
175+
V_CHECK_VALID(list);
176+
177+
printf("head <-> ");
178+
179+
if (!(list->head))
180+
{
181+
printf("tail <-> head\n");
182+
return;
183+
}
184+
185+
pNode node = list->head;
186+
do
187+
{
188+
printf("%d <-> ", node->data);
189+
node = node->next;
190+
}while(node != list->head);
191+
192+
printf("tail <-> head\n");
193+
}
194+
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/******************************************************
2+
* doubley linked list, looping from tail to head.
3+
*
4+
* 双向循环列表
5+
*
6+
* gaoc@dsp.ac.cn
7+
* 2009.09.11
8+
*****************************************************/
9+
10+
#ifndef _D_LIST_H_
11+
#define _D_LIST_H_
12+
13+
/* defined by gcc */
14+
#include "stdbool.h"
15+
16+
typedef struct Node {
17+
struct Node *prev;
18+
struct Node *next;
19+
int data;
20+
} Node, *pNode;
21+
22+
typedef struct List {
23+
Node *head;
24+
Node *tail;
25+
int count;
26+
} List, *pList;
27+
28+
#define P_CHECK_MEMORY(x) \
29+
if ((x) == NULL) \
30+
{ printf("[%s]:[%s()] no available memory.\n", \
31+
__FILE__, __FUNCTION__); \
32+
return NULL;\
33+
}
34+
35+
#define B_CHECK_MEMORY(x) \
36+
if ((x) == NULL) \
37+
{ printf("[%s]:[%s()] no available memory.\n", \
38+
__FILE__, __FUNCTION__); \
39+
return false;\
40+
}
41+
42+
#define P_CHECK_RETURN(x) \
43+
if ((x) == NULL) \
44+
{ printf("[%s]:[%s()] return error.\n", \
45+
__FILE__, __FUNCTION__); \
46+
return NULL;\
47+
}
48+
49+
#define B_CHECK_RETURN(x) \
50+
if ((x) == NULL) \
51+
{ \
52+
return false;\
53+
}
54+
55+
56+
#define P_CHECK_VALID(x) \
57+
if ((x) == NULL) \
58+
{ printf("[%s]:[%s()] NULL, not valid.\n", \
59+
__FILE__, __FUNCTION__); \
60+
return NULL;\
61+
}
62+
63+
#define B_CHECK_VALID(x) \
64+
if ((x) == NULL) \
65+
{ printf("[%s]:[%s()] NULL, not valid.\n", \
66+
__FILE__, __FUNCTION__); \
67+
return false;\
68+
}
69+
70+
#define V_CHECK_VALID(x) \
71+
if ((x) == NULL) \
72+
{ printf("[%s]:[%s()] NULL, not valid.\n", \
73+
__FILE__, __FUNCTION__); \
74+
return;\
75+
}
76+
77+
pList CreateList();
78+
void DestroyList(pList list);
79+
80+
pNode SearchNode(pList list, int data);
81+
bool InsertNode(pList list, int data);
82+
bool DeleteNode(pList list, int data);
83+
84+
void PrintList(pList list);
85+
86+
#endif // _D_LIST_H_
87+

0 commit comments

Comments
 (0)