Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | 88888 Dihedral <dihedral88888@googlemail.com> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Remving an element from Queue |
| Date | 2012-05-11 00:39 -0700 |
| Organization | http://groups.google.com |
| Message-ID | <20139213.396.1336721955573.JavaMail.geo-discussion-forums@pbbpp10> (permalink) |
| References | <4fa8d4c2$0$290$14726298@news.sunsite.dk> |
arnuld於 2012年5月8日星期二UTC+8下午4時09分39秒寫道:
> WANTED: Removing all elements matching a particular key from a Queue
> WHAT I GOT: Segfault
> WANTED: Segfaults in removeAllMatches(). I think routine/fucntion is too
> complex to be understood. removeAllMatches(arg1, arg2) is supposed to
> remove all elements from arg1 which match value of arg2.
>
>
>
>
> #include <stdio.h>
> #include <stdlib.h>
>
> struct myStruct
> {
> int num;
> struct myStruct* next;
> };
>
> struct myQueue
> {
> struct myStruct* head;
> struct myStruct* tail;
> };
>
>
> int enqueue(struct myQueue* q, int n);
> int dequeue(struct myQueue* q);
>
> struct myQueue* newQueue(void);
> void makeNull(struct myQueue* q);
> void removeAllMatches(struct myQueue* q, const int num);
>
> void printQueue(struct myQueue* q);
> void printStruct(struct myStruct* p);
>
>
>
> int main(void)
> {
> struct myQueue* q = newQueue();
> printQueue(q);
>
> enqueue(q, -1);
> enqueue(q, 100);
> enqueue(q, 38);
> enqueue(q, 100);
> enqueue(q, 38);
> enqueue(q, 9);
> enqueue(q, 100);
> printQueue(q);
>
> removeAllMatches(q, 100);
> printQueue(q);
> /* makeNull(q);
> printQueue(q); */
> free(q);
> q = NULL;
>
> return 0;
> }
>
>
>
> int enqueue(struct myQueue* q, int n)
> {
> int ret;
> if(NULL == q)
> {
> printf("Invalid Args\n");
> ret = -1;
> }
> else
> {
> struct myStruct* p = malloc(1 * sizeof *p);
> if(NULL == p)
> {
> printf("Out of memory\n");
> exit(EXIT_FAILURE);
> }
>
> p->num = n;
> p->next = NULL;
> if(NULL == q->tail && NULL == q->head)
> {
> printf("Empty Queue, Adding first element\n");
> q->head = q->tail = p;
> ret = 0;
> }
> else if(NULL == q->tail || NULL == q->head)
> {
> printf("IN: %s @%d; ", __func__, __LINE__);
> printf("There is something seriously wrong with head/tail
> assignment of your Queue\n");
> free(p);
> ret = 1;
> }
> else
> {
> printf("Adding Element\n");
> q->tail->next = p;
> q->tail = p;
> ret = 0;
> }
> }
>
> return ret;
> }
>
>
>
> int dequeue(struct myQueue* q)
> {
> int ret = -1;
> if(NULL == q)
> {
> printf("Invalid Args\n");
> ret = -1;
> }
> else
> {
> struct myStruct* h;
> if(NULL == q->head && NULL == q->tail)
> {
> printf("Queue is already empty\n");
> ret = 0;
> }
> else if(NULL == q->head || NULL == q->tail)
> {
> printf("IN: %s @%d; ", __func__, __LINE__);
> printf("There is something seriously wrong with head/tail
> assignment of your Queue\n");
> ret = 1;
> }
> else
> {
> h = q->head;
> q->head = q->head->next;
> free(h);
> if(NULL == q->head)
> {
> printf("Removing Last element left\n");
> q->head = q->tail = NULL;
> }
> else
> {
> printf("Removing Some element\n");
> }
> ret = 0;
> }
> }
>
> return ret;
> }
>
>
> /* Added many months later */
> void removeAllMatches(struct myQueue* q, const int num)
> {
> if(NULL == q)
> {
> printf("Invalid Args\n");
> return;
> }
>
> if(NULL == q->head && NULL == q->tail)
> {
> printf("Queue is already empty\n");
> }
> else if(NULL == q->head || NULL == q->tail)
> {
> printf("IN: %s @%d; ", __func__, __LINE__);
> printf("There is something seriously wrong with head/tail
> assignment of your Queue\n");
> exit(EXIT_FAILURE);
> }
> else
> {
> struct myStruct *p, *d, *h;
> p = d = h = NULL;
> for(h = q->head; h; h = h->next)
> {
> d = h;
> if(num == d->num)
> {
> if(num == q->head->num )
> {
> q->head = q->head->next;
> }
>
> if(num == q->tail->num)
> {
> q->tail = q->tail->next;
> }
>
> if(p)
> {
> p->next = h->next;
> }
> free(d);
> }
> else
> {
> p = h;
> }
> }
>
> }
> }
>
>
>
>
> void makeNull(struct myQueue* q)
> {
> if(q)
> {
> while(q->head) dequeue(q);
> }
> }
>
>
>
> struct myQueue* newQueue(void)
> {
> struct myQueue* p = malloc(1 * sizeof *p);
> if(NULL == p)
> {
> printf("Out of Memory\n");
> exit(EXIT_FAILURE);
> }
>
> return p;
> }
>
>
> void printQueue(struct myQueue* q)
> {
> if(q)
> {
> struct myStruct* p = q->head;
> for(; p; p = p->next) printStruct(p);
> printf("-----------------------------------------\n");
> }
> }
>
>
> void printStruct(struct myStruct* p)
> {
> if(p)
> {
> printf("title: %d\n", p->num);
> }
> }
>
> ======================== OUTPUT ==========================
> [arnuld@dune C]$ gcc -ansi -pedantic -Wall -Wextra Queue.c
> [arnuld@dune C]$ ./a.out
> -----------------------------------------
> Empty Queue, Adding first element
> Adding Element
> Adding Element
> Adding Element
> Adding Element
> Adding Element
> Adding Element
> title: -1
> title: 100
> title: 38
> title: 100
> title: 38
> title: 9
> title: 100
> -----------------------------------------
> Segmentation fault
> [arnuld@dune C]$
>
>
>
>
>
> --
> arnuld
> http://LispMachine.Wordpress.com
I checked your code that I assume it requires not only the standard
operations of a queue.
I suggest you should use a link list.
Back to comp.lang.c | Previous | Next — Previous in thread | Find similar | Unroll thread
Remving an element from Queue arnuld <sunrise@invalid.address> - 2012-05-08 08:09 +0000
Re: Remving an element from Queue Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-05-08 08:01 -0400
Re: Remving an element from Queue arnuld <sunrise@invalid.address> - 2012-05-09 05:43 +0000
Re: Remving an element from Queue Barry Schwarz <schwarzb@dqel.com> - 2012-05-08 23:21 -0700
Re: Remving an element from Queue arnuld <sunrise@invalid.address> - 2012-05-09 09:27 +0000
Re: Remving an element from Queue James Kuyper <jameskuyper@verizon.net> - 2012-05-09 07:54 -0400
Re: Remving an element from Queue arnuld <sunrise@invalid.address> - 2012-05-09 13:18 +0000
Re: Remving an element from Queue Barry Schwarz <schwarzb@dqel.com> - 2012-05-09 10:59 -0700
Re: Remving an element from Queue arnuld <arnuld.mizong@gmail.com> - 2012-05-10 05:26 -0700
Re: Remving an element from Queue nick_keighley_nospam@hotmail.com - 2012-05-11 00:11 -0700
Re: Remving an element from Queue Barry Schwarz <schwarzb@dqel.com> - 2012-05-14 15:53 -0700
Re: Remving an element from Queue lawrence.jones@siemens.com - 2012-05-15 17:42 -0400
Re: Remving an element from Queue Barry Schwarz <schwarzb@dqel.com> - 2012-05-15 16:40 -0700
Re: Remving an element from Queue James Kuyper <jameskuyper@verizon.net> - 2012-05-15 23:03 -0400
Re: Remving an element from Queue Barry Schwarz <schwarzb@dqel.com> - 2012-05-15 20:38 -0700
Re: Remving an element from Queue Jens Gustedt <jens.gustedt@loria.fr> - 2012-05-16 08:09 +0200
Re: Remving an element from Queue James Kuyper <jameskuyper@verizon.net> - 2012-05-16 07:18 -0400
Re: Remving an element from Queue Barry Schwarz <schwarzb@dqel.com> - 2012-05-16 16:30 -0700
Re: Remving an element from Queue James Kuyper <jameskuyper@verizon.net> - 2012-05-17 09:09 -0400
Re: Remving an element from Queue Barry Schwarz <schwarzb@dqel.com> - 2012-05-17 11:48 -0700
Re: Removing an element from Queue James Kuyper <jameskuyper@verizon.net> - 2012-05-17 15:38 -0400
Re: Removing an element from Queue Barry Schwarz <schwarzb@dqel.com> - 2012-05-17 15:21 -0700
Re: Removing an element from Queue James Kuyper <jameskuyper@verizon.net> - 2012-05-17 19:08 -0400
Re: Removing an element from Queue Tim Rentsch <txr@alumni.caltech.edu> - 2012-05-19 09:05 -0700
Re: Removing an element from Queue lawrence.jones@siemens.com - 2012-05-20 15:10 -0400
Re: Removing an element from Queue Tim Rentsch <txr@alumni.caltech.edu> - 2012-05-21 09:00 -0700
Re: Remving an element from Queue Tim Rentsch <txr@alumni.caltech.edu> - 2012-05-19 08:49 -0700
Re: Remving an element from Queue Tim Rentsch <txr@alumni.caltech.edu> - 2012-05-17 12:36 -0700
Re: Remving an element from Queue nick_keighley_nospam@hotmail.com - 2012-05-08 05:09 -0700
Re: Remving an element from Queue "BartC" <bc@freeuk.com> - 2012-05-08 14:03 +0100
Re: Remving an element from Queue nick_keighley_nospam@hotmail.com - 2012-05-09 00:01 -0700
Re: Remving an element from Queue nick_keighley_nospam@hotmail.com - 2012-05-09 00:03 -0700
Re: Remving an element from Queue "BartC" <bc@freeuk.com> - 2012-05-09 10:37 +0100
Re: Remving an element from Queue nick_keighley_nospam@hotmail.com - 2012-05-09 03:05 -0700
Re: Remving an element from Queue arnuld <sunrise@invalid.address> - 2012-05-09 09:44 +0000
Re: Remving an element from Queue Mark Bluemel <mark.bluemel@googlemail.com> - 2012-05-08 06:07 -0700
Re: Remving an element from Queue Mark Bluemel <mark.bluemel@googlemail.com> - 2012-05-08 06:35 -0700
Re: Remving an element from Queue Johann Klammer <klammerj@NOSPAM.a1.net> - 2012-05-08 20:50 +0200
Re: Remving an element from Queue 88888 Dihedral <dihedral88888@googlemail.com> - 2012-05-11 00:39 -0700
csiph-web