Question: #include #include #include #include struct stock { int data; struct stock *next; }*nnode,*ptr,*p,*prev,*start; class LinkedList { public: LinkedList() { start=NULL; } void create() { nnode=new

#include

#include

#include

#include

struct stock

{

int data;

struct stock *next;

}*nnode,*ptr,*p,*prev,*start;

class LinkedList

{

public:

LinkedList()

{

start=NULL;

}

void create()

{

nnode=new stock;

cout<<"enter the data you want to enter";

cin>>nnode->data;

nnode->next=NULL;

start=nnode;

}

void addAtBeginning()

{

nnode=new stock;

cout<<"enter the data you want to enter";

cin>>nnode->data;

nnode->next=start;

start=nnode;

}

void addAtLast()

{

for(ptr=start;ptr!=NULL;prev=ptr,ptr=ptr->next);

cout<<"enter the data you want to enter";

nnode=new stock;

cin>>ptr->data;

prev->next=nnode;

nnode->next=NULL;

}

void deleteFirst()

{

ptr=start;

start=ptr->next;

delete(ptr);

}

void deleteLast()

{

for(ptr=start;ptr!=NULL;p=prev,prev=ptr,ptr=ptr->next);

p->next=NULL;

delete(prev);

}

void display()

{

for(ptr=start;ptr!=NULL;ptr=ptr->next)

cout<data;

}

int totalNumberOfNodes()

{

stock *ptr;

ptr=start;

int count=0;

while (ptr!=NULL)

{

count++;

ptr=ptr->next;

}

return count;

}

void listCircularOrNot()

{

}

void removeCircularLoop()

{

}

};

int main()

{

int ch;

LinkedList l;

clrscr();

while(1)

{

cout<<"enter choice";

cin>>ch;

switch(ch)

{

case 1:l.create();

break;

case 2:l.addAtBeginning();

break;

case 3:l.addAtLast();

break;

case 4:l.deleteFirst();

break;

case 5:l.deleteLast();

break;

case 6:l.display();

break;

case 7:l.listCircularOrNot();

break;

case 8:exit(1);

default:cout<<"invalid choice";

}

}

getch();

return 0;

}

Write function listCircularOrNot() to find whether a singly linked list is circular or not in other words how to determine whether a singly linked list contains a Loop or not? Also if the linkedList contains loop then write another function removeCircularLoop() to remove the loop from the linked list keeping the entire structure same.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!