Question


001- 1. Write a C++ function which has a pointer to a linked list as one of its parameters. The function produces two linked

This is the creatList and printList function

#include <iostream>
#include <fstream>
using namespace std;
struct nodeType
{
   int info;
   nodeType *link;
   nodeType *next;
   double value;

};
void createList(nodeType*& first, nodeType*& last, ifstream& inf);
void printList(nodeType* first);
int main()
{
   nodeType *first, *last;
   int num;
   ifstream infile;
   infile.open("InputIntegers.txt");
   createList(first, last, infile);
   printList(first);

   infile.close();
   system("pause");
   return 0;
}
void createList(nodeType*& first, nodeType*& last, ifstream& infile)
{
   int number;
   nodeType *newNode;
   first = nullptr;
   last = nullptr;

   infile >> number;
   while (infile)
   {
       newNode = new nodeType;
       newNode->info = number;
       newNode->link = nullptr;

       if (first == nullptr)
       {
           first = newNode;
           last = newNode;
       }
       else
       {
           last->link = newNode;
           last = newNode;
       }
       infile >> number;
   }
}

void printList(nodeType* first)
{
   int i = 0;
   nodeType *current;
   current = first;
   while (current != nullptr)
   {
       i++;
       cout << current->info << " ";
       if (i % 10 == 0)
           cout << endl;
       current = current->link;
   }
   cout << endl<<endl;
}

InputFile:

2 4 10 28 50 14 -5 9 -2 30 15 -25 64 22 18
-28 14 30 -45 -16 4 5 -12 8
31 -50 -72 -41 -61 -7 100 -33
-20 -18 -6 -43 -15 -22 -21 -1
-7 -8 -14 -3 -11 -26 19 -9 -23
-55 1450
18189
1680
5
65
9630 275 400 300 700 200 100
5287 30 248 31 45 200
1700 305 528 602 13 85
11 19 30 49

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

#include <iostream>

#include <fstream>

using namespace std;

struct nodeType

{

   int info;

   nodeType *link;

   //nodeType *next; // not required

   //double value; // not required

};

void createList(nodeType*& first, nodeType*& last, ifstream& inf);

void printList(nodeType* first);

void sublist(nodeType *first, nodeType *&pos, nodeType *&neg);

int main() {

       nodeType *first, *last, *pos, *neg;

       //int num;

       ifstream infile;

       infile.open("InputIntegers.txt");

       createList(first, last, infile);

       cout<<"List : "<<endl;

       printList(first);

       infile.close();

       sublist(first,pos,neg);

       cout<<"Positive list : "<<endl;

       printList(pos);

       cout<<"Negative list : "<<endl;

       printList(neg);

       system("pause");

       return 0;

}

// function to create a linked list of integer read from a file

void createList(nodeType*& first, nodeType*& last, ifstream& infile)

{

   int number;

   nodeType *newNode;

   first = nullptr;

   last = nullptr;

   infile >> number;

   while (infile)

   {

       newNode = new nodeType;

       newNode->info = number;

       newNode->link = nullptr;

       if (first == nullptr)

       {

           first = newNode;

           last = newNode;

       }

       else

       {

           last->link = newNode;

           last = newNode;

       }

       infile >> number;

   }

}

// function to print a linked list

void printList(nodeType* first)

{

   int i = 0;

   nodeType *current;

   current = first;

   while (current != nullptr)

   {

       i++;

       cout << current->info << " ";

       if (i % 10 == 0)

           cout << endl;

       current = current->link;

   }

   cout << endl<<endl;

}

// function to create two sublist pos and neg from list first where pos contains the positive integers and neg contains the negative integers of first

void sublist(nodeType *first, nodeType *&pos, nodeType *&neg)

{

       pos = nullptr;

       neg = nullptr;

       nodeType *node = first;

       // loop over the list

       while(node != nullptr)

       {

             nodeType *curr = new nodeType;

             curr->info = node->info;

             curr->link = nullptr;

             // check if value is > 0 insert in pos else insert in neg

             if(node->info > 0)

             {

                    if(pos == nullptr)

                           pos = curr;

                    else

                    {

                           nodeType *temp = pos;

                           while(temp->link != nullptr)

                                 temp = temp->link;

                           temp->link = curr;

                    }

             }else

             {

                    if(neg == nullptr)

                           neg = curr;

                    else

                    {

                           nodeType *temp = neg;

                           while(temp->link != nullptr)

                                 temp = temp->link;

                           temp->link = curr;

                    }

             }

             node = node->link;

       }

}

//end of program

Output:

Input file:

24 10 28 50 14 -5 9 -2 30 15 -25 64 22 18 -28 14 30-45 -16 4 5 -12 8 31 -50 -72 -41 -61 -7 100 -33 -20 -18-6 -43 -15 -22 -21

Output:

List 2 4 10 28 50 14 -5 9-2 30 15 -25 64 22 18 -28 14 30 -45 -16 4 5 -12 8 31 -50 -72 -41 -61 -7 100 -33 -20 -18 -6 -43 -15 -

Add a comment
Know the answer?
Add Answer to:
This is the creatList and printList function #include <iostream> #include <fstream> using namespace std; struct nodeType...
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
  • How would you get this lab to work with the numbers 37 14 68 47, the...

    How would you get this lab to work with the numbers 37 14 68 47, the book we are using is Data structures using C++ by D.S. Malik. Please I need help? Just keeps repeating the same question. Code: #include <iostream> #include <cstdlib> using namespace std; struct nodeType { int info; nodeType *link; }; void createList(nodeType*& first, nodeType*& last); void printList(nodeType*& first); void insertFront(nodeType*& first); void insertBack(nodeType*& last); void deleteFirst(nodeType*& first); void deleteLast(nodeType*& last, nodeType* first); int main() { nodeType...

  • #include <iostream> #include <fstream> using namespace std; //constants const int CAP = 100; //function prototypes bool...

    #include <iostream> #include <fstream> using namespace std; //constants const int CAP = 100; //function prototypes bool openFile(ifstream &); void readData(ifstream &, int [], int &); void printData(const int [], int); void sum(const int[], int); void removeItem(int[], int &, int); int main() { ifstream inFile; int list[CAP], size = 0; if (!openFile(inFile)) { cout << "Program terminating!! File not found!" << endl; return -1; } //read the data from the file readData(inFile, list, size); inFile.close(); cout << "Data in file:" <<...

  • Write a function, swapSubtrees, that swaps all of the left and right subtrees of a binary...

    Write a function, swapSubtrees, that swaps all of the left and right subtrees of a binary tree. Add this function to the class binaryTreeType and create a program to test this function. #include <iostream> using namespace std; //Definition of the Node template <class elemType> struct nodeType { elemType info; nodeType<elemType> *lLink; nodeType<elemType> *rLink; }; //Definition of the class template <class elemType> class binaryTreeType { public: //Overload the assignment operator. const binaryTreeType<elemType>& operator=(const binaryTreeType<elemType>&) { if (this != &otherTree) //avoid self-copy...

  • I have a problem with merging the two linked lists together. In the function AscendMerge, I...

    I have a problem with merging the two linked lists together. In the function AscendMerge, I am trying to compare the values of each node in the two lists and output them into one list in ascended order. Everything works except for the one function. Can someone tell me what im doing wrong, when I run it it skips over values. #include <iostream> #include <cassert> using namespace std; struct nodeType {    int info;    nodeType *link;    nodeType *current;...

  • PLEASE FILL THE CODE ACCORDINGY AND THE REQUIRED OUTPUT IS GIVEN BELOW ALONG WITH THE INPUT...

    PLEASE FILL THE CODE ACCORDINGY AND THE REQUIRED OUTPUT IS GIVEN BELOW ALONG WITH THE INPUT Problem: The following function, buildListForward, builds a linked list in a forward manner and returns the pointer of the built list: Q1: The bulidListForward function to create a linked List structure with the keyboard input( cin >> num). Change this function to receive the values stored in the array from the main function( use int type pointer variable). eg. nodeType* buildListForward(int *arrayPrt, int Size)...

  • #include <iostream> using namespace std; struct ListNode { float value; ListNode *next; }; ...

    #include <iostream> using namespace std; struct ListNode { float value; ListNode *next; }; ListNode *head; class LinkedList { public: int insertNode(float num); void deleteNode(float num); void destroyList(); void displayList(); LinkedList(void) {head = NULL;} ~LinkedList(void) {destroyList();} }; int LinkedList::insertNode(float num) { struct ListNode *newNode, *nodePtr = head, *prevNodePtr = NULL; newNode = new ListNode; if(newNode == NULL) { cout << "Error allocating memory for new list member!\n"; return 1; } newNode->value = num; newNode->next = NULL; if(head==NULL) { cout << "List...

  • Double linked list implementation of PutItem function. How to fix my code to get desired output b...

    Double linked list implementation of PutItem function. How to fix my code to get desired output below: Output: 2 5 8 #ifndef ITEMTYPE_H #define ITEMTYPE_H enum RelationType { LESS, GREATER, EQUAL}; class ItemType { public:     ItemType();     void setValue(int newValue);     int getValue() const;     RelationType ComparedTo(ItemType newItem); private:     int value; }; #endif // ITEMTYPE_H // ItemType.cpp #include "ItemType.h" ItemType::ItemType() {     value = 0; } void ItemType::setValue(int newValue) {     value = newValue; } int ItemType::getValue() const {     return value; } RelationType ItemType::ComparedTo(ItemType newItem)...

  • #include <iostream> #include <string> #include <fstream> #include <sstream> using namespace std; struct transition{ // transition structure...

    #include <iostream> #include <string> #include <fstream> #include <sstream> using namespace std; struct transition{ // transition structure char start_state, to_state; char symbol_read; }; void read_DFA(struct transition *t, char *f, int &final_states, int &transitions){ int i, j, count = 0; ifstream dfa_file; string line; stringstream ss; dfa_file.open("dfa.txt"); getline(dfa_file, line); // reading final states for(i = 0; i < line.length(); i++){ if(line[i] >= '0' && line[i] <= '9') f[count++] = line[i]; } final_states = count; // total number of final states // reading...

  • Please rewrite this function using recursive function #include using namespace std; struct Node { char ch;...

    Please rewrite this function using recursive function #include using namespace std; struct Node { char ch; Node* next; }; class LinkedList { Node* head; public: LinkedList(); ~LinkedList(); void add(char ch); bool find(char ch); bool del(char ch); friend std::ostream& operator<<(std::ostream& out, LinkedList& list); }; LinkedList::LinkedList() { head = NULL; } LinkedList::~LinkedList() { Node* cur = head, * tmp; while (cur != NULL) { tmp = cur->next; delete cur; cur = tmp; } } void LinkedList::add(char ch) { Node* cur = head,...

  • Introduction In this lab, you are supposed to implement a graph class with the data structure...

    Introduction In this lab, you are supposed to implement a graph class with the data structure implemented before like linked list and queue. graph The class graph contains three member variables: linkedList *adjacentVertices; //an array of linked list. For a vertice i, adjacentVertices[i] stores the linked list that contains all other vertices connected to vertice i. int numVertices; //The number of vertices in the graph. int maxNumVertices; //The maximum number of vertices the graph can hold. Following public methods are...

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