Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Make a C++ program that will run in Microsoft Visual Studio 2012 Implement a cla

ID: 3550161 • Letter: M

Question

Make a C++ program that will run in Microsoft Visual Studio 2012

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. You should implement the following operators, and any others that you may deem best.

start with

#include "stdafx.h"

#include<iostream>

    

using namespace std;


1)    DestroyList


2)    InitializeList


3)    GetFirst


4)    InsertFirst, InsertLast, Insert


5)    DeleteFirst, DeleteLast, Delete


6)    IsEmpty


7)    Length


8)    Print, ReversePrint

Explanation / Answer

#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.."<<" ";

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

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

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

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

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

cout<<"6.IsEmpty"<<" ";

cout<<"7.Length"<<" ";

cout<<"8.Print";

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

cout<<"10.Search"<<" ";

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"<<" ";

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


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

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;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote