Question

In: Computer Science

The code file is attached. Portion of the code is given (1, 2, 3, 4). You...

The code file is attached. Portion of the code is given (1, 2, 3, 4). You have to write the code for the remaining items (5, 6, 7).

#include <iostream>
using namespace std;
struct node 
{
   int data;
   node* next;
};

node* head=NULL;

void appendNode(int value)
{
        node *newNode, *curr;
        newNode = new node();
        newNode->data=value;
        newNode->next=NULL;
        if (!head)  
                head=newNode;
        else 
        {
                curr=head;
                while (curr->next)
                        curr = curr->next;
                curr->next = newNode;
        }
}

void insertNode(int value)
{
        node *newNode, *curr, *previous;
        newNode = new node();
        newNode->data = value;
        if (head==NULL) 
        {
                head = newNode;
                newNode->next = NULL;
        }       
        else 
        {
                curr = head;
                while (curr != NULL && curr->data < value) 
                {
                        previous = curr;
                        curr = curr->next;
                }
                if (previous == NULL)
                {       
                        head = newNode;
                        newNode->next = curr;
                }
                else 
                {       
                        previous->next = newNode;
                        newNode->next = curr;
                }
        }       
}

void deleteNode(int value)
{
        node *curr, *previous;
        if(!head)
           return;
        if (head->data == value)
        {
                curr = head->next;
                delete head;
                head = curr;
        }
        else
        {
                curr = head;
                while( curr != NULL && curr->data != value)
                {
                        previous=curr;
                        curr=curr->next;
                }               
                previous->next = curr->next;
                delete curr;
        }
        cout << "Value is deleted\n";

}

void displayList(void)
{ 
        node *curr;
        curr = head;
        while (curr != NULL)
        {
                cout << curr->data << " " ;
                curr = curr->next;
        }
        cout << "\n";
}

int     findMaxNode()
{
        int m;
        //complete the code here
        return m;
}

int     searchItem(int value)
{
        int f;
        //complete the code here
        return f;
}

int     countNodes()
{
        int c;
        //complete the code here
        return c;
}

int main()
{
        int choice;
        int x;
        do
        {
                system("cls");
                cout << "1. Append a node" << "\n";
                cout << "2. Insert a node" << "\n";
                cout << "3. Delete a node" << "\n";
                cout << "4. Display the linked list" << "\n";
                cout << "5. Find the maximum node" << "\n";
                cout << "6. Search for a node" << "\n";
                cout << "7. Count the number of the nodes " << "\n";
                cout << "8. Exit" << "\n";
                cout << "Enter your choice(1-8):";
                cin >> choice;
                switch(choice)
                {
                        case 1:
                        cout << "Enter a value:";
                        cin >> x;
                        appendNode(x);
                        cout << "Value is appeneded\n";
                        break;

                        case 2:
                        cout << "Enter a value to insert:";
                        cin >> x;
                        insertNode(x);
                        cout << "Value is inserted\n";
                        break;

                        case 3:
                        cout << "Enter a value to delete:";
                        cin >> x;
                        deleteNode(x); 
                        break;

                        case 4:
                        displayList();
                        break;
                        
                        case 5:
                        int max;
                        max = findMaxNode();
                        cout << "Maximum is :" << max << endl;
                        break;

                        case 6:
                        cout << "Enter value to search for\n";
                        cin >> x;
                        int flag;
                        flag= searchItem(x);
                        if(flag==1)
                                cout << "Found\n";
                        else
                                cout << "Not found\n";
                        break;
                
                        case 7:
                        int count;
                        count= countNodes();
                        cout<<"Number of nodes in the list is:" << count << endl;
                        break;
                }
                        
                system("pause");
                
        } while(choice!=8);
}

Solutions

Expert Solution

5)

To find the max of a node first assume the first element of the list as the maximum element. Next, traverse the entire list and see if the value of that node is greater than the current maximum. Update m with new maximum. If list is empty return -1 indicating list is empty hence no maximum

int findMaxNode()
{
int m;
node *cur;
cur=head;
if(cur==NULL)
{
return -1;
}
else
m=cur->data;
while (cur != NULL)
{
if(m<cur->data)
m=cur->data;
cur = cur->next;
}
return m;
}

6)

Simple traverse the entire list and see if data in node matches with search value.

int searchItem(int value)
{
int f=0;
node *cur;
cur=head;
while (cur != NULL)
{
if(cur->data==value)
{
f=1;
break;
}
cur = cur->next;
}
return f;
}

7)

To count the no of nodes simple maintain a counter while traveling across the nodes. Increment the value of the counter upon visiting each node.

int countNodes()
{
int c=0;
node *cur;
cur=head;
while (cur != NULL)
{
c++;
cur = cur->next;
}
return c;
}


Related Solutions

I need these written in shell code 1.nested loop. e.g. 1*2 + 2*3 + 3*4 +...
I need these written in shell code 1.nested loop. e.g. 1*2 + 2*3 + 3*4 + ...(n-1)*n. (Only nested loops) 2.Fibonacci numbers.
SE-FamilySize 1 1 4 3 2 4 2 3 4 2 4 1 4 2 2...
SE-FamilySize 1 1 4 3 2 4 2 3 4 2 4 1 4 2 2 4 5 4 5 4 4 2 4 3 1 2 3 5 5 5 Make a confidence interval. Be sure you show all the steps you took. Include a screen shot of any applet you used in your calculations. 2. Choose a confidence level (1 – α). 3. What is xbar? 4. What is s? 5. What is t? (Show a screen shot...
Given the StudentIDDriver.java file, fill in the ‘gaps’ in the code (designated by insert code here...
Given the StudentIDDriver.java file, fill in the ‘gaps’ in the code (designated by insert code here that “exercises” (tests) the StudentID class by calling its methods. There are six occurrences of ‘insert code here comments’. Above these are comments that help you understand what your code should accomplish.   Don’t forget to comment your StudentIDDriver.java file (including a prologue section at the top of the file). Create a project in Eclipse for your StudentIdDriver. Add another class for it StudentId Sample...
write a java code to calculate 1+2-3+4-5 …-99+100
write a java code to calculate 1+2-3+4-5 …-99+100
Given the following dataset x   1   1   2   3   4   5 y   0   2   4   5  ...
Given the following dataset x   1   1   2   3   4   5 y   0   2   4   5   5   3 We want to test the claim that there is a correlation between xand y. The level of cretaine phosphokinase (CPK) in blood samples measures the amount of muscle damage for athletes. At Jock State University, the level of CPK was determined for each of 25 football players and 15 soccer players before and after practice. The two groups of athletes are trained...
Given the following dataset x 1 1 2 3 4 5 y 0 2 4 5...
Given the following dataset x 1 1 2 3 4 5 y 0 2 4 5 5 3 We want to test the claim that there is a correlation between xand y. (a) What is the null hypothesis Ho and the alternative hypothesis H1? (b) Using α= 0.05, will you reject Ho? Justify your answer by using a p-value. (c) Base on your answer in part (b), is there evidence to support the claim? (d) Find r, the linear correlation...
(LANGUAGE: C++) 1)     For this lab, you will fill in what’s missing to make the attached file...
(LANGUAGE: C++) 1)     For this lab, you will fill in what’s missing to make the attached file cust_leads.cpp work 2)     The Lead class is for a very limited amount of information about a potential customer ·       Include string fields for name and email ·       A constructor that takes values for these two fields ·       Include a getter for the email field ·       Overload the == operator ·       2 Lead’s are equal if the names are the same (the emails don’t need to match) ·       the first few lines...
DIET ANALYSIS PART 4 : NO FILE NEEDS TO BE ATTACHED FOR THIS SECTION. Cannot submit...
DIET ANALYSIS PART 4 : NO FILE NEEDS TO BE ATTACHED FOR THIS SECTION. Cannot submit part 4 if you have not submitted part 1-3. Review SAM: SUBJECT: SAM Age: 39 Birthdate:7/14/1980 Height: 6 feet 2 inches Weight: 312 pounds Waist circumference: 65 inches Sam works at the local Wells Fargo Bank. He is a Branch Manager He works 40 hours per week. Sam spends a lot of his time behind a desk working for his clients. He smokes cigarette...
Let X = {1, 2, 3, 4, 5, 6} and let ∼ be given by {(1,...
Let X = {1, 2, 3, 4, 5, 6} and let ∼ be given by {(1, 1),(2, 2),(3, 3),(4, 4),(5, 5),(6, 6),(1, 3),(1, 5),(2, 4),(3, 1),(3, 5), (4, 2),(5, 1),(5, 3)}. Is ∼ an equivalence relation? If yes, write down X/ ∼ .
3. Given the following GPA for 4 students: 2, 3, 3, 4. Age of students are:...
3. Given the following GPA for 4 students: 2, 3, 3, 4. Age of students are: 10, 11, 12, 15. There are 10,000 total students. Average GPA: Unknown, Standard Deviation of GPA: 0.02 and it’s Normal. d. Find Regression Line Equation. (5 points) e. If a student’s age is 13, what is the corresponding GPA? (5 points) Please show the work, thanks
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT