Question
can you guys please help me with these questions thank you

9.Given a Double Linked-List that contains the values in order: 1,2,3,4,5; and the following class declarations: class List p
0 0
Add a comment Improve this question Transcribed image text
Answer #1

a) here is the code to prints whole list backwards

code C++

#include<iostream>
#include<stack>
using namespace std;

class Node
{
friend class List;
private:
int value;
Node *next;
Node *prev;
};
class List{
private:
Node *head;
public:
void printBackwards();
void addNode(int);
void removeNodeAtValue(int);
};
//remove node at value
void List::removeNodeAtValue(int value)
{
Node *ptr=head;
if(head==NULL)
return;
if(head->value==value)
{
head=head->next;
return;
}
while(ptr!=NULL)
{
if(ptr->value==value)
{
ptr->prev->next=ptr->next;
  
}
ptr=ptr->next;
}
}
//print backwards using only singly link list
void List::printBackwards()
{
Node *ptr=head;
stack<int> elements;
while(ptr!=NULL)
{
elements.push(ptr->value);
ptr=ptr->next;
}
while(!elements.empty())
{
cout<<elements.top()<<", ";
elements.pop();
}
cout<<endl;
}
//add node to create link list
void List::addNode(int value)
{
Node *ptr=head;
if(head==NULL)
{
head=new Node();
head->value=value;
head->next=NULL;
head->prev=NULL;
return;
}
Node * temp=new Node();
temp->value=value;
temp->next=NULL;
while(ptr->next!=NULL)
{
ptr=ptr->next;
}
ptr->next= temp;
temp->prev=ptr;
}

int main()
{
List *l= new List();
l->addNode(1);
l->addNode(2);
l->addNode(3);
l->addNode(4);
l->addNode(5);
l->printBackwards();
l->removeNodeAtValue(4);
l->printBackwards();
return 0;
}
output

5, 4, 3, 2, 1,
5, 3, 2, 1,

b) extra credit for using singly link list above code is also using singly link list

void List::printBackwards()
{
Node *ptr=head;
stack<int> elements;
while(ptr!=NULL)
{
elements.push(ptr->value);
ptr=ptr->next;
}
while(!elements.empty())
{
cout<<elements.top()<<", ";
elements.pop();
}
cout<<endl;
}

c) Big o running time for this algorithm in O(N) since to store all the elements in a stack we are traversing link list only once.then we are using one more loop to traverse stack and print elements. so total N+N = O(N)

Question 10

A) above code is also implementation of above algorithm , below is only the function

//remove node at value
void List::removeNodeAtValue(int value)
{
Node *ptr=head;
if(head==NULL)
return;
if(head->value==value)
{
head=head->next;
return;
}
while(ptr!=NULL)
{
if(ptr->value==value)
{
ptr->prev->next=ptr->next;
  
}
ptr=ptr->next;
}
}

b) Big o running time for this algorithm is also O(N) since we are only traversing the doubly link list only once which can only take upto N time in worst case

Add a comment
Know the answer?
Add Answer to:
can you guys please help me with these questions thank you 9.Given a Double Linked-List that...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Define a struct Node to represent a node in a double linked-list that stores a string...

    Define a struct Node to represent a node in a double linked-list that stores a string as data. Write a function to insert a node to the head of the linked list. The function takes two arguments: a pointer to the first node in the double linked list and a string value. It should create a new node with the given value to the head of the double linked list.

  • C++ ONLY PLEASE. CAN I GET SOME HELP WRITING THIS HEADER FILE? I MAINLY WANT TO...

    C++ ONLY PLEASE. CAN I GET SOME HELP WRITING THIS HEADER FILE? I MAINLY WANT TO KNOW HOW I WOULD WRITE IT WITH A TEMPLATE. PLEASE AND THANKS IN ADVANCE. **************LinkedList.h requirements************************ linked list Class EXTRA CREDIT OPPORTUNITY: Create the LinkedList class as a template class for 5 extra credit points!!! private memebers Create a structure called ListNode, which should hold a Dinosaur and a pointer to the next ListNode ListNode pointer called head – will eventually point to the...

  • C++ program, item.cpp implementation. Implementation: You are supposed to write three classes, called Item, Node and In...

    C++ program, item.cpp implementation. Implementation: You are supposed to write three classes, called Item, Node and Inventory respectively Item is a plain data class with item id, name, price and quantity information accompanied by getters and setters Node is a plain linked list node class with Item pointer and next pointer (with getters/setters) Inventory is an inventory database class that provides basic linked list operations, delete load from file / formatted print functionalities. The majority of implementation will be done...

  • ***CODE MUST BE IN C++*** Using the linked list in "basic linked list" which has a...

    ***CODE MUST BE IN C++*** Using the linked list in "basic linked list" which has a STRUCTURE for each node, write FUNCTION which starts at the head and outputs the value for each node until the last node is reached. Note: your function should work with the structure that is in this program. Please do not use the example which is for a class, but this example canbe helkpful.  Also note that the function must work no matter how many nodes...

  • Problem Statement This problem provides you with a linked list composed of node objects chained together...

    Problem Statement This problem provides you with a linked list composed of node objects chained together via node pointers. Each node has a next pointer which points to the next node in the chain. The last node is identified by having a NULL (or nullptr) next pointer. The linked lists for this problem store string data. Your task: identify the longest string stored in the linked list. If two strings are of the same length, return the string that occurred...

  • 16 and 18 C++ 11. Given the dynamic linked implementation of a linked list shown below,...

    16 and 18 C++ 11. Given the dynamic linked implementation of a linked list shown below, write expressions that do the following, assuming that currPtr is somewhere in the middle of the list: a. Access the component member of the first list element. b. Advance currPtr to point to the next element. c. Access the component member of the next element (the one that follows the current element). d. Access the component member of the element that follows the next...

  • ***Using C++ and also please label "a" and "c" accordingly. Thank you!!**** a) Write the member...

    ***Using C++ and also please label "a" and "c" accordingly. Thank you!!**** a) Write the member function void deleteNode() that will delete the first node from the linked list. b) Modify the LList to create a data member Node *cur that points to the current node. Write the following member functions: • void forward() that moves the cur pointer to the next node. • void backward() that moves the cur pointer to the previous node. • void delete() that removes...

  • You are given a pointer head to the first node in a linked list. Each node...

    You are given a pointer head to the first node in a linked list. Each node points to the next node. We call the list a snail if the last node points to some node v in the list. We call the list a snake if the last node points to NULL. The list has n nodes. You are not allowed to change the list (permanently or temperately). You are allowed to use O(1) extra memory. The value of n...

  • This is a c programming problem. Would you please help me to write this problem??? I...

    This is a c programming problem. Would you please help me to write this problem??? I really appreciate it if you add comments for explanation step by step. Thank you. Reverse a Doubly linked list using recursion: Given a doubly linked list. Reverse it using recursion. Original Doubly linked list: next pointer - DDHIHI Null prev painter Reversed Doubly linked list: next pointer Start Pointer Null prev pointer Include: a) A struct for a node of the doubly linked list....

  • Q) Modify the class Linked List below to make it a Doubly Linked List. Name your...

    Q) Modify the class Linked List below to make it a Doubly Linked List. Name your class DoublyLinkedList. Add a method addEnd to add an integer at the end of the list and a method displayInReverse to print the list backwards. void addEnd(int x): create this method to add x to the end of the list. void displayInReverse(): create this method to display the list elements from the last item to the first one. Create a main() function to test...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT