Que Append and Served
Guys i'm new to queues and finding it difficult to understand how it
works, all i understood is it Appends items and serves the first item that
was Appended.
from the given below, what i understood is everytime it appends the Link
is always NuLL is that correct? And also when does it becomes Tail !=NULL?
i'm confused cause everytime we append Tail is set to NULL...
int item;
struct Node
{
int Data;
struct Node *Link;
}; typedef struct Node *QueuePointer;
void Append(QueuePointer &Head,QueuePointer &Tail, int Num)
{
QueuePointer NewNode;
NewNode= (QueuePointer)malloc(sizeof(struct Node));
NewNode->Data = Num;
NewNode->Link = NULL;
if(Tail == NULL)
{
Head = NewNode;
// printf("Queue is empty"); //checks if queue is empty
// printf("\n");
}
else
{
Tail->Link = NewNode;
}
Tail = NewNode;
printf("Inserted number %d \n", item); //checks if Appends into Queue
working
}
void Serve(QueuePointer &Head, QueuePointer &Tail, int item)
{
QueuePointer Temp;
printf("Served ");
while(Head != NULL)
{
item = Head->Data;
Temp = Head;
Head = Head->Link;
if(Head == NULL)
{
Tail = NULL;
}
free(Temp);
printf("%d ", item); //prints out SERVED
}
}
int main()
{
QueuePointer Head, Tail;
Head=NULL;
Tail=NULL;
item=1;
for(item=1; item<=4; item++)
{
// if(item%2==0)
// {
Append(Head,Tail,item); //Appends For Every Even Number
Detected
// }
}
Serve(Head, Tail, item);//Calls out Serve Function, See LOOPING,
please refer serve function
//****NOTE: the loop for Removing items is inside Serve Function
getch();
}
No comments:
Post a Comment