Question

In: Computer Science

IN C++. Objective: Create a Singly linked list of numbers based upon user input. Program logic:...

IN C++.

Objective: Create a Singly linked list of numbers based upon user input. Program logic: Ask for a number, add that number to the front of the list, print the list. Repeat until they enter -1 for the number. . Sample Input: 10, 15, 5, 2, 4, -1 Output: 4, 2, 5, 15, 10.

Next sort all the numbers using selection sort and display them. Next give the user option to search for a specific number in the list. If the user desired number exist output "the number you entered exists" else output "this number does not exist".

Next ask user if they want to delete a specific number. Delete and display an appropriate message. If the user specified number does not exist display an appropriate message.

Don't use class. Structs and nodes only

Solutions

Expert Solution

Code

#include<iostream>
using namespace std;
void sorted_ins(struct Node **,struct Node *);
struct Node
{
    int value;
    struct Node *next;
};

struct Node *head=NULL;

void add(struct Node **head,int v)
{
    struct Node *node = new Node;
    node->value=v;
    node->next=*head;
    *head=node;
}

void selection_sort(struct Node **head)
{
    struct Node* temp = *head;
    while(temp)
    {
        struct Node* min = temp;
        struct Node* r = temp->next;
        while(r)
        { 
            if(min->value > r->value)
                min = r; 
            r = r->next;
        } 
        int x = temp->value;
        temp->value= min->value;
        min->value= x;
        temp = temp->next;
    }
}


void del(struct Node **head,int d)
{
    struct Node *current=NULL;
    struct Node *prev=NULL;
    if((*head)->value != d && (*head)->next == NULL) 
    {
        printf("%d not found in the list\n", d);
         return;
    }
    current=*head;
    while(current->next != NULL && current->value != d)
    {
        prev = current;
        current = current->next;
    }
    if(current->value == d)
    {
        prev->next = prev->next->next;
        cout<<d<<" has been deleted successfully";
        free(current);
    }
    else
        printf("%d not found in the list.", d);
}
void search(struct Node** head,int v)
{
    struct Node *current=*head;
    while(current!=NULL)
    {
        if(current->value == v)
        {
            printf("the number you entered exists\n");
            return;
        }
        current = current->next;
    }
    printf("the number you entered does not exist",v);
} 
void print(struct Node** head)
{
    struct Node * temp=*head;
    while(temp!=NULL)
    {
        printf("%d ",temp->value);
        temp=temp->next;
    }
}
int main()
{
    struct Node *s=NULL;
    int d;
    while(1) //store  value to list till -1 entered 
    {
        cin>>d;
        if(d==-1)
            break;
        add(&s,d);
    }
    cout<<"inserted elements \n";
    print(&s);
    selection_sort(&s);
    cout<<"\nelements after sort\n";
    print(&s);
    cout<<"\nenter a number to search\n";
    cin>>d;
    search(&s,d); //search the element
    cout<<"enter a number to delete\n";
    cin>>d;
    del(&s,d); //delete the element
    cout<<"\nelememts after deletion\n";
    print(&s);
    return 0;
}

Terminal Work

.


Related Solutions

IN C++. Objective: Create a Singly linked list of numbers based upon user input. Program logic:...
IN C++. Objective: Create a Singly linked list of numbers based upon user input. Program logic: Ask for a number, add that number to the front of the list, print the list. Repeat until they enter -1 for the number. . Sample Input: 10, 15, 5, 2, 4, -1 Output: 4, 2, 5, 15, 10. Next sort all the numbers using selection sort and display them. Next give the user option to search for a specific number in the list....
Exercise 1: Write a program in Java to manipulate a Singly Linked List: 1. Create Singly...
Exercise 1: Write a program in Java to manipulate a Singly Linked List: 1. Create Singly Linked List 2. Display the list 3. Count the number of nodes 4. Insert a new node at the beginning of a Singly Linked List. 5. Insert a new node at the end of a Singly Linked List 6. Insert a new node after the value 5 of Singly Linked List 7. Delete the node with value 6. 8. Search an existing element in...
Using C++, Create a singly Linked List of patients list so that you can sort and...
Using C++, Create a singly Linked List of patients list so that you can sort and search the list by last 4 digits of the patient's Social Security number. Implement Node insertion, deletion, update and display functionality in the Linked List. Each patient's record or node should have the following data: Patient Name: Age: Last 4 digits of Social Security Number: The program should first display a menu that gives the user the option to: 1) Add a Patient's record...
Create a program that implements a singly linked list of Students. Each node must contain the...
Create a program that implements a singly linked list of Students. Each node must contain the following variables: Student_Name Student_ID In main(): Create the following list using addHead(). The list must be in the order shown below. Student_ID Student_Name 00235 Mohammad 00662 Ahmed 00999 Ali 00171 Fahad Print the complete list using toString() method. Create another list using AddTail(). The list must be in the order shown below. Print the complete list using toString() method. Delete head note from both...
Create a program that implements a singly linked list of Students. Each node must contain the...
Create a program that implements a singly linked list of Students. Each node must contain the following variables: Student_Name Student_ID In main(): Create the following list using addHead(). The list must be in the order shown below. Student_ID Student_Name 00235 Mohammad 00662 Ahmed 00999 Ali 00171 Fahad Print the complete list using toString() method. Create another list using AddTail(). The list must be in the order shown below. Student_ID Student_Name 00236 Salman 00663 Suliman 00998 Abdulrahman Print the complete list...
Create the logic for a rhyming program that takes in five words from a user input...
Create the logic for a rhyming program that takes in five words from a user input and replaces the selected rhyming words with the user's input, then creates and displays the Humpty Dumpty rhyme with the five words from the user input. Hint: 1. You will use the sentinel values: start and stop 2. The program will ask the user for 5 words. 3. The program will store the 5 words 4. The program outputs the rhyme replacing the ____...
In C++, Implement the queue ADT with a singly linked list
In C++, Implement the queue ADT with a singly linked list
Create a program that asks the user to input three numbers and computes their sum. This...
Create a program that asks the user to input three numbers and computes their sum. This sounds simple, but there's a constraint. You should only use two variables and use combined statements. You can use the output below as a guide. Please enter the first number: 4 Please enter the second number: 2 Please enter the third number: 9 The sum of the three numbers is: 15.
C Program: Create a C program that prints a menu and takes user choices as input....
C Program: Create a C program that prints a menu and takes user choices as input. The user will make choices regarding different "geometric shapes" that will be printed to the screen. The specifications must be followed exactly, or else the input given in the script file may not match with the expected output. Important! Consider which control structures will work best for which aspect of the assignment. For example, which would be the best to use for a menu?...
C Program: Create a C program that prints a menu and takes user choices as input....
C Program: Create a C program that prints a menu and takes user choices as input. The user will make choices regarding different "geometric shapes" that will be printed to the screen. The specifications must be followed exactly, or else the input given in the script file may not match with the expected output. Your code must contain at least one of all of the following control types: nested for() loops a while() or a do-while() loop a switch() statement...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT