Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Program Using the basic Linked List program, (1) Create a linked list from f

ID: 3571194 • Letter: C

Question

C++ Program

Using the basic Linked List program,
(1) Create a linked list from file input data
(2) Print the linked list, with 10 values per line

Add new functions:

(1) A function gets user input of a value to look for in the list, and reports how many times it

appears (could be 0)

(2) A function counts the nodes in a list and reports how many there are.

(3) A function gets user input of a value to delete, and deletes the first node found which has

that value. (Does it matter whether that node is first, last, or in the middle of the list?)

The main program calls the functions and shows that they work.

Explanation / Answer

Create File nums.txt and put integers in it.

#include<iostream.h>
#include<fstream.h>
#include<string.h>
#include<conio.h>
#include<stdio.h>
using namespace std;
// Node structure
struct Node {
    int data;        // The data being stored at the node
    Node *pNext;     // Pointer to the next node
};
//-----------------------------------------------------------------
// Traverse the list and display the data at each node
void displayList( const char message, Node pTemp)
{
    cout << message<< endl;
    int i=0;
    while( pTemp != NULL) {
        cout << pTemp->data << " ";
        pTemp = pTemp->pNext;
        i++;
        if(i==10)
        {
   cout << endl;
   i=0;
}
    }
    cout << " ";
}//end displayList()

// Prepend a new node with the given number, at the front of the list
void prependNode( Node *&pHead, int number)
{
    // Get a new node
    Node *pTemp = new Node;
    // Store the values into the new node
    pTemp->data = number;
    pTemp->pNext = pHead;
    // Make this new node the new front of the list
    pHead = pTemp;
}//end prependNode()


// Read numbers and place them on a linked list
void readNumbers( Node *&pHead)
{
          
  
    ifstream myfile;
    myfile.open("nums.txt");

    int a;
    while (myfile >> a)
    {
         prependNode( pHead, a);

    }

    }


// Return number of times number is found on the list.
int searchNum( int numberToFind, Node *pHead)
{
Node *pTemp=pHead;
    int n=0;
  
    while( pTemp != NULL) {
   
        if(pTemp->data == numberToFind)
        {
    n++;
     }      
        pTemp = pTemp->pNext;
    }
  
    return n;
  

  
  
}//end searchNum()

bool deleteNum( int numberToDel, Node *pHead)
{
Node *pTemp=pHead;
    int n=0;
    bool f=false;
  
    if(pHead!=NULL)
    {
if(pHead->data== numberToDel)
{

   f=true;

   pHead=pHead->pNext;

}
else   
     while( pTemp->pNext != NULL) {
   
         if(pTemp->pNext->data == numberToDel)
         {
     pTemp->pNext=pTemp->pNext->pNext;
     f=true;
     break;
      }      
         pTemp = pTemp->pNext;
     }
}

  
    return f;
  

  
  
}//end deleteNum()
int countNodes(Node *pHead)
{
Node *pTemp=pHead;
    int n=0;
  
    while( pTemp != NULL) {   
     
   n++;
         
        pTemp = pTemp->pNext;
    }
  
    return n;
  

  
  
}//end searchNum()


int main()
{
    Node *pHead = NULL;    // head of linked list of numbers
    int n;      // Number to search for on linked list
  
  
  
    // Read Numbers from file nums.txt of numbers and display them.
    readNumbers( pHead);
    displayList( "List of numbers is: ", pHead);
  
    // Prompt for number to find, and see if it is on the linked list.
    cout << "Enter the number to look for: ";
    cin >> n;
    cout << "Number was found "<<searchNum( n, pHead) <<" times in the list ";
  
  
    // Prompt for number to delete, and delete if it is on the linked list.
    cout << "Enter the number to delete for: ";
    cin >> n;
    if(deleteNum( n, pHead)) cout << "Number deleted ";
    else cout << "Number not Found ";  
  
  
    displayList( "After deletion , list is : ", pHead);
  
    cout << "Number of Nodes in the list "<<countNodes(pHead);
  
    return 0;         // return value to keep C++ happy
}//end main()

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote