forked from cforth/cnforth
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstr_lex.c
More file actions
133 lines (115 loc) · 1.89 KB
/
str_lex.c
File metadata and controls
133 lines (115 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
** str_lex.c
** cforth输入流词法分析模块
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include <assert.h>
#include "str_lex.h"
/*
** 初始化str_now指针和str_prev指针。
** str_now用于指向当前队列尾部节点。
** str_prev用于新增节点到队列时,保存临时节点信息。
**
*/
static StrNode *str_prev = NULL;
static StrNode *str_now = NULL;
/*
** create_str
*/
StrNode *create_str( void ) {
StrNode *p;
p = malloc(sizeof(StrNode));
assert(p != NULL);
p->next = NULL;
str_prev = p;
return p;
};
/*
** add_str
*/
void add_str( int t, char *s )
{
StrNode *new;
new = malloc(sizeof(StrNode));
assert(new != NULL);
new->type = t;
new->length = strlen(s);
strcpy(new->str, s);
str_now = new;
str_now->next = NULL;
str_prev->next = str_now;
str_prev = str_now;
return;
}
/*
** del_str
*/
void del_str( StrNode *head, char *s )
{
StrNode *prov = head;
StrNode *now = head->next;
while(now != NULL) {
if(!strcmp(now->str, s)) {
now = now->next;
free(prov->next);
prov->next = now;
}
else {
prov = now;
now = now->next;
}
}
return;
}
/*
** printf_str
*/
void printf_str( StrNode *head )
{
StrNode *p = head->next;
while(p != NULL) {
printf("type:%d\nlength:%d\nstring:%s\n\n",p->type,p->length,p->str);
p = p->next;
}
return;
}
/*
** destroy_str
*/
void destroy_str( StrNode *head )
{
str_prev = head;
StrNode *p = head->next;
while(p != NULL) {
head = p->next;
free(p);
p = head;
}
str_prev->next = NULL;
str_now = NULL;
return;
}
/*
** 队列测试用例。
*/
int main()
{
StrNode *str_head;
str_head = create_str();
while(1) {
add_str(1,"cf");
add_str(1,"hello");
add_str(0,"811");
add_str(1,"hello");
add_str(0,"811");
add_str(1,"tail");
del_str(str_head, "hello");
del_str(str_head, "811");
del_str(str_head, "cf");
destroy_str(str_head);
}
return 0;
}