//QUEUE IS WORKS ON FIFO ALGORITHM....MEANS FIRST IN FIRST OUT LOGIC....//
//MEANS PUT THE VALUES FROM UP & PICK THE VALUES FROM DOWN..& SHIFT ALL OTHER VALUES TO DOWN ..MEANS THING OF PLACE 1 TO 0 2 TO 1; 3 TO 2; 4 TO 3 ; ETC....&THEN AGAIN PICK THE NEXT VALUE FROM 0 POSITION AGAIN .....//
//THIS IS JUST LIKE NO. OF PERSONS ARE STANDING INTO QUEUE BEFORE COUNTER TO GET TICKETS...PERSON WHO CAME 1ST WILL BE SERVED 1ST..WHEN 1 PERSON IS SERVED .2ND PERSON WILL TAKE POSITION OF POSITION OF THAT PERSON WHICH IS SERVED BEFORE..& ALSO ALL THE PERSONS IN QUEUE WILL SHIFT THERE PODITION BY MOVING 1 STEP FORWARD ...IN THE SAME WAY ...OUR QUEUE WILL WORK..//
#include<stdio.h>
#include<stdlib.h>
void push(int*);
void pop(int*);
int* create_queue();
int ov=0,count=0;
void pop(int* fnew)//POP OR PICK THE VALUES & MOVE ALL OTHER VALUES TO TOP...0 POSITION IS REPRESENTING THE TOP POSITION ..//
{
if(ov>0)//POP THE VALUE FROM QUEUE IF U HAVE PUSHED ANY VALUE INTO QUEUE...MEANS IF THERE IS SOME VALUE INTO QUEUE...MEANS OV >0 ...
{
int s=0;
//ov--;
//count=ov;
if(count<ov)//count=0 always...CHECK if ...count<ov...ov--....pick the values from queue untill ov becomes 0......means from ov=1 to max. +ve value upto which it hAS PUSHED VALUES TO POINTER...
{
printf("*(fnew+%d)=%d\n",count,*(fnew+count));//ONE CALL OF FUNCTION WILL TEND TO PICK ONLY 1 VALUE ....THAT VALUE SHOULD BE PICKED FROM THE TOP..AGAIN FROM ...0TH POSITION...
ov--;//IF 1 VALUE IS PICKED ...OV-- MEANTS...
}
s=ov;//NOW AFTER PICKING 1 VALUE...I WILL SHIFT ALL OTHER VALUES TOWARDS THE TOP ...MEANS TOWARDS 0 1 BY 1.....SO ...NO USE OF s=ov
for(s=0;s<ov;s++)//take ANOTHER VARIABLE s WHICH WILL TEND TO ++MEANT UNTILL IT REACHES TO OV VALUE,...
{
*(fnew+s)=*(fnew+s+1);// 1ST POSITION VALUE IS PUT INTO 0TH POSIION//2ND TO 1ST //3RD TO 2ND //4THTO 3RD & SO ON.....
printf("fnew+s=%d\n",*(fnew+s));//TO CHECK JUST PRINT...
}//THIS LOOP CONTINUES UNTILL IT SHIFTS ALL THE VALUES UPTO OV....NOW OV..WILL HAVE RETAINED VALUE INTO IT.....SO WE CAN PUT VALUES AGAIN INTO OV....FROM TOP..4 TO 5 TO 6 POSITION.....//OR WE CAN SAY DURING SHIFTING THE VALUES ...VALUE OF OV WILL NOT BE CHANGED......//
}
}
void push(int* fnew)
{
int top;
printf("ENTER THE TOP\n");//ENTER THE VALUE TO BE PUSHED INTO QUEUE
scanf("%d",&top);
*(fnew+ov)=top;//PUT THE VALUE INTO 0TH PLACE OF POINTER ...
//WARNING---DO NOT USE (fnew+ov)=&top.. to assign the value....THIS WILL CHANGE THE ADDRESS OF POINTER...fnew....//
printf("*(fnew+%d)=%d\n",ov,*(fnew+ov));//print the value just to check the value that is pushed
ov++;//++ment ov which is GLOBALLY DECLARED....EVERY TIME WHEN VALUE IS PUSSHED INTO POINTER...ov ++MEANT MEANS AT LAST OV IS AT VALUE WHERE THERE IS NO VALUE AFTER PUSHING ALL THE VALUES ....NOW I HAVE TO POP OR I CAN SAY..PICK THE VALUE..//
fnew=malloc(sizeof(int*));//QUEUE IS CREATED ...MALLOC OF int* POINTER IS DONE ...& POINTER ALWAYS TAKES 4 BYTES..EITHER IT IS char* ...||void* || int* ||struct node* ||..pointer is provided always 4 bytes once...
return fnew;
}
int main()//0 POSITION WILL REPRESENT THE TOP POSITION....//
{
int* new;
int flag=0,choice;
do
{
printf("case1:create_queue\n");
printf("case2:push\n");//create_menu for queue
printf("case3:pop\n");
printf("ENTER THE CHOICE\n");
scanf("%d",&choice);//CHOOSE THE CASE TO PUSH THE VALUES INTO QUEUE OR POP VALUES FROM QUEUE
switch(choice)
{
case 1:
if(flag==0)
{
new=create_queue();//FIRST OF ALL CASE 1 IS SELECTED...BCZ...QUEUE MUST BE CREATED...MEANS NOTHING BUT A POINTER MUST BE MALLOCED...TO PUT THE VALUES INTO IT OR PICK THE VALUES FROM THIS POINTER & OVERITETHAT MEMORY OF POINTER ONCE VALUE IS PICKED.....
flag=1;
}
else if(flag==1)//IF FLAG IS 1 THAT MEANS QUEUE IS ALREADY CREATED..SO NO NEED TO CREATE IT AGAIN ....//
{
printf("queue is already created\n");
}
break;
case 2:
push(new);//TO PUSH THE VAUE INTO THAT CREATED POINTER OR WE CAN SAY INTO QUEUE FROM THE TOP....FROM 0 POSITION TO NEXT & NEXT & NEXT...SO ON....//
break;
case 3:
pop(new);//POP THE VALUES ONCE ANY VALUE IS PUSHED INTO QUEUE ||NO. OF VALUES ARE PUSHED INTO QUEUE....//
break;
}
}while(choice != 0);//IF CHOICE IS O...MEANS I DONT WANT TO DEAL WITH QUEUE...SO COME OUT ....OF PROGRAM...//