Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #22460 > unrolled thread

dequeue() not working

Started byarnuld <sunrise@invalid.address>
First post2012-06-21 07:11 +0000
Last post2012-06-23 00:39 -0400
Articles 11 — 8 participants

Back to article view | Back to comp.lang.c


Contents

  dequeue() not working arnuld <sunrise@invalid.address> - 2012-06-21 07:11 +0000
    Re: dequeue() not working Ike Naar <ike@iceland.freeshell.org> - 2012-06-21 08:37 +0000
      Re: dequeue() not working Ike Naar <ike@iceland.freeshell.org> - 2012-06-21 08:42 +0000
        Re: dequeue() not working Ike Naar <ike@iceland.freeshell.org> - 2012-06-21 08:43 +0000
    Re: dequeue() not working Johann Klammer <klammerj@NOSPAM.a1.net> - 2012-06-21 11:09 +0200
      Re: dequeue() not working jgk@panix.com (Joe keane) - 2012-06-22 23:23 +0000
        Re: dequeue() not working Martin Shobe <martin.shobe@yahoo.com> - 2012-06-23 00:09 +0000
        Re: dequeue() not working Heikki Kallasjoki <fis+usenet@zem.fi> - 2012-06-23 00:10 +0000
        What's in a name? Johann Klammer <klammerj@NOSPAM.a1.net> - 2012-06-23 12:19 +0200
    Re: dequeue() not working Neil Cerutti <neilc@norwich.edu> - 2012-06-21 12:38 +0000
    Re: dequeue() not working pete <pfiland@mindspring.com> - 2012-06-23 00:39 -0400

#22460 — dequeue() not working

Fromarnuld <sunrise@invalid.address>
Date2012-06-21 07:11 +0000
Subjectdequeue() not working
Message-ID<4fe2c91e$0$289$14726298@news.sunsite.dk>
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 <stdio.h>
#include <stdlib.h>
#include <string.h>

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 $ 


-- 
 arnuld
http://LispMachine.Wordpress.com

[toc] | [next] | [standalone]


#22464

FromIke Naar <ike@iceland.freeshell.org>
Date2012-06-21 08:37 +0000
Message-ID<slrn3vfsju5nah.m6r.ike@iceland.freeshell.org>
In reply to#22460
On 2012-06-21, arnuld <sunrise@invalid.address> wrote:
> 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;

Didn't you forget to set prev->next to NULL here?

>
>   return temp;
> }

The code above could be simplified in a couple of ways.

First, you can change the type of s to struct mylist*
and replace every (*s) by s.
(and of course, change the call dequeue(&s) to dequeue(s)).

Second, the tail pointer in a struct MyList does not add
much value, so you might as well get rid of it. The last
element in a list is already characterized by element->next
being NULL, and it is that property that you use everywhere.

[toc] | [prev] | [next] | [standalone]


#22467

FromIke Naar <ike@iceland.freeshell.org>
Date2012-06-21 08:42 +0000
Message-ID<slrn3vfsju5nkg.m6r.ike@iceland.freeshell.org>
In reply to#22464
On 2012-06-21, Ike Naar <ike@iceland.freeshell.org> wrote:
> First, you can change the type of s to struct mylist*

Typo, "myList" should be "myList".

> Second, the tail pointer in a struct MyList does not add

Typo, "MyList" should be "myList".

[toc] | [prev] | [next] | [standalone]


#22468

FromIke Naar <ike@iceland.freeshell.org>
Date2012-06-21 08:43 +0000
Message-ID<slrn3vfsju5nmf.m6r.ike@iceland.freeshell.org>
In reply to#22467
On 2012-06-21, Ike Naar <ike@iceland.freeshell.org> wrote:
> On 2012-06-21, Ike Naar <ike@iceland.freeshell.org> wrote:
>> First, you can change the type of s to struct mylist*
>
> Typo, "myList" should be "myList".

Okay, you get the point ;-)

[toc] | [prev] | [next] | [standalone]


#22470

FromJohann Klammer <klammerj@NOSPAM.a1.net>
Date2012-06-21 11:09 +0200
Message-ID<4fe2e4e6$0$1586$91cee783@newsreader04.highway.telekom.at>
In reply to#22460
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

Do it the other way around and add to the end/remove from the start.
It should be quicker and simpler (you have a next pointer to use for head).

>   *  (2) remove an element with a unique ID (string constant)
>   *  (3) compare 2 elements (string comparison)
>   *  (4) print queue
>   *
>   * VERSION 0.0
>   */
>

[toc] | [prev] | [next] | [standalone]


#22564

Fromjgk@panix.com (Joe keane)
Date2012-06-22 23:23 +0000
Message-ID<js2uos$df7$1@reader1.panix.com>
In reply to#22470
In article <4fe2e4e6$0$1586$91cee783@newsreader04.highway.telekom.at>,
Johann Klammer  <klammerj@NOSPAM.a1.net> wrote:
>Do it the other way around and add to the end/remove from the start.
>It should be quicker and simpler (you have a next pointer to use for head).

question

What do you call three-quarters of a deque?

e.g.

add to front
add to back
remove from front

[toc] | [prev] | [next] | [standalone]


#22565

FromMartin Shobe <martin.shobe@yahoo.com>
Date2012-06-23 00:09 +0000
Message-ID<js31f4$m88$1@dont-email.me>
In reply to#22564
Joe keane wrote:

> In article <4fe2e4e6$0$1586$91cee783@newsreader04.highway.telekom.at>,
> Johann Klammer  <klammerj@NOSPAM.a1.net> wrote:
>>Do it the other way around and add to the end/remove from the start.
>>It should be quicker and simpler (you have a next pointer to use for head).
>
> question
>
> What do you call three-quarters of a deque?
>
> e.g.
>
> add to front
> add to back
> remove from front

output-restricted deque.

Martin Shobe

[toc] | [prev] | [next] | [standalone]


#22566

FromHeikki Kallasjoki <fis+usenet@zem.fi>
Date2012-06-23 00:10 +0000
Message-ID<slrnjua2b5.3a8.fis@iris.zem.fi>
In reply to#22564
On 2012-06-22, Joe keane <jgk@panix.com> wrote:
> What do you call three-quarters of a deque?

A seque?  (Short for sesquible-ended queue.)


-- 
Heikki Kallasjoki

[toc] | [prev] | [next] | [standalone]


#22578 — What's in a name?

FromJohann Klammer <klammerj@NOSPAM.a1.net>
Date2012-06-23 12:19 +0200
SubjectWhat's in a name?
Message-ID<4fe59830$0$1582$91cee783@newsreader04.highway.telekom.at>
In reply to#22564
Joe keane wrote:
> In article<4fe2e4e6$0$1586$91cee783@newsreader04.highway.telekom.at>,
> Johann Klammer<klammerj@NOSPAM.a1.net>  wrote:
>> Do it the other way around and add to the end/remove from the start.
>> It should be quicker and simpler (you have a next pointer to use for head).
>
> question
>
> What do you call three-quarters of a deque?
>
> e.g.
>
> add to front
> add to back
> remove from front


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
  */

note: the remove from front is missing.

de-queue versus deque [pronounced deck]

[toc] | [prev] | [next] | [standalone]


#22480

FromNeil Cerutti <neilc@norwich.edu>
Date2012-06-21 12:38 +0000
Message-ID<a4giuaF4g8U2@mid.individual.net>
In reply to#22460
On 2012-06-21, arnuld <sunrise@invalid.address> 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 <stdio.h>
> #include <stdlib.h>
> #include <string.h>
>
> 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;
> };

Implementing linked lists in C may be like walking for most of
the people here, but I think you will benefit by writing down
some notes here.

Along with the struct write down the invariants for the
structures.

For example:

struct myQ
{
  char id[SIZE_ID+1]; # null-terminated string.
  struct myQ* next; # pointer to the next element or NULL.
};

struct myList
{
  struct myQ* head; # pointer to first item in list or NULL
  struct myQ* tail; # pointer to last item  in list or NULL
};
>
>
>
> 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;

This type of error checking is the equivalent of crotchless
underpants. Why bother? Anybody careless enough to pass NULL as
either of those arguments will not be checking the return value.

>   p = malloc(1 * (sizeof *p));
>   if(NULL == p) return -1;
>   strcpy(p->id, id);

You must use strncpy with SIZE_ID here to protect against
overflow of p->id.

>   if(s->head)
>     {
>       p->next = s->head;
>       s->head = p;
>     }
>   else
>     {
>       s->head = s->tail = p;

Checking invariants, in this branch p->next is left undefined.

>     }
>   return 1;
> }

I suggest paring down the interface to a minimal singly-linked
list with head and tail pointers, with perhaps an apply function
to allow you to do some tests.

The interface is confusing.

For example, your search function:

> struct myQ* searchElement(struct myList* s, const char* id, struct myQ** 
> pr)

What is pr supposed to be?

dequeue and enqueue are function names without obvious meanings.
push_front and push_back along with pop_front and pop_back would
be easier to comprehend.

-- 
Neil Cerutti

[toc] | [prev] | [next] | [standalone]


#22573

Frompete <pfiland@mindspring.com>
Date2012-06-23 00:39 -0400
Message-ID<4FE54893.4A6C@mindspring.com>
In reply to#22460
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 <stdio.h>
> #include <stdlib.h>
> #include <string.h>
> 
> 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 <stdio.h>
#include <stdlib.h>
#include <string.h>

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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c


csiph-web