Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Distributed Relational Database Architecture Connectivity Guide

Authors: Teresa Hopper

4th Edition

0133983064, 978-0133983067

More Books

Students also viewed these Databases questions

Question

Explain why households do not hold diversified portfolios.

Answered: 1 week ago