#include #include #include #include /*--------- STRUCTURE DECLARATION ----------*/ struct node { int val; struct node* next; }; typedef struct node NODE; struct queue { NODE *front; NODE *rear; }; typedef struct queue QUEUE; /*---------------------- FUNCTIONS -----------------------*/ NODE* newnode(void) { NODE* pnode; pnode = (NODE*)malloc(sizeof(NODE)); if(pnode!=NULL) pnode->next = NULL; //(*pnode).next=NULL; return pnode; } /*--------------------------------------------------------*/ QUEUE* createqueue(void) { QUEUE* q = (QUEUE*)malloc(sizeof(QUEUE)); q->front= NULL; q->rear = NULL; return q; } /*-------------- INSERT A VALUE IN THE END OF QUEUE--------*/ void enqueue(QUEUE *q, int val) { NODE* nnode = newnode(); nnode->val = val; if (q->front == NULL) { q->front = nnode; q->rear = nnode; } else { q->rear->next = nnode; q->rear = nnode; } } /*---------- REMOVE THE FIRST VALUE OF QUEUE -----------*/ int dequeue(QUEUE* q) { NODE *t=q->front; int val = t->val; q->front = q->front->next; free(t); return val; } /*--------------------------------------------------------*/ int peek(QUEUE *q) { return q->front->val; } /*--------------------------------------------------------*/ void displayqueue(QUEUE *queue) { NODE* pnode; pnode = queue->front; if (pnode == NULL) printf("The queue is empty\n"); else while(pnode!=NULL) { printf("%u (%d) ->%u\n", (int)pnode, pnode->val, (int)pnode->next); pnode = pnode->next; } } /*---------------- FIND A NODE INCLUDING A SPECIFIC VALUE ---------*/ NODE* findnode(QUEUE *q, int val) { NODE* pnode = q->front; while (pnode) { if (val == pnode->val) break; pnode = pnode->next; } return pnode; } /*--------PRESS ENTER-------*/ void press_enter() { printf("Press ENTER to continue"); getchar(); while (getchar() != '\n'); } /*------------------------------------------------------------*/ int main(int argc, char* argv[]) { QUEUE *queue=createqueue(); int key, val; NODE* pnode; do { system("cls"); printf("1.Enqueue\n"); printf("2.Dequeue\n"); printf("3.Search node\n"); printf("4.Print Queue\n"); printf("5.Peek\n"); printf("0.Exit\n"); key = getchar(); switch (key) { case '1':printf("val:"); scanf("%i", &val); enqueue(queue, val); break; case '2':printf("Value %d removed from the front of queue\n", dequeue(queue)); break; case '3':printf("val:"); scanf("%i", &val); pnode = findnode(queue, val); if (pnode)printf("%i found in address %u\n", val, (int)pnode); else printf("Value not found\n"); break; case '4':displayqueue(queue); break; case '5':printf("Val %i on the top of queue\n",peek(queue)); break; case '0':break; }; press_enter(); } while (key != '0'); return 1; }