Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #define EMPTY -1
- struct node {
- int data;
- struct node* next;
- };
- struct queue {
- struct node* front;
- struct node* back;
- };
- void init(struct queue* qPtr);
- int enqueue(struct queue* qPtr, int val);
- int dequeue(struct queue* qPtr);
- int empty(struct queue* qPtr);
- int front(struct queue* qPtr);
- int main(void) {
- struct queue* MyQuePtr = (struct queue *)malloc(sizeof(struct queue));
- init(MyQuePtr);
- enqueue(MyQuePtr, 3);
- enqueue(MyQuePtr, 5);
- enqueue(MyQuePtr, 7);
- printf("\n dequeue %d ", dequeue(MyQuePtr));
- printf("\n front of the queue %d", front(MyQuePtr));
- printf("\n dequeue %d ", dequeue(MyQuePtr));
- printf("\n dequeue %d ", dequeue(MyQuePtr));
- printf("\n is empty %d ", empty(MyQuePtr));
- return 0;
- }
- void init(struct queue* qPtr) {
- qPtr->front = NULL;
- qPtr->back = NULL;
- }
- int enqueue(struct queue* qPtr, int val) {
- struct node* temp;
- temp = (struct node*)malloc(sizeof(struct node));
- if(temp != NULL) {
- temp->data = val;
- temp->next = NULL;
- if(qPtr->back != NULL)
- qPtr->back->next = temp;
- qPtr->back = temp;
- if(qPtr->front == NULL)
- qPtr->front = temp;
- return 1;
- }
- else
- return 0;
- }
- int dequeue(struct queue* qPtr) {
- struct node * temp;
- int retval;
- if(qPtr->front == NULL)
- return EMPTY;
- retval = qPtr->front->data;
- temp = qPtr->front;
- qPtr->front = qPtr->front->next;
- if(qPtr->front == NULL)
- qPtr->back = NULL;
- free(temp);
- return retval;
- }
- int empty(struct queue* qPtr) {
- return qPtr->front == NULL;
- }
- int front(struct queue* qPtr) {
- if(qPtr->front != NULL)
- return qPtr->front->data;
- else
- return EMPTY;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement