jeudi 13 juillet 2017

Queue implemented with LinkedList error in pointer display function

In this queue is implemented using Linked List, So the display function is showing the correct result but the SizeOf() is also implemented by the same logic and if called not showing the proper answer. Why is this happening?

// A C program to demonstrate linked list based implementation of queue
#include <stdlib.h>
#include <stdio.h>
#include<bits/stdc++.h>
using namespace std;

// A linked list (LL) node to store a queue entry
struct QNode
{
    int key;
    struct QNode *next;
};

// The queue, front stores the front node of LL and rear stores the last node of LL
struct Queue
{
    struct QNode *front, *rear;
};

// A utility function to create a new linked list node.
struct QNode* newNode(int k)
{
    struct QNode *temp = (struct QNode*)malloc(sizeof(struct QNode));
    temp->key = k;
    temp->next = NULL;
    return temp;
}

// A utility function to create an empty queue
struct Queue *createQueue()
{
    struct Queue *q = (struct Queue*)malloc(sizeof(struct Queue));
    q->front = q->rear = NULL;
    return q;
}

// The function to add a key k to q
void enQueue(struct Queue *q, int k)
{
    // Create a new LL node
    struct QNode *temp = newNode(k);

    // If queue is empty, then new node is front and rear both
    if (q->rear == NULL)
    {
    q->front = q->rear = temp;
    return;
    }

    // Add the new node at the end of queue and change rear
    q->rear->next = temp;
    q->rear = temp;
}

// Function to remove a key from given queue q
struct QNode *deQueue(struct Queue *q)
{
    // If queue is empty, return NULL.
    if (q->front == NULL)
    return NULL;

    // Store previous front and move front one node ahead
    struct QNode *temp = q->front;
    q->front = q->front->next;

    // If front becomes NULL, then change rear also as NULL
    if (q->front == NULL)
    q->rear = NULL;
    return temp;
}

void Display(struct Queue *q)
{
    if(q==NULL)
    {
        cout<<"No elements"<<endl;
        return;
    }
    else{
        while(q->front->next!=NULL)
        {
            cout<<q->front->key<<" ";
            q->front=q->front->next;
        }
        cout<<q->front->key<<" ";
    }
}

int SizeOf(struct Queue *q)
{
    int count=0;
    if(q==NULL)
    {
        cout<<"No elements"<<endl;
        return 0;
    }
    else{
        while(q->front->next!=NULL)
        {
            count++;
            q->front=q->front->next;
        }
      count++;
    }
    return count;
}
// Driver Program to test anove functions
int main()
{
    struct Queue *q = createQueue();

    enQueue(q, 10);
    enQueue(q, 20);
    deQueue(q);
    deQueue(q);
    enQueue(q, 30);
    enQueue(q, 40);
    enQueue(q, 50);
    enQueue(q, 40);
    enQueue(q, 50);


    struct QNode *n = deQueue(q);
    if (n != NULL)
    printf("Dequeued item is %d\n", n->key);



    cout<<"The Queue is Displayed as follows:"<<endl;
    Display(q);

    cout<<"The Queue Size is as follows:"<<endl;
    int no=SizeOf(q);
    cout<<no<<endl;`enter code here`
    return 0;
}

Output of Display() is 40 50 40 50 but output of SizeOf() is 1. What is the problem with that?

Aucun commentaire:

Enregistrer un commentaire