Question

In: Computer Science

Please Use C++ to finish as the requirements. Implement a class called SinglyLinkedList. In the main...

Please Use C++ to finish as the requirements.

Implement a class called SinglyLinkedList. In the main function, instantiate the SinglyLinkedList class. Your program should provide a user loop and a menu so that the user can access all the operators provided by the SinglyLinkedList class.

  1. DestroyList
  2. InitializeList
  3. GetFirst
  4. InsertFirst, InsertLast, Insert
  5. DeleteFirst, DeleteLast, Delete
  6. IsEmpty
  7. Length
  8. Print, ReversePrint

Solutions

Expert Solution

If You have Any Query Regarding this please ask in comment section I will be there to solve all your query in comment section immediately hope you will like it

#include "stdafx.h"

#include<iostream>

#include<conio.h>

#include<stdlib.h>



struct node

{

int data;

node *link;

}*p;



class list

{

node *next;

public:

void InsertLast(int);

void InsertFirst(int);

void Insert(int,int);

void Delete(int);

void DeleteFirst();

void DeleteLast();

void Print();

int seek(int);

list(){p=NULL;}

~list();

bool IsEmpty();

void reverse();

int Length();

};

bool IsEmpty()

{

if(p==NULL)

return true;

else

return false;

}



void list :: reverse()

{

int a[50];

next =p;

int i=0;

while(next->link!=NULL)

{

a[i]=next->data;

next=next->link;

i=i+1;

}

next=p;

while(next->link!=NULL)

{

next->data=a[i];

next=next->link;

i=i-1;

}

Print();

}

void list::InsertLast(int x)

{

node *q,*t;

if(p==NULL)

{

p=new node;

p->data=x;

p->link=NULL;

}

else

{

q=p;

while(q->link!=NULL)

q=q->link;

t=new node;

t->data=x;

t->link=NULL;

q->link=t;

}

cout<<" Inserted successfully at the end..";

Print();

}



void list:: InsertFirst(int x)

{

node *q;

q=p;

p=new node;

p->data=x;

p->link=q;

cout<<" Inserted successfully at the begining..";

Print();

}





void list::Delete(int x)

{

node *q,*r;

q=p;

if(q->data==x)

{

p=q->link;

delete q;

return;

}

r=q;

while(q!=NULL)

{

if(q->data==x)

{

r->link=q->link;

delete q;

return;

}

r=q;

q=q->link;

}

cout<<"

Element u entered "<<x<<" is not found..

";

}



void list:: DeleteFirst()

{

cout<<" The list before deletion:";

disp();

node *q;

q=p;

if(q==NULL)

{

cout<<" No data is present..";

return;

}

p=q->link;

delete q;

return;

}





void list:: DeleteLast()

{

cout<<"

The list before deletion:

";

disp();

node *q,*t;

q=p;

if(q==NULL)

{

cout<<" There is no data in the list..";

return;

}

if(q->link==NULL)

{

p=q->link;

delete q;

return;

}



while(q->link->link!=NULL)

q=q->link;

q->link=NULL;

return;

}



list::~list()

{

node *q;

if(p==NULL) return;

while(p!=NULL)

{

q=p->link;

delete p;

p=q;

}

}



void list::Print()

{

node *q;

q=p;

if(q==NULL)

{

cout<<" No data is in the list..";

return;

}

cout<<" The items present in the list are :";

while(q!=NULL)

{

cout<<" "<<q->data;

q=q->link;

}

}



void list :: Insert(int value,int position)

{

node *temp,*temp1;

temp=p;

if(temp1==NULL)

{

temp1= new node;

temp1->data=value;

temp1->link=NULL;

p=temp1;

return;

}

for(int i=0;((i<position)&&(temp->link!=NULL)) ;i++)

{

if(i==(position-1))

{

temp1= new node;

temp1->data= value;

temp1->link=temp->link;

temp->link=temp1;

}

temp=temp->link;

}

Print();

}





int list::seek(int value)

{

node *temp;

temp=p;

int position=0;

while(temp!=NULL)

{

if(temp->data==value)

return position+1;

else

{

temp=temp->link;

position=position+1;

}

}

cout<<" Element "<<value<<" not found";

return 0;

}



int list::GetFirst()

{

return p->data;

}

int list::Length(int value)

{

node *temp;

temp=p;

int position=0;

while(temp!=NULL)

{

position++

}

cout<<" Element "<<value<<" not found";

return position;

}



void main()

{

list l;

int ch,v,p,ps,n;

bool flag;

do

{

clrscr();

cout<<" Operations on List.."<<"\n";

cout<<"1.Destroy List"<<"\n";

cout<<"2.Initialize List"<<"\n";

cout<<"3.Get First"<<"\n";

cout<<"4.Insert First,Insert Last,Insert"<<"\n";

cout<<"5.Delete First,Delete Last,Delete"<<"\n";

cout<<"6.IsEmpty"<<"\n";

cout<<"7.Length"<<"\n";

cout<<"8.Print";

cout<<"9.Reverse Print"<<"\n";

cout<<"10.Search"<<"\n";

cout<<"11.Exit";

cout<<"Enter ur choice:";

cin>>ch;



switch(ch)

{

case 1:

~l();

cout<<"List has been destroyed";

case 2:

l=new list();

cout<<"List has been initialized";

case 2:

n= GetFirst();

cout<<"First element in list is: "<<n;

case 4:

cout<<" 1.Insertion at begining"<<"\n";

cout<< "2.Insertion at the end"<<"\n";



cout<<"3.Insertion after the mentioned position"<<"\n";

cout<<" Enter ur choice:";

cin>>ps;

cout<<"Enter the value to insert:";

cin>>v;

switch(ps)

{

case 1:

l.InsertFirst(v);

break;

case 2:

l.InsertLast(v);

break;

case 3:

cout<<" Enter the position to insert the value:";

cin>>p;

l.Insert(v,p);

break;



default:

cout<<"The choice is invalid";

return;

}

break;



case 5:

cout<<"1.Delete the first element ";

cout<<"2.Delete the last element"

cout<<"3.Enter the element to delete from the list";

cout<<" Enter ur choice:";

cin>>ps;

switch(ps)

{

case 1:

l.DeleteFirst();

cout<<" The list after deletion:";

l.Print();

break;

case 2:

l.DeleteLast();

cout<<" The list after deletion:";

l.Print();

break;

case 3:

l.Print();

cout<<" Enter the element to delete : ";

cin>>v;

l.Delete(v);

cout<<" The list after deletion:";

l.Print();

break;



default:

cout<<" The option is invalid...";

break;

}

break;



case 6:

flag= l.IsEmpty();

if(flag==true)

cout<<" List is Empty";

else

cout<<" List is not Empty";

break;

case 7:

n=l.Length();

cout<<"number of elements : "<<n;

break;

case 8:

l.Print();

break;

case 9:

l.reverse();

break;

case 10:

l.Print();

cout<<" Enter the element to search:";

cin>>v;

cout<<" The position of the element "<< v<<" is "<<l.seek(v);

getch();

break;



case 11:

exit(1);



default:

cout<<" The option is invalid...";

return;

}

getch();

}while(ch!=11);

getch();

return;

}

Related Solutions

Please use C++ to complete this question follow the requirement. Question: Implement a class called DoublyLinkedList....
Please use C++ to complete this question follow the requirement. Question: Implement a class called DoublyLinkedList. In the main function, instantiate the DoublyLinkedList class and make sure that there is a user loop and a menu so that the user can access all the list operators. You should implement the following operators, and any others that you may deem best. DestroyList InitializeList GetFirst InsertFirst, InsertLast, Insert DeleteFirst, DeleteLast, Delete IsEmpty Length Print, ReversePrint
Please complete absolutely follow the requirements. Thanks! Implement a stack ADT by writing a class called...
Please complete absolutely follow the requirements. Thanks! Implement a stack ADT by writing a class called Stack. Use a static array to hold stack elements. Instantiate the Stack class in the main function and provide a user loop and a menu so that all the Stack class member-functions, push, pop, etc., are available so that the user can thoroughly exercise the member-functions of the Stack class. Also, implement a ReversePrint() for the stack. My StackProject, whose exposition I have given...
Define a class called Goals that has the following requirements in c++: a function called set...
Define a class called Goals that has the following requirements in c++: a function called set that takes 3 int parameters that are goals for "fame", "happiness" and "money". The function returns true and saves the values if they add up to exactly 60, and returns false otherwise (you may assume the parameters are not negative) a functions satisfies that takes 3 int parameters and returns true if each parameter is at least as large as the saved goal, false...
Use the Heap class provided to implement a sort routine in a Main class where the...
Use the Heap class provided to implement a sort routine in a Main class where the user enters a series of values, each value is then pushed onto a heap, then the values are printed out in ascending order. public class Heap { public static final int SIZE = 1025; public Heap() { elt = new Element[SIZE]; lastLoc = 0; } public void push(String k, Object o) { if (!fullCheck()) { lastLoc++; elt[lastLoc] = new Element(k,o); int loc = lastLoc;...
in C++ Requirements: Write a program that creates a new class called Customer. The Customer class...
in C++ Requirements: Write a program that creates a new class called Customer. The Customer class should include the following private data: name - the customer's name, a string. phone - the customer's phone number, a string. email - the customer's email address, a string. In addition, the class should include appropriate accessor and mutator functions to set and get each of these values. Have your program create one Customer objects and assign a name, phone number, and email address...
In C++, implement a class called setOper that provides several simple set operations. The class only...
In C++, implement a class called setOper that provides several simple set operations. The class only needs to deal with sets that are closed intervals specified by two real numbers; for example, the pair (2.5, 4.5) represent the interval [2.5, 4.5]. The following operations should be supported: - Check if the value x belongs to the given interval. - Check if the value x belongs to the intersection of two intervals. - Check if the value x belongs to the...
Overview For this assignment, implement and use the methods for a class called Seller that represents...
Overview For this assignment, implement and use the methods for a class called Seller that represents information about a salesperson. The Seller class Use the following class definition: class Seller { public: Seller(); Seller( const char [], const char[], const char [], double ); void print(); void setFirstName( const char [] ); void setLastName( const char [] ); void setID( const char [] ); void setSalesTotal( double ); double getSalesTotal(); private: char firstName[20]; char lastName[30]; char ID[7]; double salesTotal; };...
Overview For this assignment, implement and use the methods for a class called Seller that represents...
Overview For this assignment, implement and use the methods for a class called Seller that represents information about a salesperson. The Seller class Use the following class definition: class Seller { public: Seller(); Seller( const char [], const char[], const char [], double ); void print(); void setFirstName( const char [] ); void setLastName( const char [] ); void setID( const char [] ); void setSalesTotal( double ); double getSalesTotal(); private: char firstName[20]; char lastName[30]; char ID[7]; double salesTotal; };...
For this assignment, implement and use the methods for a class called Seller that represents information about a salesperson.
For this assignment, implement and use the methods for a class called Seller that represents information about a salesperson.The Seller classUse the following class definition:class Seller { public:   Seller();   Seller( const char [], const char[], const char [], double );        void print();   void setFirstName( const char [] );   void setLastName( const char [] );   void setID( const char [] );   void setSalesTotal( double );   double getSalesTotal(); private:   char firstName[20];   char lastName[30];   char ID[7];   double salesTotal; };Data MembersThe data members for the class are:firstName holds the Seller's first namelastName holds the Seller's last nameID holds the Seller's id numbersalesTotal holds the Seller's sales totalConstructorsThis class has two constructors. The default constructor (the one that takes...
ROCK, PAPER, SCISSORS Using C++, you will implement a class called Tool. It should have an...
ROCK, PAPER, SCISSORS Using C++, you will implement a class called Tool. It should have an int field called strength and a char field called type. You may make them either private or protected. The Tool class should also contain the function void setStrength(int), which sets the strength for the Tool. Create 3 more classes called Rock, Paper, and Scissors, which inherit from Tool. Each of these classes will need a default constructor that sets the strength to 1 and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT