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;

// Creating a structure for linked list
struct node{
int data;
struct node *link;
};
struct node* head=NULL; // Initially head will point to no node

// Function to display elements in linked list
void dis_ele()
{
cout<<"\nThe elements in linked list are: \n";
struct node* p;
p = head;
while(p!=NULL)
{
cout<<p->data<<" ";
p = p->link;
}
}

// Main function
int main()
{
int ele;
  
// Taking input from the user until he enters -1
while(true)
{
cout<<"Enter the number: ";
cin>>ele;
if(ele==-1)
{
dis_ele();
break;
}
else
{
struct node* temp = (struct node*) malloc(sizeof(struct node));
temp->data = ele;
temp->link = head; // storing address of head in newly created node
head = temp; // Now newly created node will become the first node
}
}
  
// Sorting using selection sort
int a;
struct node* q;
q = head;
while(q!=NULL)
{
node* min = q;
node* r = q->link;
  
// Finding min element
while(r!=NULL)
{
if(min->data > r->data)
{
min = r;
}
r = r->link;
}
  
//Swapping
a = q->data;
q->data = min->data;
min->data = a;
q = q->link;
}
cout<<"\n\nAfter Sorting:";
dis_ele();
  
// Searching
int x,found=0;
cout<<"\n\nEnter the element you want to search: ";
cin>>x;
  
struct node* search;
search = head;
while(search!=NULL)
{
if(search->data == x)
{
found=1;
break;
}
search = search->link;
}
  
if(found==1)
{
cout<<"The number you entered exists\n";
}
else
{
cout<<"The number you entered doesnot exists\n";
}
  
// Deleting
int y;
cout<<"\n\nEnter the element you want to delete: ";
cin>>y;
struct node* cur;
struct node* pre;
cur = head;
pre = NULL;
  
while(cur->link!=NULL && cur->data!=y)
{
pre = cur;
cur = cur->link;
}
  
if(cur->data == y)
{
pre->link = pre->link->link;
free(cur);
cout<<"\nEntered number is deleted from the list";
dis_ele();
}
else
{
cout<<"\nEntered number doesnot exist in the list";
}
return 0;
}

OUTPUT:


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...
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 ____...
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.
In C++, Implement the queue ADT with a singly linked list
In C++, Implement the queue ADT with a singly linked list
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...
Ask the user to input a series of numbers, write a C# program to output the...
Ask the user to input a series of numbers, write a C# program to output the sum, max, and min. Be sure to do error checking if the user input is not a number.
OBJECTIVE C 2) // Create a small app that takes user input and performs a useful...
OBJECTIVE C 2) // Create a small app that takes user input and performs a useful calculation with that user input // - make sure that absolutely no input crashes the app. // - Use layout constraints to make sure the app looks good in portraint and landscape mode, as well as on small and large devices.
Create a C++ program that will prompt the user to input an integer number and output...
Create a C++ program that will prompt the user to input an integer number and output the corresponding number to its numerical words. (From 0-1000000 only) **Please only use #include <iostream>, switch and if-else statements only and do not use string storing for the conversion in words. Thank you.** **Our class is still discussing on the basics of programming. Please focus only on the basics. Thank you.** Example outputs: Enter a number: 68954 Sixty Eight Thousand Nine Hundred Fifty Four...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT