home *** CD-ROM | disk | FTP | other *** search
- Xref: sparky comp.lang.c:12219 comp.programming:2283
- Path: sparky!uunet!zaphod.mps.ohio-state.edu!pacific.mps.ohio-state.edu!linac!att!att!ulysses!ulysses.att.com!kpv
- From: kpv@ulysses.att.com (Phong Vo)
- Newsgroups: comp.lang.c,comp.programming
- Subject: Re: A LITTLE BRAINTEASER...
- Message-ID: <17063@ulysses.att.com>
- Date: 12 Aug 92 14:40:50 GMT
- References: <aet.713608023@munagin> <1992Aug12.103013.4999@cs.ruu.nl>
- Sender: netnews@ulysses.att.com
- Organization: AT&T Bell Labs
- Lines: 56
-
- In article <1992Aug12.103013.4999@cs.ruu.nl> matthys@cs.ruu.nl (Matthys Kuiper) writes:
- >In <aet.713608023@munagin> aet@mullian.ee.mu.OZ.AU (bert thompson) writes:
- >>is it possible to write a program that reverse the order of input lines
- >>without using data structures.
- >>the rules of the game are you can use anything but data structures, or more
- >>specifically you cannot use the heap.
- >
- >Below is my solution in Pascal. The translation of this program
- >into C is horrible to read and not included in this posting.
- <code deleted>
-
- The code probably isn't right either since functions such as readln() may very well
- does some form of dynamic memory allocation to store the lines.
- Here's a piece of C code that does the job and uses nothing but stack space.
- The more general requirement of not using data structures is of course impossible
- to satisfy since if a program manipulates data, it has to hold the data somewhere.
- The below code uses a thinly disguised link list to store characters of a line.
-
- ---------------------------------------------
- write_line(line)
- char** line;
- {
- if(line)
- { write_line((char**)line[1]);
- write(1,line[0],1);
- }
- }
-
- reverse_lines(last)
- char **last;
- {
- char c;
- char *line[2];
-
- if(read(0,&c,1) != 1)
- return;
-
- if(c != '\n')
- { line[0] = &c;
- line[1] = (char*)last;
- reverse_lines(line);
- }
- else
- { reverse_lines((char**)0);
- write_line(last);
- write(1,&c,1);
- }
- }
-
- main()
- {
- reverse_lines((char**)0);
- }
-
- Phong Vo, kpv@ulysses.att.com
- AT&T Bell Labs, 600 Mountain Ave, Murray Hill, NJ07974
-