Question

could you implement this function please, im having issues with it. void makeList (const ListNode::value_type [],const...

could you implement this function please, im having issues with it.

void makeList (const ListNode::value_type [],const size_t& count)

class ListNode

{

public:

typedef int value_type;

ListNode (value_type d = value_type(), ListNode* n = NULL) { datum = d; next = n; }

  

//Assessor

value_type getDatum () const { return datum; }

ListNode const* getNext () const { return next; }

  

//Mutator

void setDatum (const value_type& d) {datum = d; }

ListNode* getNext () { return next; }

void setNext (ListNode* new_link) {next = new_link; }

  

private:

value_type datum;

ListNode* next;

};

  

  

class LinkedList

{

public:

LinkedList ();

virtual ~LinkedList ();

  

void insertItem (ListNode::value_type);

void makeList (const ListNode::value_type [],const size_t& count);

void deleteList ();

  

//The following friend function is implemented in lablinklist.cpp

friend std::ostream& operator<<(std::ostream&, const LinkedList&);

private:

ListNode* head;

};

This is the pseudocode, but i still have a hard time undertanding it.

Creating a List (makeList(const ListNode::value_type [],const size_t& count)) This function receives an array in the order that we want to add it to the linkedlist. Index 0 will be the head node, index n will be the last node.

First, create a node initialized with a data value and a NULL pointer. Set the "head" to point to the first node. Set up a current-pointer to the first node (or "head"). Get a data value for the next node. While more nodes to add { Create a new node initialized with the data value and a NULL pointer. Set the current-pointer link member ("next") to the new node. Set the current-pointer to point to the new node. Get a data value for the next node. }

Thanks.

Homework Answers

Answer #1

//linkedList.h

#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include <cstdlib>
#include<iostream>
class ListNode
{
public:
   typedef int value_type;
   ListNode(value_type d = value_type(), ListNode* n = NULL) { datum = d; next = n; }

   //Assessor
   value_type getDatum() const { return datum; }
   ListNode const* getNext() const { return next; }

   //Mutator
   void setDatum(const value_type& d) { datum = d; }
   ListNode* getNext() { return next; }
   void setNext(ListNode* new_link) { next = new_link; }

private:
   value_type datum;
   ListNode* next;
};


class LinkedList
{
public:
   LinkedList();
   virtual ~LinkedList();

   void insertItem(ListNode::value_type);
   void makeList(const ListNode::value_type[], const size_t& count);
   void deleteList();

   //The following friend function is implemented in lablinklist.cpp
   friend std::ostream& operator<<(std::ostream&, const LinkedList&);
private:
   ListNode* head;
};
#endif

-------------------------------------------------------------

//linkedList.cpp

#include "linkedlist.h"

LinkedList::LinkedList()
{
   head = NULL;
}
LinkedList::~LinkedList()
{
   ListNode *tmp = head;
   while (tmp != NULL)
   {
       head = head->getNext();
       delete tmp;
       tmp = head;
   }
}

void LinkedList::insertItem(ListNode::value_type item)
{
   ListNode *newNode,*cur = head;
   newNode = new ListNode;
   newNode->setDatum(item);
   newNode->setNext(NULL);
   if (head == NULL)
   {
       head = newNode;
   }
   else
   {

       while (cur->getNext() != NULL)
       {
           cur = cur->getNext();
       }
       cur->setNext(newNode);
   }

}

void LinkedList::makeList(const ListNode::value_type num[], const size_t& count)
{
   int i = 0;
   while ( i < count)
   {
       insertItem(num[i++]);
   }
}

void LinkedList::deleteList()
{
   ListNode *tmp = head;
   while (tmp != NULL)
   {
       head = head->getNext();
       delete tmp;
       tmp = head;
   }
}

std::ostream& operator<<(std::ostream& os, const LinkedList &srcList) {

   //Set a current-pointer to the "head".
   ListNode* cursor = srcList.head;

   //While current-pointer is not NULL
   while (cursor != NULL)
   {
       //Print the data member ("datum") of the current node
       os << "->[" << cursor->getDatum() << "]";
       //Set the current-pointer to the "next" node in the list.
       cursor = cursor->getNext();
   }
   //Print out a basic termination symbol
   std::cout << "--X" << std::endl;

   return os;

}

---------------------------------------

//main.cpp

#include <iostream>
#include "linkedlist.h"

using namespace std;

int main() {

   LinkedList list1;

   //Test of adding items out of order
   list1.insertItem(5);
   list1.insertItem(20);
   list1.insertItem(10);
   cout << "After adding 5,20,10 to list ,List contains" << endl;
   cout << list1 << endl;

   //Test of deleting entire list
   list1.deleteList();
   cout << "After deleteing list,List contains" << endl;
   cout << list1 << endl;

   //Add items again in same order as before
   cout << "After adding 5,20,10 to list ,List contains" << endl;
   list1.insertItem(5);
   list1.insertItem(20);
   list1.insertItem(10);
   cout << list1 << endl;

   //Now replace list with a new one in a specific order
   int pow2[] = { 1, 2, 4, 8, 16, 32, 16, 8, 4, 2, 1 };
   list1.makeList(pow2, sizeof(pow2) / sizeof(int));
   cout << "After calling makeList function" << endl;
   cout << list1 << endl;

   //Returning a non-zero number, if not 3, then we know it seg-faulted
   return 3;
}

-----------------------------------------------------------------------------------------------------------

After adding 5,20,10 to list ,List contains
->[5]->[20]->[10]--X

After deleteing list,List contains
--X

After adding 5,20,10 to list ,List contains
->[5]->[20]->[10]--X

After calling makeList function
->[5]->[20]->[10]->[1]->[2]->[4]->[8]->[16]->[32]->[16]->[8]->[4]->[2]->[1]--X

Know the answer?
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
Similar Questions
Write the implementation of a non-member function Node* deleteSecond(Node* head_ptr), where Node is a class defined...
Write the implementation of a non-member function Node* deleteSecond(Node* head_ptr), where Node is a class defined on page 257. The function takes as input a pointer to the head of a linked list consisting of numbers. The function should remove the second item in the list. If the list had only one item, the function should delete that item. If the list was empty, then let the list remain empty. In all cases return the new head of the list...
(C++) Suppose you want to use a linked list where the items stored in the list...
(C++) Suppose you want to use a linked list where the items stored in the list are strings from the standard library string class, how would you change the node1.h header file? Header File: #ifndef MAIN_SAVITCH_NODE1_H #define MAIN_SAVITCH_NODE1_H #include <cstdlib> // Provides size_t and NULL namespace main_savitch_5 { class node { public: // TYPEDEF typedef double value_type; // CONSTRUCTOR node(const value_type& init_data=value_type( ), node* init_link=NULL) { data_field = init_data; link_field = init_link; } // MODIFICATION MEMBER FUNCTIONS node* link( )...
Assume that you have the two classes below (a partial implementation of a linked list of...
Assume that you have the two classes below (a partial implementation of a linked list of Dogs). Modify them to utilize generics (so that they can store any typed of object - instead of just Dogs). I’d suggest just writing any line that needs to be modified to the right of the current line. public class Node { private Node next; private Dog data; public Node(Dog d) { next = null; data = d; } public Node getNext() { return...
C++ pls finish code! Lab: Singly-Linked List (Student class) Review and finish the following files (read...
C++ pls finish code! Lab: Singly-Linked List (Student class) Review and finish the following files (read code and all comments carefully): Student.h StudentList.h StudentList.cpp main.cpp This program: Creates a sorted linked list (student name and gpa) . The list is sorted in ascending order by name. Displays the list Read and understand this program, then do the following: finish writing Student.h and other files fix errors in StudentList.cpp Student.h: #ifndef STUDENT_H #define STUDENT_H //using namespace std; //<==== This statement //...
Requirements The assignment is to create a dynamic array implementation of a set (defined in set.h)....
Requirements The assignment is to create a dynamic array implementation of a set (defined in set.h). Add the efficiency of each function to the documentation in the herder file. Use the test_set.cpp as your test program. _______________________________________________________________________________________________________________________________________________________ set.h file #ifndef _SET_H_ #define _SET_H_ #include <cstdlib> #include <iostream> class set { public: typedef int value_type; typedef std::size_t size_type; set(size_type initial_capacity); // postcondition: empty set with initial_capacity has been created ~set(); // postcondition: all dynamically allocated memory has been deallocated set(const set&...
Data Structure in C++ I keep getting the same warning, and I cant seem to fix...
Data Structure in C++ I keep getting the same warning, and I cant seem to fix it.. Can you explain to me what I am doing wrong? Warning: dlist.cc: In function 'std::ostream& operator<<(std::ostream&, dlist&)': dlist.cc:66:10: error: invalid initialization of reference of type 'std::ostream& {aka std::basic_ostream&}' from expression of type 'dlist::node*' dlist.cc: In function 'dlist operator+(dlist&, dlist&)': dlist.cc:93:8: error: invalid operands of types 'dlist::node*' and 'dlist::node*' to binary 'operator+' dlist.cc:97:8: error: could not convert 'result' from 'int' to 'dlist' My code:...
Question: I get a Segmentation fault error sometimes when I addElementBack or print. Am I using...
Question: I get a Segmentation fault error sometimes when I addElementBack or print. Am I using pointers correctly and deleting memory properly? #ifndef DYNAMICARRAY_H #define DYNAMICARRAY_H #include <cstdlib> #include <iostream> using namespace std; // Node class class Node { int data; Node* next; Node* prev; public: Node(); Node(int); void SetData(int newData) { data = newData; }; void SetNext(Node* newNext) { next = newNext; }; void SetPrev(Node* newPrev) { prev = newPrev; }; int getData() { return data; }; Node* getNext()...
c++ data structures linked list delete node bool deleteNode(int); pass this method an id to delete....
c++ data structures linked list delete node bool deleteNode(int); pass this method an id to delete. Return true or false to indicate success or failure. Delete the memory the node was using The algorithm for deleting a Node to a Linked List (general case): ● Begin at the head. ● Compare the id to the current node. ● If search id > current id, stop. ● Detach the current Node ○ current->previous->next = current->next ○ current->next->previous = current->previous ● Deallocate...
Create a C++ project. Download and add the attached .h and .cpp to the project. Write...
Create a C++ project. Download and add the attached .h and .cpp to the project. Write an implementation file to implement the namespace declared in the attached CSCI361Proj5.h. Name the implementation file as YourNameProj5.cpp and add it to the project. Run the project to see your grade. .h file: // Provided by: ____________(your name here)__________ // Email Address: ____________(your email address here)________ // FILE: link.h // PROVIDES: A toolkit of 14 functions for manipulating linked lists. Each // node of...
1. Design and implement a CircularLinkedList, which is essentially a linked list in which the next...
1. Design and implement a CircularLinkedList, which is essentially a linked list in which the next reference of the tail node is set to refer back to the head of the list (rather than null) . Start from the SinglyLinkedList provided in the Lecture (not the built-in Java LinkedList class). 2. Starting from  SinglyLinkedList you will need to modify or complete methods: first(), last(), addFirst(), addLast(), removeFirst(), toString(), and also create a new rotate() method which will move the tail to...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT