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.
Could you modify the code make output arranged into 5x5, like this: 1 2 3 4...
Could you modify the code make output arranged into 5x5, like this: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 The code is: import java.util.*; public class RandomGen { public static void main(String[] args) { int[][] ArrayRand = new int [5][5]; Random rand = new Random(); int sum = 0; System.out.println("The generated random matris is: ");    for(int i = 0; i <...
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...
For java code: Create a library to do it all. The attached file has all the...
For java code: Create a library to do it all. The attached file has all the directions and methods that need to be included in your library (.jar). You will create a .jar file, and a tester app that will include your .jar file (Add External JAR). The attached file below: Create a java project called MyLibrary, a package called net.dtcc.lib, and a class called AllInOne. The class should have the following methods, each method should return the answer: Area:...
1. Let R be the relation on A = {1, 2, 3, 4, 5} given by...
1. Let R be the relation on A = {1, 2, 3, 4, 5} given by R = {(1, 1),(1, 3),(2, 2),(2, 4),(2, 5),(3, 1),(3, 3),(4, 2),(4, 4),(4, 5),(5, 2),(5, 4),(5, 5)}. (a) Draw the digraph which represents R. (b) Give the 0 -1 matrix of R with respect to the natural ordering. (c) Which of the five properties (reflexive, irreflexive, symmetric, antisymmetric, transitive) does R have? Give a brief reason why or why not each property holds. 2. Let...
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...
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
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