Question

Language:C++

only numbers 4 and 5 please2. 1. Use the Node class from the slides to create a linked list of ints. Create a Node pointer called ptrHead that points t

0 0
Add a comment Improve this question Transcribed image text
Answer #1

C++ Code:

#include <iostream>
using namespace std;
class Node {
   private:
       float data;
       Node* nextPtr;
   public :
   Node()
   {
       this->nextPtr = NULL;
   }
   float getData() const
   {
       return this->data;
   }
   void setData(float d)
   {
       this->data = d;
   }
   Node *getNodePtr() const
   {
       return this->nextPtr;
   }
   void setNodePtr(Node *n)
   {
       this->nextPtr = n;
   }
  
};
// revrese a list
Node* reverse(Node *head)
{
   // Initialize current, previous, next pointers
   Node* current = head;
   Node *prev = NULL, *next = NULL;

   while (current != NULL)
   {
       // before reversing store next of current
       next = current->getNodePtr();
       // Reverse current node's pointer
       current->setNodePtr(prev);
       // Move pointers one position ahead.
       prev = current;
       current = next;
   }
   head = prev;
   return head;
}
//print a list
void print(Node *head)
{
   struct Node* temp = head;
   while (temp != NULL) {
       cout << temp->getData ()<< " ";
       temp = temp->getNodePtr();
   }
}
// insert a value in front of list
Node *insert(Node *head, float data)
{
   Node* temp = new Node();
   temp->setData(data);
   temp->setNodePtr(head);
   head = temp;
   return head;
}
//returns sum of all element of list
float sum(Node *head)
{
   float total=0;
   struct Node* temp = head;
   while (temp != NULL) {
       total += temp->getData ();
       temp = temp->getNodePtr();
   }
   return total;
}
/* Driver program to test above function*/
int main()
{
   /* Start with the empty list */
   Node *head = NULL;
  
   // insert few values into list
   head = insert(head,20);
   head = insert(head,30);
   head = insert(head,40);
   head = insert(head,50);
   cout<<"The list is: ";
   print(head);
   cout<<"\nSum of all list elements is: "<<sum(head);
   //reverse the list
   head = reverse(head);
   cout<<"\nList after reversal is: ";
   print(head);
   return 0;
}
Sample output:

The list is: 50 40 30 20 Sum of all list elements is: 140 List after reversal is: 20 30 40 50 = = = = = = = = = = = = = = - -Note: The functions except sum() and reverse() are just used for testing purpose.

If you have any doubts in this solution please ask them in comments.

Add a comment
Know the answer?
Add Answer to:
Language:C++ only numbers 4 and 5 please 2. 1. Use the Node class from the slides...
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
  • C++ Implement a templated class list and listnode. You may add methods/functions as you see fit....

    C++ Implement a templated class list and listnode. You may add methods/functions as you see fit. Test these classes. I have left all of the implementation as an exercise for you. template< class NODETYPE > class List;  // forward declaration template<class NODETYPE> class ListNode {    friend class List< NODETYPE >; // make List a friend public:    ListNode( const NODETYPE &newData);  // copy constructor    NODETYPE getData() const;      // return data in the node private:    NODETYPE data;                 // data    ListNode< NODETYPE > *nextPtr; // next node...

  • PLEASE USE C++ Source Code Attached is a linked list with 2 nodes. You can use...

    PLEASE USE C++ Source Code Attached is a linked list with 2 nodes. You can use this or write a similar one. The assignment is to write 2 functions. One function will add another node at the end of the list. The other function will delete a node. Don't forget - No dangling pointers ! Example linked source code attached below #include<iostream> using namespace std; class Node { int data; Node *next; public: void setdata(int d) {data = d;} void...

  • Working in C++, Complete the code. // Lab 9 code /* Insert leading comments with your...

    Working in C++, Complete the code. // Lab 9 code /* Insert leading comments with your name and the date. Describe the purpose of the code. Also, list each pointer and describe how it is used to enable the selection sort for the linked list structure. */ /* Insert code as described by the comments. */ /* Add comments to each line of code that uses a pointer, describing how it is being used. */ #include <iostream> using namespace std;...

  • What is the specific answer for 1. and 2. Thanks! Add a new method, find, to...

    What is the specific answer for 1. and 2. Thanks! Add a new method, find, to class SinglyLinkedList (defined here) that takes as input a “data” value and returns a pointer to a node. If the input data is present in the linked list, the returned pointer should point to that node; if not, the returned pointer is nullptr. Write the (single line) method declaration/specification. Write the method definition/implementation. Test by running the main() function below and capture the console...

  • above in the image is a linked list with 2 nodes. You can use this or...

    above in the image is a linked list with 2 nodes. You can use this or write a similar one. The assignment is to write 2 functions. One function will add another node at the end of the list. The other function will delete a node. Don't forget - No dangling pointers C++ language Make sure its corect and runs without errors and I promise to give you a thumbs up. #include iostream» using namespace std; class Node int data;...

  • Create an h file called list. It should have the following features: lisi's funnc In no particular order: List(): Default constructor. This should construct an empty List, the member variables sh...

    Create an h file called list. It should have the following features: lisi's funnc In no particular order: List(): Default constructor. This should construct an empty List, the member variables should be initialized to reflect this state. This function is already fully implemented. 1. List(const List<Type>& other): Copy constructor for the linked list. This should create an entirely new linked list with the same number of Nodes and the Values stored these Nodes in the same order as seen the...

  • We have written the following Node class: class Node { // instance variables private int value;...

    We have written the following Node class: class Node { // instance variables private int value; private Node next; // constructor public Node(int value) { this.value = value; } // get the 'next' variable public Node getNext() { return this.next; } // get the 'value' variable public int getValue() { return this.value; } // set the 'next' variable public void setNext(Node next) { this.next = next; } } TASK: Create a class called List that has the following properties: It...

  • could somone please help me to complete this ! public class MyLinkedList<AnyType> { private Node<AnyType> head,...

    could somone please help me to complete this ! public class MyLinkedList<AnyType> { private Node<AnyType> head, tail; private int size; public MyLinkedList() { this.head = null; this.tail = null; this.size = 0; } //1.Insert a node at the end of the list public void insert(AnyType data) { Node<AnyType> newNode = new Node(); newNode.data = data; if (head == null) { head = newNode; tail = newNode; head.next = null; tail.next = null; } else { tail.next = newNode; tail =...

  • C++ 1. Please use attached script for your reference to create the node structure and a...

    C++ 1. Please use attached script for your reference to create the node structure and a linked list class, say LinkedList, that has the following basic member methods:     constructor, destructor//IMPORTANT, display(), add_node(). 2. Please implement the following additional member methods:     Please feel free to change T with any data type you'd like to use for your node stricture's data type. -- addFirst(T data) // Adds an node with data at the beginning of the list -- pop() //...

  • Enum {FALSE=0, TRUE}; #define MAXBUFF 1024 #define SMALLBUFF 10 /* The LinkNode type is used as a...

    enum {FALSE=0, TRUE}; #define MAXBUFF 1024 #define SMALLBUFF 10 /* The LinkNode type is used as an element of a linked list ** implementation of a stack of tree nodes. */ typedef struct link_t LinkNode; typedef LinkNode *LinkNodePtr; /* The TreeNode type is used as an element of a binary "parse" tree. ** Tree nodes are created while parsing the RPN expression and ** stored on a stack until it's time to place them. */ typedef struct tree_t TreeNode; typedef...

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