dimanche 14 octobre 2018

How to test the written functions in the liked lists

I have written the following code for the linked list and now I want to test whether my function work correctly or not. Can anyone please show how to test those functions. Can anyone give an idea how I can print the elements in the main.

#include <iostream>
using namespace std;

class list {
private:
    struct node
    {
        int data;
        node *next;
    };
    node *head;
    node *tail;
public:
    list()
    {
        head = NULL;
        tail = NULL;
    }
    void createnode(int value)
    {
        node *temp = new node;
        temp->data = value;
        temp->next = NULL;
        if (head == NULL)
        {
            head = temp;
            tail = temp;
            temp = NULL;
        }
        else
        {
            tail->next = temp;
            tail = temp;
        }
    }
    void display()
    {
        node *temp = new node;
        temp = head;
        while (temp != NULL)
        {
            cout << temp->data << " ";
            temp = temp->next;
        }
    }
    bool pop_front(int val)
    {
        node *temp = new node;
        if (head == NULL)
        {
            return false;
        }
        else 
        {
            temp = head;
            head = head->next;
            delete temp;
        }
    }
    void push_back(int val)
    {
        node *last = new node;
        last->data = val;
        last->next = NULL;
        node *temp = new node;
        temp = head;
        if (temp == NULL)
        {
            head = last;
        }
        else
        {
            while (temp != NULL)
            {
                temp = temp->next;
            }
            temp->next = last;
        }
    }
};

int main()
{

    system("pause");
    return 0;
}

I want to test it by printing the elements by pushing them into the linked list.

Aucun commentaire:

Enregistrer un commentaire