Which condition determines if linked list is empty or not?

With a root

The root acts as an element which is always present even if the list is empty.
The other use of having a root in list is that we can link the last element back to the root forming a cycle. According to this concept, next of any node is never NULL.

Pseudocode:

bool isEmpty[node *root]{ if [root->next == root] return true; else return false; }

The implementation is given below:

#include using namespace std; class Node{ public: int data; Node *next; Node[]{ data=0; next=NULL; } }; class linked_list{ Node *root; public: linked_list[]{ root=NULL; } Node* getRoot[]{ return root; } void add_node[int n]{ Node *temp = new Node[]; temp->data = n; temp->next = NULL; if[root == NULL]{ root=temp; root->next=root; } else{ Node *last=root; while[last->next!=root]{ last=last->next; } temp->next=root; last->next=temp; } } void printList[]{ Node *temp=root; if[temp!=NULL]{ do{ coutdata==0] return true; return false; } }; int main[]{ linked_list l1; l1.add_node[5]; l1.add_node[10]; l1.add_node[15]; if[l1.isEmpty[]] cout

Bài mới nhất

Chủ Đề