I need details comments for each line ... Thank you. The code: (Programming Language is C): #define MAXELEMENTS 5 #define MIN 0 struct queue { int items[MAXELEMENTS]; int front, rear; }; struct node { int data; struct node *next; }; typedef struct node *NODEPTR; void cqinsert(struct queue*,int); int cqdelete(struct queue*); void insert(node *head,int); int main(void) { struct queue q; int x; q.front= q.rear=-1; for(int i=0;i<5;i++) { printf("Enter input Number \n"); scanf("%d",&x); cqinsert(&q,x); } x=cqdelete(&q); NODEPTR head=(NODEPTR)malloc(sizeof(struct node)); head->data=x; head->next=NULL; for(int i=0;i<5;i++) { x=cqdelete (&q); insert(head, x); } NODEPTR save = head; do { printf("%d\n",save->data); save-save->next; } while(save!=NULL); return 0; }//end of main int cqdelete(struct queue *pq) { if(pq->front==-1) { printf("\nQueue Underflow"); exit(1); } int pq->items[pq->front]; if(pq->front==pq->rear)/*queue has only one element*/ { pq->front =-1; pq->rear =-1; } else if(pq->==MAXELEMENTS-1) pq->front=0; else pq->front=pq->front+1; return(item); } void cqinsert(struct queue *pq,int x) { if((pq->front==0 && pq->rear==MAXELEMENTS-1)||(front==pq->rear+1)) { printf("\nQueue Overflow \n"); //exit(0); return; } if(pq->front==-1) /*queue is empty*/ { pq->front=MIN; pq->rear=MIN; } else if(pq->rear==MAXELEMENTS-1) /*rear is at last position of queue*/ pq->rear=0; else pq->rear=pq->rear+1; pq->items[rear]=x; } void insert(node*head,int x){ NODEPTR p,q; q=NULL; for(p=head:p!=NULL && x>p->data;p+p->next) q=p; if(q==NULL) /*insert x at the head of the list*/ { p=(NODEPTR)malloc(sizeof(struct node)); p->data=x; p->next=head; head=p; } else{ p=(NODEPTR)malloc(sizeof(struct node)); p->data=x; p->next=q->next; q->next=p; } }
This code for:
1) Add five integer values into a queue.
2) Delete the values from the queue then store the popped values in a sorted linked list and print it on the screen
I need details comments for each line ... Thank you.
The code: (Programming Language is C):
#define MAXELEMENTS 5
#define MIN 0
struct queue {
int items[MAXELEMENTS];
int front, rear;
};
struct node {
int data;
struct node *next;
};
typedef struct node *NODEPTR;
void cqinsert(struct queue*,int);
int cqdelete(struct queue*);
void insert(node *head,int);
int main(void) {
struct queue q;
int x;
q.front= q.rear=-1;
for(int i=0;i<5;i++)
{ printf("Enter input Number \n");
scanf("%d",&x);
cqinsert(&q,x);
}
x=cqdelete(&q);
NODEPTR head=(NODEPTR)malloc(sizeof(struct node));
head->data=x;
head->next=NULL;
for(int i=0;i<5;i++)
{ x=cqdelete (&q);
insert(head, x);
}
NODEPTR save = head;
do { printf("%d\n",save->data);
save-save->next;
} while(save!=NULL);
return 0;
}//end of main
int cqdelete(struct queue *pq)
{ if(pq->front==-1)
{ printf("\nQueue Underflow");
exit(1);
}
int pq->items[pq->front];
if(pq->front==pq->rear)/*queue has only one
element*/
{ pq->front =-1;
pq->rear =-1;
}
else
if(pq->==MAXELEMENTS-1)
pq->front=0;
else
pq->front=pq->front+1;
return(item);
}
void cqinsert(struct queue *pq,int x)
{
if((pq->front==0 && pq->rear==MAXELEMENTS-1)||(front==pq->rear+1))
{
printf("\nQueue Overflow \n");
//exit(0);
return;
}
if(pq->front==-1) /*queue is empty*/
{
pq->front=MIN;
pq->rear=MIN;
}
else
if(pq->rear==MAXELEMENTS-1) /*rear is at last position of queue*/
pq->rear=0;
else
pq->rear=pq->rear+1;
pq->items[rear]=x;
}
void insert(node*head,int x){
NODEPTR p,q;
q=NULL;
for(p=head:p!=NULL && x>p->data;p+p->next)
q=p;
if(q==NULL) /*insert x at the head of the list*/
{
p=(NODEPTR)malloc(sizeof(struct node));
p->data=x;
p->next=head;
head=p;
}
else{
p=(NODEPTR)malloc(sizeof(struct node));
p->data=x;
p->next=q->next;
q->next=p;
}
}
Step by step
Solved in 2 steps