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

Build a Single_Linked_List class. Your class should have the data members: head,

ID: 3587850 • Letter: B

Question

Build a Single_Linked_List class. Your class should have the data members: head,
tail, and num_items. Write the following member functions, which perform the same
operations as the corresponding functions in the standard list class: push_front,
push_back, pop_front, pop_back, front, back, empty, size. Also write the following
member functions:
• void insert(size_t index, const Item_Type& item): Insert item at position
index (starting at 0). Insert at the end if index is beyond the end of the list.
308 Chapter 4 Sequential Containers
• bool remove(size_t index): Remove the item at position index. Return true if
successful; return false if index is beyond the end of the list.
• size_t find(const Item_Type& item): Return the position of the first occurrence
of item if it is found. Return the size of the list if it is not found.

This should be written in C++.

I wrote up my own code already, it would just be extremely helpful to compare mine to someone else who also knows what they're doing. Thanks!

Explanation / Answer

#include<iostream>

#include<conio.h>

#include<stdlib.h>

using namespace std;

class Node

{

public:

    int info;

    Node* next;

};

class List:public Node

{

    Node *first,*last;

public:

    List()

    {

        first=NULL;

        last=NULL;

    }

    void create();

    void insert();

    void delet();

    void display();

    void search();

};

void List::create()

{

    Node *temp;

    temp=new Node;

    int n;

    cout<<" Enter an Element:";

    cin>>n;

    temp->info=n;

    temp->next=NULL;

    if(first==NULL)

    {

        first=temp;

        last=first;

    }

    else

    {

        last->next=temp;

        last=temp;

    }

}

void List::insert()

{

    Node *prev,*cur;

    prev=NULL;

    cur=first;

    int count=1,pos,ch,n;

    Node *temp=new Node;

    cout<<" Enter an Element:";

    cin>>n;

    temp->info=n;

    temp->next=NULL;

    cout<<" INSERT AS 1:FIRSTNODE 2:LASTNODE 3:IN BETWEEN FIRST&LAST NODES";

    cout<<" Enter Your Choice:";

    cin>>ch;

    switch(ch)

    {

    case 1:

        temp->next=first;

        first=temp;

        break;

    case 2:

        last->next=temp;

        last=temp;

        break;

    case 3:

        cout<<" Enter the Position to Insert:";

        cin>>pos;

        while(count!=pos)

        {

            prev=cur;

            cur=cur->next;

            count++;

        }

        if(count==pos)

        {

            prev->next=temp;

            temp->next=cur;

        }

        else

            cout<<" Not Able to Insert";

        break;

    }

}

void List::delet()

{

    Node *prev=NULL,*cur=first;

    int count=1,pos,ch;

    cout<<" DELETE 1:FIRSTNODE 2:LASTNODE 3:IN BETWEEN FIRST&LAST NODES";

    cout<<" Enter Your Choice:";

    cin>>ch;

    switch(ch)

    {

    case 1:

        if(first!=NULL)

        {

            cout<<" Deleted Element is "<<first->info;

            first=first->next;

        }

        else

            cout<<" Not Able to Delete";

        break;

    case 2:

        while(cur!=last)

        {

            prev=cur;

            cur=cur->next;

        }

        if(cur==last)

        {

            cout<<" Deleted Element is: "<<cur->info;

            prev->next=NULL;

            last=prev;

        }

        else

            cout<<" Not Able to Delete";

        break;

    case 3:

        cout<<" Enter the Position of Deletion:";

        cin>>pos;

        while(count!=pos)

        {

            prev=cur;

            cur=cur->next;

            count++;

        }

        if(count==pos)

        {

            cout<<" Deleted Element is: "<<cur->info;

            prev->next=cur->next;

        }

        else

            cout<<" Not Able to Delete";

        break;

    }

}

void List::display()

{

    Node *temp=first;

    if(temp==NULL)

    {

        cout<<" List is Empty";

    }

    while(temp!=NULL)

    {

        cout<<temp->info;

        cout<<"-->";

        temp=temp->next;

    }

    cout<<"NULL";

}

void List::search()

{

    int value,pos=0;

    bool flag=false;

    if(first==NULL)

    {

        cout<<"List is Empty";

        return;

    }

    cout<<"Enter the Value to be Searched:";

    cin>>value;

    Node *temp;

    temp=first;

    while(temp!=NULL)

    {

        pos++;

        if(temp->info==value)

        {

            flag=true;

            cout<<"Element"<<value<<"is Found at "<<pos<<" Position";

            return;

        }

        temp=temp->next;

    }

    if(!flag)

    {

        cout<<"Element "<<value<<" not Found in the List";

    }

}

int main()

{

    List l;

    int ch;

    while(1)

    {

        cout<<" **** MENU ****";

        cout<<" 1:CREATE 2:INSERT 3:DELETE 4:SEARCH 5:DISPLAY 6:EXIT ";

        cout<<" Enter Your Choice:";

        cin>>ch;

        switch(ch)

        {

        case 1:

            l.create();

            break;

        case 2:

            l.insert();

            break;

        case 3:

            l.delet();

            break;

        case 4:

            l.search();

            break;

        case 5:

            l.display();

            break;

        case 6:

            return 0;

        }

    }

    return 0;

}

#include<iostream>

#include<conio.h>

#include<stdlib.h>

using namespace std;

class Node

{

public:

    int info;

    Node* next;

};

class List:public Node

{

    Node *first,*last;

public:

    List()

    {

        first=NULL;

        last=NULL;

    }

    void create();

    void insert();

    void delet();

    void display();

    void search();

};

void List::create()

{

    Node *temp;

    temp=new Node;

    int n;

    cout<<" Enter an Element:";

    cin>>n;

    temp->info=n;

    temp->next=NULL;

    if(first==NULL)

    {

        first=temp;

        last=first;

    }

    else

    {

        last->next=temp;

        last=temp;

    }

}

void List::insert()

{

    Node *prev,*cur;

    prev=NULL;

    cur=first;

    int count=1,pos,ch,n;

    Node *temp=new Node;

    cout<<" Enter an Element:";

    cin>>n;

    temp->info=n;

    temp->next=NULL;

    cout<<" INSERT AS 1:FIRSTNODE 2:LASTNODE 3:IN BETWEEN FIRST&LAST NODES";

    cout<<" Enter Your Choice:";

    cin>>ch;

    switch(ch)

    {

    case 1:

        temp->next=first;

        first=temp;

        break;

    case 2:

        last->next=temp;

        last=temp;

        break;

    case 3:

        cout<<" Enter the Position to Insert:";

        cin>>pos;

        while(count!=pos)

        {

            prev=cur;

            cur=cur->next;

            count++;

        }

        if(count==pos)

        {

            prev->next=temp;

            temp->next=cur;

        }

        else

            cout<<" Not Able to Insert";

        break;

    }

}

void List::delet()

{

    Node *prev=NULL,*cur=first;

    int count=1,pos,ch;

    cout<<" DELETE 1:FIRSTNODE 2:LASTNODE 3:IN BETWEEN FIRST&LAST NODES";

    cout<<" Enter Your Choice:";

    cin>>ch;

    switch(ch)

    {

    case 1:

        if(first!=NULL)

        {

            cout<<" Deleted Element is "<<first->info;

            first=first->next;

        }

        else

            cout<<" Not Able to Delete";

        break;

    case 2:

        while(cur!=last)

        {

            prev=cur;

            cur=cur->next;

        }

        if(cur==last)

        {

            cout<<" Deleted Element is: "<<cur->info;

            prev->next=NULL;

            last=prev;

        }

        else

            cout<<" Not Able to Delete";

        break;

    case 3:

        cout<<" Enter the Position of Deletion:";

        cin>>pos;

        while(count!=pos)

        {

            prev=cur;

            cur=cur->next;

            count++;

        }

        if(count==pos)

        {

            cout<<" Deleted Element is: "<<cur->info;

            prev->next=cur->next;

        }

        else

            cout<<" Not Able to Delete";

        break;

    }

}

void List::display()

{

    Node *temp=first;

    if(temp==NULL)

    {

        cout<<" List is Empty";

    }

    while(temp!=NULL)

    {

        cout<<temp->info;

        cout<<"-->";

        temp=temp->next;

    }

    cout<<"NULL";

}

void List::search()

{

    int value,pos=0;

    bool flag=false;

    if(first==NULL)

    {

        cout<<"List is Empty";

        return;

    }

    cout<<"Enter the Value to be Searched:";

    cin>>value;

    Node *temp;

    temp=first;

    while(temp!=NULL)

    {

        pos++;

        if(temp->info==value)

        {

            flag=true;

            cout<<"Element"<<value<<"is Found at "<<pos<<" Position";

            return;

        }

        temp=temp->next;

    }

    if(!flag)

    {

        cout<<"Element "<<value<<" not Found in the List";

    }

}

int main()

{

    List l;

    int ch;

    while(1)

    {

        cout<<" **** MENU ****";

        cout<<" 1:CREATE 2:INSERT 3:DELETE 4:SEARCH 5:DISPLAY 6:EXIT ";

        cout<<" Enter Your Choice:";

        cin>>ch;

        switch(ch)

        {

        case 1:

            l.create();

            break;

        case 2:

            l.insert();

            break;

        case 3:

            l.delet();

            break;

        case 4:

            l.search();

            break;

        case 5:

            l.display();

            break;

        case 6:

            return 0;

        }

    }

    return 0;

}

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