GRASS Programmer's Manual
6.4.2(2012)
Main Page
Related Pages
Namespaces
Data Structures
Files
File List
Globals
All
Data Structures
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Macros
Pages
try2.c
Go to the documentation of this file.
1
/*
2
** Written by David Gerdes US Army Construction Engineering Research Lab
3
** April 1992
4
** Copyright 1992 USA-CERL All rights reserved.
5
**
6
*/
7
8
9
/*
10
** read from stdin and each line into a linked list of chars
11
** then print it back out. if there is any argument specified
12
** the lines will be printed out reversed.
13
*/
14
15
#include <stdio.h>
16
#include <grass/linkm.h>
17
18
struct
link
19
{
20
char
let
;
21
struct
link
*
next
;
22
};
23
24
int
main
(
int
argc,
char
*argv[])
25
{
26
register
int
i;
27
VOID_T
*head;
28
struct
link
List, *tmp, *p;
29
int
rev = 0;
30
char
buf[4096];
31
32
if
(argc == 2)
33
rev = 1;
34
35
36
List.
next
=
NULL
;
37
List.
let
=
' '
;
38
39
40
link_set_chunk_size
(1);
41
head = (
VOID_T
*)
link_init
(
sizeof
(
struct
link
));
42
43
44
while
(
NULL
!= gets(buf)) {
45
for
(i = 0; buf[i] !=
'\0'
; i++) {
46
tmp = (
struct
link
*)
link_new
(head);
47
tmp->
let
= buf[i];
48
if
(rev)
49
add_link_rev
(&List, tmp);
50
else
51
add_link
(&List, tmp);
52
}
53
54
dumplist
(&List);
55
56
p = List.
next
;
57
58
while
(p !=
NULL
&& p->
next
!=
NULL
) {
59
tmp = p->
next
;
60
link_dispose
(head, p);
61
p = tmp;
62
}
63
List.
next
=
NULL
;
64
}
65
66
link_cleanup
(head);
67
68
exit(0);
69
}
70
71
int
add_link_rev
(
struct
link
*List,
struct
link
*
link
)
72
{
73
struct
link *p;
74
75
p = List->
next
;
76
List->
next
= link;
77
link->
next
= p;
78
}
79
80
int
add_link
(
struct
link
*List,
struct
link
*
link
)
81
{
82
struct
link *p;
83
84
p = List;
85
while
(p->
next
!=
NULL
)
86
p = p->
next
;
87
p->
next
= link;
88
link->
next
=
NULL
;
89
}
90
91
int
dumplist
(
struct
link
*List)
92
{
93
struct
link
*p;
94
95
p = List->
next
;
96
while
(p !=
NULL
) {
97
putchar(p->
let
);
98
p = p->
next
;
99
}
100
putchar(
'\n'
);
101
}
lib
linkm
try2.c
Generated on Sun Mar 16 2014 05:07:49 for GRASS Programmer's Manual by
1.8.1.2