A
arnuld
AIM: To write a queue with 3 operations.
WHAT I GOT: It works
PROBLEM: Have a question: Why do "pointer to pointer" in enqueue() and "a
pointer" to deleteElement() both work fine ? Will enqueue() work fine if
I pass just "a pointer" to it ? Will deleteElement() work fine if I pass
"pointer to pointer" ?
/* A queue implementataion with the operations that I need:
* (1) Add element to the front of the queue
* (2) remove an element with a unique ID (string constant)
* (3) compare 2 elements (string comparison)
* (4) print queue
*
* VERSION 0.1
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
enum { SIZE_ID = 100 };
struct myQ
{
char id[SIZE_ID+1];
struct myQ* next;
};
struct myList
{
struct myQ* head;
};
int enqueue(struct myList** s, const char* id);
void deleteElement(struct myList* s, const char* id);
int compareElements(struct myQ* e1, struct myQ* e2);
void printQ(struct myList* s);
int main(void)
{
struct myList* s = malloc(1 * (sizeof *s));
if(NULL == s) exit(EXIT_FAILURE);
enqueue(&s, "1");
enqueue(&s, "2");enqueue(&s, "3");
printQ(s);
deleteElement(s, "3");
printQ(s);
deleteElement(s, "2");
printQ(s);
deleteElement(s, "1");
printQ(s);
free(s);
return 0;
}
int enqueue(struct myList** s, const char* id)
{
struct myQ *p, *h;
if(NULL == s || NULL == id) return -1;
p = malloc(1 * (sizeof *p));
if(NULL == p) return -1;
strcpy(p->id, id);
p->next = NULL;
h = (*s)->head;
(*s)->head = p;
p->next = h;
return 1;
}
void deleteElement(struct myList* s, const char* id)
{
struct myQ *prev, *curr;
if(NULL == s || NULL == id) return;
prev = s->head;
for(curr = s->head; curr; curr = curr->next)
{
if(0 == strcmp(curr->id, id))
{
prev->next = curr->next;
if(0 == strcmp(s->head->id, curr->id)) s->head = curr->next;
free(curr);
break;
}
else
{
prev = curr;
}
}
}
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");
}
====================== OUTPUT ========================
/home/arnuld/programs/C $ gcc -ansi -pedantic -Wall -Wextra queue.c
/home/arnuld/programs/C $ ./a.out
ID = 3
ID = 2
ID = 1
WHAT I GOT: It works
PROBLEM: Have a question: Why do "pointer to pointer" in enqueue() and "a
pointer" to deleteElement() both work fine ? Will enqueue() work fine if
I pass just "a pointer" to it ? Will deleteElement() work fine if I pass
"pointer to pointer" ?
/* A queue implementataion with the operations that I need:
* (1) Add element to the front of the queue
* (2) remove an element with a unique ID (string constant)
* (3) compare 2 elements (string comparison)
* (4) print queue
*
* VERSION 0.1
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
enum { SIZE_ID = 100 };
struct myQ
{
char id[SIZE_ID+1];
struct myQ* next;
};
struct myList
{
struct myQ* head;
};
int enqueue(struct myList** s, const char* id);
void deleteElement(struct myList* s, const char* id);
int compareElements(struct myQ* e1, struct myQ* e2);
void printQ(struct myList* s);
int main(void)
{
struct myList* s = malloc(1 * (sizeof *s));
if(NULL == s) exit(EXIT_FAILURE);
enqueue(&s, "1");
enqueue(&s, "2");enqueue(&s, "3");
printQ(s);
deleteElement(s, "3");
printQ(s);
deleteElement(s, "2");
printQ(s);
deleteElement(s, "1");
printQ(s);
free(s);
return 0;
}
int enqueue(struct myList** s, const char* id)
{
struct myQ *p, *h;
if(NULL == s || NULL == id) return -1;
p = malloc(1 * (sizeof *p));
if(NULL == p) return -1;
strcpy(p->id, id);
p->next = NULL;
h = (*s)->head;
(*s)->head = p;
p->next = h;
return 1;
}
void deleteElement(struct myList* s, const char* id)
{
struct myQ *prev, *curr;
if(NULL == s || NULL == id) return;
prev = s->head;
for(curr = s->head; curr; curr = curr->next)
{
if(0 == strcmp(curr->id, id))
{
prev->next = curr->next;
if(0 == strcmp(s->head->id, curr->id)) s->head = curr->next;
free(curr);
break;
}
else
{
prev = curr;
}
}
}
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");
}
====================== OUTPUT ========================
/home/arnuld/programs/C $ gcc -ansi -pedantic -Wall -Wextra queue.c
/home/arnuld/programs/C $ ./a.out
ID = 3
ID = 2
ID = 1