Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!216.196.98.146.MISMATCH!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: Fri, 22 Jun 2012 23:39:49 -0500 Message-ID: <4FE54893.4A6C@mindspring.com> Date: Sat, 23 Jun 2012 00:39:47 -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: dequeue() not working References: <4fe2c91e$0$289$14726298@news.sunsite.dk> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Lines: 414 X-Usenet-Provider: http://www.giganews.com NNTP-Posting-Host: 4.154.222.246 X-Trace: sv3-Of4X4faeyJnvU0jsdVz5gYkQmkTX7zlyFowfgwn7Eim+LL/8iiJdY/CzlmlR6wPHRCUhw/LT9odri9u!WVirCoueXYrhqgaWrSLbmhC3iCtGzb+bXvdTBI4Oo5ZZXGek06c3SBLssKnwmFKg0IXarmw6vrkd!65QhFUcRZnM= 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: 9939 Xref: csiph.com comp.lang.c:22573 arnuld wrote: > > WANTED: to dequeue an element from Queue. > GOT: nothing happening > > /* A queue implementation with the operations that I need: > * (1) Add an element to the front (head) of the queue > * (2) Remove an element from rear (tail) of the queue > * (2) remove an element with a unique ID (string constant) > * (3) compare 2 elements (string comparison) > * (4) print queue > * > * VERSION 0.0 > */ > > #include > #include > #include > > enum { SIZE_ID = 100, SIZE_ARR = 20, REPS = 3 }; > > struct myQ > { > char id[SIZE_ID+1]; > struct myQ* next; > }; > > struct myList > { > struct myQ* head; > struct myQ* tail; > }; > > struct myList* allocmyList(void); > int enqueue(struct myList* s, const char* id); > struct myQ* dequeue(struct myList** s); > void removeElement(struct myList* s, const char* id); > struct myQ* searchElement(struct myList* s, const char* is, struct myQ** > pr); > void deleteElement(struct myList* s, struct myQ* q, struct myQ* prev); > void printQ(struct myList* s); > /*void reverseQueue(struct myList** s); */ > > int main(void) > { > struct myList* s = allocmyList(); > int i; > for(i = 0; i < REPS; ++i) > { > int r; > char arrc[SIZE_ARR+1]; > r = sprintf(arrc, "%d", i); > if(0 > r) > { > printf("ERROR-SPRINF() = %d\n", r); > } > else > { > enqueue(s, arrc); > } > printf("s->head = %p, s->tail = %p\n", (void*)s->head, (void*)s- > >tail); > } > > printQ(s); > /* > for(i = 0; i < REPS; ++i) > { > int r; > char arrc[SIZE_ARR+1]; > r = sprintf(arrc, "%d", i); > if(0 > r) > { > printf("ERROR-SPRINF() = %d\n", r); > } > else > { > removeElement(s, arrc); > } > } > */ > dequeue(&s); > printQ(s); > free(s); > > return 0; > } > > int enqueue(struct myList* s, const char* id) > { > struct myQ *p; > if(NULL == s || NULL == id) return -1; > p = malloc(1 * (sizeof *p)); > if(NULL == p) return -1; > strcpy(p->id, id); > > if(s->head) > { > p->next = s->head; > s->head = p; > } > else > { > s->head = s->tail = p; > } > > return 1; > } > > struct myQ* dequeue(struct myList** s) > { > struct myQ *temp, *prev; > if(NULL == s || NULL == *s) return 0; > if(NULL == (*s)->head) return 0; > prev = (*s)->head; > temp = (*s)->head; > if(NULL == (*s)->head->next) > { > struct myQ* retp = (*s)->head; > (*s)->head = (*s)->tail = NULL; > return retp; > } > > while(temp->next) > { > prev = temp; > temp = temp->next; > } > > (*s)->tail = prev; > > return temp; > } > > > void removeElement(struct myList* s, const char* id) > { > struct myQ *p, *prev; > if(NULL == s || NULL == id) return; > p = searchElement(s, id, &prev); > if(p) > { > deleteElement(s, p, prev); > } > } > > struct myQ* searchElement(struct myList* s, const char* id, struct myQ** > pr) > { > struct myQ *prev, *cur; > prev = s->head; > for(cur = s->head; cur; cur = cur->next) > { > if(0 == strcmp(cur->id, id)) { *pr = prev; return cur;} > else prev = cur; > } > > return NULL; > } > > void deleteElement(struct myList* s, struct myQ* q, struct myQ* prev) > { > if(0 == strcmp(s->head->id, q->id)) /* removing head */ > { > s->head = q->next; > free(q); > } > else > { > prev->next = q->next; > free(q); > } > } > > void printQ(struct myList* s) > { > if(s) > { > struct myQ* t; > for(t = s->head; t; t = t->next) printf("ID = %s\n", t->id); > } > else > { > printf("Nothing to print\n"); > } > > printf("--------------------------------\n"); > } > > > struct myList* allocmyList(void) > { > struct myList* p = malloc(1 * (sizeof *p)); > if(NULL == p) exit(EXIT_FAILURE); > p->head = NULL; > p->tail = NULL; > return p; > } > > /* -------------------------------------- > void reverseQueue(struct myList** s) > { > return; > } > */ > > ========================= OUTPUT ============================= > /home/arnuld/programs/C $ gcc -ansi -pedantic -Wall -Wextra queue.c > /home/arnuld/programs/C $ ./a.out > s->head = 0x804a018, s->tail = 0x804a018 > s->head = 0x804a088, s->tail = 0x804a018 > s->head = 0x804a0f8, s->tail = 0x804a018 > ID = 2 > ID = 1 > ID = 0 > -------------------------------- > ID = 2 > ID = 1 > ID = 0 > -------------------------------- > /home/arnuld/programs/C $ #include #include #include enum { SIZE_ID = 100, SIZE_ARR = 20, REPS = 7 }; struct myQ { struct myQ *next; char id[SIZE_ID + 1]; }; struct myList { struct myQ *head; struct myQ *tail; }; struct myQ *enqueue(struct myList *s, const char *id); void printQ(struct myList *s); void dequeue(struct myList *s); struct myQ *searchElement(struct myList *s, const char *is); void removeElement(struct myList *s, struct myQ *p); void reverseQueue(struct myList *s); void freequeue(struct myList *s); int main(void) { struct myList Mylist = {0}; struct myList *s = &Mylist; int i; int r; struct myQ *temp; char arrc[SIZE_ARR + 1]; for (i = 0; i < REPS; ++i) { r = sprintf(arrc, "%d", i); if (0 > r) { printf("ERROR-SPRINF() = %d\n", r); } else { temp = enqueue(s, arrc); if (temp == NULL) { puts("\n\nenqueue(s, arrc) == NULL\n\n"); freequeue(s); break; } s -> head = temp; if (s -> tail == NULL) { s -> tail = s -> head; } } printf("s->head = %p, s->tail = %p\n", (void*)s -> head, (void*)s -> tail); } printQ(s); dequeue(s); printQ(s); dequeue(s); printQ(s); temp = searchElement(s, "3"); if (temp != NULL) { puts(temp -> id); removeElement(s, temp); printQ(s); } temp = searchElement(s, "47"); if (temp != NULL) { puts(temp -> id); removeElement(s, temp); printQ(s); } reverseQueue(s); printQ(s); reverseQueue(s); printQ(s); freequeue(s); printQ(s); return 0; } struct myQ * enqueue(struct myList *s, const char* id) { struct myQ *p; p = malloc(sizeof *p); if (p != NULL) { p -> next = s -> head; strcpy(p -> id, id); } return p; } void printQ(struct myList *s) { if (s != NULL) { struct myQ *t; for (t = s -> head; t != NULL; t = t -> next) { printf("ID = %s\n", t -> id); } } else { puts("Nothing to print"); } puts("--------------------------------"); } void dequeue(struct myList *s) { struct myQ *cur; for (cur = s -> head; cur != NULL; cur = cur -> next) { if (cur -> next == s -> tail) { break; } } if (cur != NULL) { cur -> next = NULL; } free(s -> tail); s -> tail = cur; } struct myQ * searchElement(struct myList *s, const char *id) { struct myQ *prev, *cur; prev = s -> head; for (cur = s -> head; cur; cur = cur -> next) { if (0 == strcmp(cur -> id, id)) { break; } } return cur; } void removeElement(struct myList *s, struct myQ *p) { struct myQ *prev, *cur; prev = s -> head; for (cur = s -> head; cur; cur = cur->next) { if (p == cur) { break; } prev = cur; } if (cur != NULL) { prev -> next = cur -> next; free(cur); } } void reverseQueue(struct myList *s) { struct myQ *head, *next_node, *previous; head = s -> tail = s -> head; if (head != NULL) { next_node = head -> next; head -> next = NULL; while (next_node != NULL) { previous = next_node -> next; next_node -> next = head; head = next_node; next_node = previous; } } s -> head = head; } void freequeue(struct myList *s) { struct myQ *next, *cur; for (cur = s -> head; cur != NULL; cur = next) { next = cur -> next; free(cur); } s -> head = s -> tail = NULL; } -- pete