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...
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
Python 3 Fix the code. It is not saving the data into the xls file Code:...
Python 3 Fix the code. It is not saving the data into the xls file Code: import tkinter as tk from tkcalendar import DateEntry from openpyxl import load_workbook from tkinter import messagebox from datetime import datetime window = tk.Tk() window.title("daily logs") window.grid_columnconfigure(1,weight=1) window.grid_rowconfigure(1,weight=1) # labels tk.Label(window, text="Bar code").grid(row=0, sticky="W", pady=20, padx=20) tk.Label(window, text="Products failed").grid(row=1, sticky="W", pady=20, padx=20) tk.Label(window, text="Money Lost").grid(row=2, sticky="W", pady=20, padx=20) tk.Label(window, text="sold by").grid(row=3, sticky="W", pady=20, padx=20) tk.Label(window, text="Working product").grid(row=4, sticky="W", pady=20, padx=20) #Working product label tk.Label(window, text="Failed...
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...
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/ ∼ .
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT