Question

In: Computer Science

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

Solutions

Expert Solution

Hi,

Please find the code below.

Below program illustrates doubly linked list in c++ and differnet operations on the same.

#include
#include
#include
using namespace std;

struct Node
{
int element;
struct Node* next;
struct Node* prev;
};
struct Node* head;
struct Node* tail;

void initialisePointers()
{
head=NULL;
tail=NULL;
}

void insertToStartOfList(int input)
{
struct Node* newNode;
newNode=new Node;
if(head==NULL)
{
head=newNode;
newNode->prev=NULL;
newNode->element=input;
newNode->next=NULL;
tail=newNode;
}
else
{
newNode->next=head;
newNode->element=input;
newNode->prev=NULL;
head->prev=newNode;
head=newNode;
}
}

void insertToLastOfList(int input)
{
struct Node* newNode;
newNode=new Node;
newNode->element=input;
if(head==NULL)
{
head=newNode;
newNode->prev=NULL;
newNode->next=NULL;
tail=newNode;
}
else
{
newNode->prev=tail;
tail->next=newNode;
newNode->next=NULL;
tail=newNode;
}
}

void insertAfterGivenNumber(int old, int element)
{
struct Node* newNode;
newNode=new Node;
struct Node* temp;
temp=head;
if(head==NULL)
{
cout<<"could not insert"<<endl;
return;
}
if(head==tail)
{
if(head->element!=old)
{
cout<<"could not insert"<<endl;
return;
}
newNode->element=element;
head->next=newNode;
newNode->next=NULL;
head->prev=NULL;
newNode->prev=head;
tail=newNode;
return;
}
if(tail->element==element)
{
newNode->next=NULL;
newNode->prev=tail;
tail->next=newNode;
tail=newNode;
return;
}
while(temp->element!=old)
{
temp=temp->next;
if(temp==NULL)
{
cout<<"Could not insert"<<endl;
cout<<"element not found"<<endl;
return;
}
}

newNode->next=temp->next;
newNode->prev=temp;
newNode->element=element;
temp->next->prev=newNode;
temp->next=newNode;
}

void deleteAtFirstPosition()
{
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 deleteAtLastPosition()
{
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 deleteGivenElement(int input)
{
struct Node* temp;
temp=head;
if(head==tail)
{
if(head->element!=input)
{
cout<<"could not delete"<<endl;
return;
}
head=NULL;
tail=NULL;
delete temp;
return;
}
if(head->element==input)
{
head=head->next;
head->prev=NULL;
delete temp;
return;
}
else if(tail->element==input)
{
temp=tail;
tail=tail->prev;
tail->next=NULL;
delete temp;
return;
}
while(temp->element!=input)
{
temp=temp->next;
if(temp==NULL)
{
cout<<"given integer not found"<<endl;
return;
}
}
temp->next->prev=temp->prev;
temp->prev->next=temp->next;
delete temp;
}

void isListEmpty()
{
struct Node* temp;
temp=head;
if(temp == NULL)
{
cout << "List is empty" << endl;
}
else
{
cout << "List is not empty" << endl;
}
}

void LengthOfList()
{
int length = 0;
struct Node* temp;
temp=head;
while(temp!=NULL)
{
length++;
temp=temp->next;
}
cout << " The length of the list is :" << length << endl;
}

void displayList()
{
struct Node* temp;
temp=head;
while(temp!=NULL)
{
printf("%d->",temp->element);
temp=temp->next;
}
puts("");
}

void printReverseOrder()
{
struct Node* temp;
temp=tail;
while(temp!=NULL)
{
cout<element<<"->";
temp=temp->prev;
}
cout<<endl;
}

int main()
{
initialisePointers();

int choiceFromUser;
while(1)
{
printf("1.InsertFirst 2. InsertLast 3. InsertAfter 4.DeleteFirst 5. DeleteLast\n");
printf("6. PrintCompleteList 7. ReversePrintList 8. deleteGivenElement \n");
printf("9. IsEmpty 10.LengthOfList 12. Exit \n");
cin>>choiceFromUser;
if(choiceFromUser==1)
{
int input;
cout<<"Enter Integer element to be inserted into DLL(at start) - ";
cin>>input;
insertToStartOfList(input);
displayList();
}
else if(choiceFromUser==2)
{
int input;
cout<<"Enter Integer element to be inserted into DLL(at last) -";
cin>>input;
insertToLastOfList(input);
displayList();
}
else if(choiceFromUser==3)
{
int oldNumber,newNumber;
cout<<"Enter Old number after which you would like to insert element - ";
cin>>oldNumber;
cout<<"Enter New number you want to insert -";
cin>>newNumber;
insertAfterGivenNumber(oldNumber,newNumber);
displayList();
}
else if(choiceFromUser==4)
{
deleteAtFirstPosition();
displayList();
}
else if(choiceFromUser==5)
{
deleteAtLastPosition();
displayList();
}
else if(choiceFromUser==6)
{
displayList();
}
else if(choiceFromUser==7)
{
printReverseOrder();
}
else if(choiceFromUser==8)
{
int element;
cin>>element;
deleteGivenElement(element);
displayList();
}
else if(choiceFromUser==9)
{
cout << "Checking if list is empty" << endl;
isListEmpty();
}
else if(choiceFromUser==10)
{
cout << "Printing length of the list" << endl;
LengthOfList();
}
else if(choiceFromUser==11)
{
break;
}
  
}
return 0;
}

Outputs attached:

 


Related Solutions

print a menu so that the user can then order from that menu. The user will...
print a menu so that the user can then order from that menu. The user will enter each item they want until they are done with the order. At the end you will print the items the user ordered with their price and the total. Note: You will be storing your menu items (pizza, burger, hotdog, salad) and your prices (10, 7, 4, 8) in separate lists. Menu 0) pizza $10 1) burger $7 2) hotdog $4 3) salad $8...
Part 1 (20%) Implement a class with a main method. Using an enhanced for loop, display...
Part 1 (20%) Implement a class with a main method. Using an enhanced for loop, display each element of this array: String[] names = {"alice", "bob", "carla", "dennis", "earl", "felicia"}; Part 2 (30%) In a new class, implement two methods that will each calculate and return the average of an array of numeric values passed into it. Constraints: your two methods must have the same name one method should accept an array of ints; the other should accept an array...
in JAVA: implement a class called tree (from scratch) please be simple so i can understand...
in JAVA: implement a class called tree (from scratch) please be simple so i can understand thanks! tree(root) node(value, leftchild,rightchild) method: insert(value)
How do you think that governments can make sure that all kids have real access to...
How do you think that governments can make sure that all kids have real access to magnet and charter schools instead of just those kids with advantages? How can government make sure that magnet and charter schools provide the quality education that taxpayers want for all children?
Write a main function that reads a list of integers from a user, adds to an...
Write a main function that reads a list of integers from a user, adds to an array using dynamic memory allocation, and then displays the array. The program also displays the the largest element in the integer array. Requirement: Using pointer notation. Please do this with C++
**** IN C++ **** 1) Modify the class pointerDataClass so the main function below is working...
**** IN C++ **** 1) Modify the class pointerDataClass so the main function below is working properly. Use deep copy. int main() { pointerDataClass list1(10); list1.insertAt(0, 50); list1.insertAt(4, 30); list1.insertAt(8, 60); cout<<"List1: " < list1.displayData(); cout<<"List 2: "< pointerDataClass list2(list1); list2.displayData(); list1.insertAt(4,100); cout<<"List1: (after insert 100 at indext 4) " < list1.displayData(); cout<<"List 2: "< list2.displayData(); return 0; } Code: #include using namespace std; class pointerDataClass { int maxSize; int length; int *p; public: pointerDataClass(int size); ~pointerDataClass(); void insertAt(int index,...
**** IN C++ **** 1) Modify the class pointerDataClass so the main function below is working...
**** IN C++ **** 1) Modify the class pointerDataClass so the main function below is working properly. Use shallow copy. int main() { pointerDataClass list1(10); list1.insertAt(0, 50); list1.insertAt(4, 30); list1.insertAt(8, 60); cout<<"List1: " < list1.displayData(); cout<<"List 2: "< pointerDataClass list2(list1); list2.displayData(); list1.insertAt(4,100); cout<<"List1: (after insert 100 at indext 4) " < list1.displayData(); cout<<"List 2: "< list2.displayData(); return 0; } Code: #include using namespace std; class pointerDataClass { int maxSize; int length; int *p; public: pointerDataClass(int size); ~pointerDataClass(); void insertAt(int index,...
C++ programing Write a main function that  reads a list of integers from a user, adds to...
C++ programing Write a main function that  reads a list of integers from a user, adds to an array using dynamic memory allocation, and then displays the array. The program also displays the the largest element in the integer array. Requirement: Using pointer notation.
Write a function called main which gets a single number, furlongs, from the user and converts...
Write a function called main which gets a single number, furlongs, from the user and converts that to meters and prints out the converted result. The math you need to know is that there are 201.16800 meters in a furlong. your results should yield something like these test cases. >>> main() How many furlongs? 10 your 10 furlongs are 2011.68 Meters >>> main() How many furlongs? 24 your 24 furlongs are 4828.032 Meters
1. Delete function for a BST class given a value. You need to make sure that...
1. Delete function for a BST class given a value. You need to make sure that the tree remains a binary search tree after deletion. Your function should work for nodes that can handle all the following cases: a. Node to be deleted has no children b. Node to be deleted has only one child c. Node to be deleted has two children 2. A BST class function that returns the maximum value in a tree. 3. A BST class...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT