pandit said:
struct node
{
char c;
struct node* next;
};
struct queue
{
struct node* head;
struct node* tail;
};
#define QueueInterface(scope,T) \
typedef struct Queue##T *Queue##T; \
struct Queue##T {T value; Queue##T next, last;}; \
\
scope Queue##T null##T; \
\
scope Queue##T push##T(Queue##T q, T value); \
scope Queue##T append##T(Queue##T q, T value); \
\
scope Queue##T pop##T(Queue##T q); \
scope Queue##T next##T(Queue##T q); \
scope T top##T(Queue##T q,T value); \
\
scope _Bool empty##T(Queue##T q); \
\
scope Queue##T reverse##T(Queue##T q); \
\
scope void printqueue##T(Queue##T q); \
scope void printnode##T(Queue##T q); \
\
scope Queue##T search##T(Queue##T q, T seek); \
scope Queue##T deleteFrom(Queue##T q, T seek); \
scope queue##T insertAfter##T(Queue##T q, T seek, T value); \
scope Queue##T insertBefore(Queue##T q, T seek, T value);
#define QueueImplementation(scope,T,MALLOC,FREE,PRINT,EQ) \
scope Queue##T null##T = 0; \
\
scope Queue##T push##T(Queue##T q, T value) { \
Queue##T c = MALLOC(sizeof(struct Queue##T)); \
if (!c) return null##T; \
c->value = value; c->next = q; c->last = c; \
return c; \
} \
scope Queue##T append##T(Queue##T q, T value) { \
Queue##T c = MALLOC(sizeof(struct Queue##T)); \
if (!c) return null##T; \
c->value = value; c->next = q; c->last = c; \
if (q) { \
for (Queue##T p=q->last; p->next; p=p->next); \
q->last = p->next = c; \
}else \
q = c; \
return q; \
} \
\
scope Queue##T pop##T(Queue##T q) { \
if (q) {Queue##T c = q; q = q->next; FREE(c);} \
return q; \
} \
scope Queue##T next##T(Queue##T q) { \
return q ? q->next : null##T; \
} \
scope T top##T(Queue##T q,T value) { \
if (q) return q->value; \
else {T zero; memset(&T,sizeof T,0); return zero;} \
} \
\
scope _Bool empty##T(Queue##T q) { \
return !q; \
} \
\
scope Queue##T reverse##T(Queue##T q) { \
Queue##T prev = null##T; \
for (Queue##T curr=q, next=null##T; !empty##T(curr); prev=curr,
curr=next) { \
next = curr->next; curr->next = prev; curr->last = q; \
} \
return prev; \
} \
\
scope void printqueue##T(Queue##T q) { \
if (q) { \
for (; !empty##T(q); q=next##T(q)) { \
printf("Element = "); PRINT(top##T(q)); printf("\n"); \
} \
printf("--------------- EoQ -----------\n\n"); \
} \
} \
scope void printnode##T(Queue##T q) { \
if (q) { \
printf("Node = "); PRINT(top##T(q)); printf("\n"); \
} \
} \
\
scope Queue##T search##T(Queue##T q, T seek) { \
for (; !empty##T(q); q=next##T(q)) \
if (EQ(top##T(q), seek)) \
return q; \
return null##T; \
} \
scope Queue##T deleteFrom(Queue##T q, T seek) { \
Queue##T prev = null##T; \
for (Queue##T curr=q; empty##T(curr); prev=curr, curr=curr->next) \
if (!EQ(top##T(curr), seek)); \
else if (empty##T(prev)) {return pop##T(q);} \
else {prev->next = curr->next; FREE(curr); return q;} \
return null##T; \
} \
scope queue##T insertAfter##T(Queue##T q, T seek, T value) { \
for (Queue##T curr=q; empty##T(curr); curr=curr->next) \
if (EQ(top##T(curr), seek)) { \
Queue##T c = push##T(curr->next, value); \
if (!c) return null##T; \
else {curr->next = c; return q;} \
} \
return null##T; \
} \
scope Queue##T insertBefore(Queue##T q, T seek, T value) { \
Queue##T prev = null##T; \
for (Queue##T curr=q; empty##T(curr); prev=curr, curr=curr->next) \
if (EQ(top##T(curr), seek)) { \
Queue##T c = push##T(curr, value); \
if (!c) return null##T; \
else if (!prev) return c; \
else {prev->next = c; return q;} \
} \
return null##T; \
}