Question

In: Computer Science

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

Solutions

Expert Solution

#include<iostream>

#include<cstdio>

#include<cstdlib>

using namespace std;

struct node

{

    int value;

    struct node* next;

    struct node* prev;

};

struct node* head;

struct node* tail;

void init()

{

    head=NULL;

    tail=NULL;

}

void insertFirst(int element)

{

    struct node* newItem;

    newItem=new node;

    if(head==NULL)

    {

        head=newItem;

        newItem->prev=NULL;

        newItem->value=element;

        newItem->next=NULL;

        tail=newItem;

    }

    else

    {

        newItem->next=head;

        newItem->value=element;

        newItem->prev=NULL;

        head->prev=newItem;

        head=newItem;

    }

}

void insertLast(int element)

{

    struct node* newItem;

    newItem=new node;

    newItem->value=element;

    if(head==NULL)

    {

        head=newItem;

        newItem->prev=NULL;

        newItem->next=NULL;

        tail=newItem;

    }

    else

    {

        newItem->prev=tail;

        tail->next=newItem;

        newItem->next=NULL;

        tail=newItem;

    }

}

void insertAfter(int old, int element)

{

    struct node* newItem;

    newItem=new node;

    struct node* temp;

    temp=head;

    if(head==NULL)

    {

        cout<<"could not insert"<<endl;

        return;

    }

    if(head==tail)

    {

        if(head->value!=old)

        {

            cout<<"could not insert"<<endl;

            return;

        }

        newItem->value=element;

        head->next=newItem;

        newItem->next=NULL;

        head->prev=NULL;

        newItem->prev=head;

        tail=newItem;

        return;

    }

    if(tail->value==element)

    {

        newItem->next=NULL;

        newItem->prev=tail;

        tail->next=newItem;

        tail=newItem;

        return;

    }

    while(temp->value!=old)

    {

        temp=temp->next;

        if(temp==NULL)

        {

            cout<<"Could not insert"<<endl;

            cout<<"element not found"<<endl;

            return;

        }

    }

    newItem->next=temp->next;

    newItem->prev=temp;

    newItem->value=element;

    temp->next->prev=newItem;

    temp->next=newItem;

}

void deleteFirst()

{

    if(head==NULL)

    {

        return;

    }

    if(head==tail)///one element in the list

    {

        struct node* cur;

        cur=head;

        head=NULL;

        tail=NULL;

        delete cur;

        return;

    }

    else

    {

        struct node* cur;

        cur=head;

        head=head->next;

        head->prev=NULL;

        delete cur;

    }

}

void deleteLast()

{

    if(head==NULL) return;

    if(head==tail)

    {

        struct node* cur;

        cur=head;

      head=NULL;

        tail=NULL;

        delete cur;

        return;

    }

    else

    {

        struct node* cur;

        cur=tail;

        tail=tail->prev;

        tail->next=NULL;

        delete cur;

    }

}

void deleteItem(int element)

{

    struct node* temp;

    temp=head;

    if(head==tail)

    {

        if(head->value!=element)

        {

            cout<<"could not delete"<<endl;

            return;

        }

        head=NULL;

        tail=NULL;

        delete temp;

        return;

    }

    if(head->value==element)

    {

        head=head->next;

        head->prev=NULL;

        delete temp;

        return;

    }

    else if(tail->value==element)

    {

        temp=tail;

        tail=tail->prev;

        tail->next=NULL;

        delete temp;

        return;

    }

    while(temp->value!=element)

    {

        temp=temp->next;

        if(temp==NULL)

        {

            cout<<"element not found"<<endl;

            return;

        }

    }

    temp->next->prev=temp->prev;

   temp->prev->next=temp->next;

    delete temp;

}

struct node* searchItem(int element)

{

    struct node* temp;

    temp=head;

    while(temp!=NULL)

    {

        if(temp->value==element)

        {

            return temp;

            break;

        }

        temp=temp->next;

    }

    return NULL;

}

void printList()

{

    struct node* temp;

    temp=head;

    while(temp!=NULL)

    {

        printf("%d->",temp->value);

        temp=temp->next;

    }

    puts("");

}

void printReverse()

{

    struct node* temp;

    temp=tail;

    while(temp!=NULL)

    {

        cout<<temp->value<<"->";

        temp=temp->prev;

    }

    cout<<endl;

}

void makereverse()

{

    struct node* prv=NULL;

    struct node* cur=head;

    struct node* nxt;

    while(cur!=NULL)

    {

        nxt=cur->next;

        cur->next=prv;

        prv=cur;

        cur=nxt;

    }

    head=prv;

}

int dltfrst()

{

    if(head==NULL)

    {

        return 0;

    }

    int prev;

    prev=head->value;

    if(head==tail)///one element in the list

    {

        struct node* cur;

        cur=head;

        head=NULL;

        tail=NULL;

        delete cur;

        return prev;

    }

    else

    {

        struct node* cur;

        cur=head;

        head=head->next;

        head->prev=NULL;

        delete cur;

        return prev;

    }

}

int dltlast()

{

    if(head==NULL) return 0;

    int prev;

    prev=tail->value;

    if(head==tail)

    {

        struct node* cur;

        cur=head;

        head=NULL;

        tail=NULL;

        delete cur;

        return prev;

    }

    else

    {

        struct node* cur;

        cur=tail;

        tail=tail->prev;

        tail->next=NULL;

        delete cur;

        return prev;

    }

}

void leftRotate(int x)

{

    for(int i=0;i<x;i++)

    {

        int a=dltfrst();

        insertLast(a);

    }

}

void rightRotate(int x)

{

    for(int i=0;i<x;i++)

    {

        int a=dltlast();

        insertFirst(a);

    }

}

int main()

{

    init();

    int choice;

    while(1)

    {

        printf("1.InsertFirst 2. InsertLast 3. InsertAfter 4.DeleteFirst 5. DeleteLast\n");

        printf("6.SearchItem 7. PrintList 8. ReversePrint 9. DeleteItem \n");

        printf("10. Left Rotate 11. Right Rotate 12. Exit 13.Make reverse\n");

        cin>>choice;

        if(choice==1)

        {

            int element;

            cout<<"Enter element_";

            cin>>element;

            insertFirst(element);

            printList();

        }

        else if(choice==2)

        {

            int element;

            cout<<"Enter element_";

            cin>>element;

            insertLast(element);

            printList();

        }

        else if(choice==3)

        {

            int old,newitem;

            cout<<"Enter Old Item_";

            cin>>old;

            cout<<"Enter new Item_";

            cin>>newitem;

            insertAfter(old,newitem);

            printList();

        }

        else if(choice==4)

        {

            deleteFirst();

            printList();

        }

        else if(choice==5)

        {

            deleteLast();

            printList();

        }

        else if(choice==6)

        {

            int item;

            cout<<"Enter Item to Search_";

            cin>>item;

            struct node* ans=searchItem(item);

            if(ans!=NULL) cout<<"FOUND "<<ans->value<<endl;

            else cout<<"NOT FOUND"<<endl;

        }

        else if(choice==7)

        {

            printList();

        }

        else if(choice==8)

        {

            printReverse();

        }

        else if(choice==9)

        {

            int element;

            cin>>element;

            deleteItem(element);

            printList();

        }

        else if(choice==10)

        {

            int x;

            cin>>x;

            leftRotate(x);

            printList();

        }

        else if(choice==11)

        {

            int x;

            cin>>x;

            rightRotate(x);

            printList();

        }

        else if(choice==12)

        {

            break;

        }

        else if(choice==13)

        {

            makereverse();

        }

    }

return 0;

}


Related Solutions

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...
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. DestroyList InitializeList GetFirst InsertFirst, InsertLast, Insert DeleteFirst, DeleteLast, Delete IsEmpty Length Print, ReversePrint
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.
C++  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
implement the reverse() method that changes the ordering of the items within a doublylinkedlist class. don't...
implement the reverse() method that changes the ordering of the items within a doublylinkedlist class. don't return anything and make sure it executes without errors on lists with no items or one item example: fruits = DoublyLinkedList() fruits.append('apple') fruits.append('banana') fruits.append('cherry') for i in fruits: print(i) >> apple >> banana >> Charlie fruits.reverse() for i in fruits: print(i) >> cherry >> banana >> apple
C++ program homework question 1 1. Create and implement a class called clockType with the following...
C++ program homework question 1 1. Create and implement a class called clockType with the following data and methods (60 Points.): Data: Hours, minutes, seconds Methods: Set and get hours Set and get minutes Set and get seconds printTime(…) to display time in the form of hh:mm:ss default and overloading constructor Overloading Operators: << (extraction) operator to display time in the form of hh:mm:ss >> (insertion) operator to get input for hours, minutes, and seconds operator+=(int x) (increment operator) to...
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; };...
Hello, Please follow and answer the requirement on these question based on the case company which...
Hello, Please follow and answer the requirement on these question based on the case company which is Samsung. a) What is the organizational structure for Samsung Company (centralized or decentralized)? specific case information. (250 words)
Complete the following for full credit in this assignment: Complete the coding requirement for the Class...
Complete the following for full credit in this assignment: Complete the coding requirement for the Class Method Bodies: Write the constructor: This should initialize all of your class fields and dynamically allocate memory for your array that will hold your stack data. You should use the constant “n” as the size for your fixed-size stack. Write the push method: This should add the item in the formal parameter to your stack. Caveat: You need to check and make sure you...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT