Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!border3.nntp.dca.giganews.com!Xl.tags.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local2.nntp.dca.giganews.com!nntp.earthlink.com!news.earthlink.com.POSTED!not-for-mail NNTP-Posting-Date: Sun, 26 Jun 2011 01:11:54 -0500 Message-ID: <4E06CDAB.2E20@mindspring.com> Date: Sun, 26 Jun 2011 02:11:55 -0400 From: pete Reply-To: pfiland@mindspring.com Organization: PF X-Mailer: Mozilla 3.04Gold (WinNT; I) MIME-Version: 1.0 Newsgroups: comp.lang.c Subject: Re: Array implementation of Stack References: <4df9d41f$0$308$14726298@news.sunsite.dk> <4dfceef1$0$44207$4fafbaef@reader1.news.tin.it> <4e058797$0$44207$4fafbaef@reader1.news.tin.it> <4e06c256$0$15665$4fafbaef@reader2.news.tin.it> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Lines: 87 X-Usenet-Provider: http://www.giganews.com NNTP-Posting-Host: 4.154.219.253 X-Trace: sv3-rocrjMwqsD1ZJ3rtKU0IEpqrcGSkcwvuwctw2Qb7vv9jfWTn5+5xY+ChiCrkkWkWW4bhOcbVEgVFIfT!Btk6Suqc8tcJVrNHu1LCOMq0eU9ZdP4x/UTP+gPasOPkjjFxGTt/9gN2LW1d1UFyWXVMaJ3LSY6q!epheOsfIDTI= X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 X-Original-Bytes: 3913 Xref: x330-a1.tempe.blueboxinc.net comp.lang.c:6958 io_x wrote: > > "io_x" ha scritto nel messaggio > news:4e058797$0$44207$4fafbaef@reader1.news.tin.it... > > > > "io_x" ha scritto nel messaggio > > news:4dfceef1$0$44207$4fafbaef@reader1.news.tin.it... > >> > >> "arnuld" ha scritto nel messaggio > >> news:4df9d41f$0$308$14726298@news.sunsite.dk... > >>> WANTED: To write an array implementation of Stack. > >>> WHAT I GOT: It works fine. > >>> > >>> I think program is still little too complex. Any ideas to improve ? > > > > what about one stack of pointers? > > i think at end could be better a stack of objects, > than a stack of pointers... Here's an array based stack of structures which doesn't have any functions written for accessing the stack. http://www.mindspring.com/~pfilandr/C/q_sort/pt_sort.c #include #include void q1sort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *)) { unsigned char *left; size_t middle, last, right; struct { size_t bytes; void *base; } stack[CHAR_BIT * sizeof nmemb], *stack_ptr; unsigned char *p1, *p2, *end, swap; stack -> bytes = nmemb * size; stack -> base = base; stack_ptr = stack + 1; do { --stack_ptr; if (stack_ptr -> bytes > size) { right = stack_ptr -> bytes - size; left = stack_ptr -> base; last = middle = 0; do { middle += size; if (compar(left, left + middle) > 0) { last += size; p1 = left + middle; p2 = left + last; end = p2 + size; do { swap = *p1; *p1++ = *p2; *p2++ = swap; } while (p2 != end); } } while (middle != right); p1 = left; p2 = left + last; end = p2 + size; do { swap = *p1; *p1++ = *p2; *p2++ = swap; } while (p2 != end); if (right - last > last) { stack_ptr -> base = last + size + left; stack_ptr++ -> bytes = right - last; stack_ptr -> base = left; stack_ptr++ -> bytes = last; } else { stack_ptr++ -> bytes = last; stack_ptr -> base = last + size + left; stack_ptr++ -> bytes = right - last; } } } while (stack_ptr != stack); } -- pete