1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
vagabundo [1.1K]
3 years ago
13

Develop a program that will maintain an ordered linked list of positive whole numbers. Your program will provide for the followi

ng options: a. Add a number b. Delete a number c. Search for a number d. Display the whole list of numbers At all times, the program will keep the list ordered (the smallest number first and the largest number last).
Computers and Technology
1 answer:
Fed [463]3 years ago
6 0

Answer:

#include <iostream>

using namespace std;

struct entry

{

int number;

entry* next;

};

void orderedInsert(entry** head_ref,entry* new_node);

void init_node(entry *head,int n)

{

head->number = n;

head->next =NULL;

}

void insert(struct entry **head, int n)

{

entry *nNode = new entry;

nNode->number = n;

nNode->next = *head;

*head = nNode;

}

entry *searchNode(entry *head, int n)

{

entry *curr = head;

while(curr)

{

if(curr->number == n)

return curr;

curr = curr->next;

}

}

bool delNode(entry **head, entry *ptrDel)

{

entry *curr = *head;

if(ptrDel == *head)

{

*head = curr->next;

delete ptrDel;

return true;

}

while(curr)

{

if(curr->next == ptrDel)

{

curr->next = ptrDel->next;

delete ptrDel;

return true;

}

curr = curr->next;

}

return false;

}

void display(struct entry *head)

{

entry *list = head;

while(list!=NULL)

{

cout << list->number << " ";

list = list->next;

}

cout << endl;

cout << endl;

}

//Define the function to sort the list.

void insertionSort(struct entry **h_ref)

{

// Initialize the list

struct entry *ordered = NULL;

// Insert node to sorted list.

struct entry *current = *h_ref;

while (current != NULL)

{

struct entry *next = current->next;

// insert current in the ordered list

orderedInsert(&ordered, current);

// Update current

current = next;

}

// Update the list.

*h_ref = ordered;

}

//Define the function to insert and traverse the ordered list

void orderedInsert(struct entry** h_ref, struct entry* n_node)

{

struct entry* current;

/* Check for the head end */

if (*h_ref == NULL || (*h_ref)->number >= n_node->number)

{

n_node->next = *h_ref;

*h_ref = n_node;

}

else

{

//search the node before insertion

current = *h_ref;

while (current->next!=NULL &&

current->next->number < n_node->number)

{

current = current->next;

}

//Adjust the next node.

n_node->next = current->next;

current->next = n_node;

}

}

int main()

{

//Define the structure and variables.

char ch;int i=0;

entry *newHead;

entry *head = new entry;

entry *ptr;

entry *ptrDelete;

//Use do while loop to countinue in program.

do

{

//Define the variables

int n;

int s;

int item;

char choice;

//Accept the user choice

cout<<"Enter your choice:"<<endl;

cout<<"a. Add a number"<<endl

<<"b. Delete a number"<<endl

<<"c. Search for a number"<<endl

<<"d. Display the whole list of numbers"<<endl;

cin>>choice;

//Check the choice.

switch(choice)

{

//Insert an item in the list.

case 'a' :

// cin>>item;

cout<<"Enter the element:"<<endl;

cin>>item;

//To insert the first element

if(i==0)

init_node(head,item);

//To insert remaining element.

else

{

ptr = searchNode(head,item);

//Check for Duplicate data item.

if(ptr==NULL)

{

insert(&head,item);

}

else

{

cout<<"Duplicate data items not allowed";

cout<<endl<<"EnterAgain"<<endl;

cin>>item;

insert(&head,item);

}

}

insertionSort(&head);

i=i+1;

break;

//Delete the item from the list

case 'b' :

int numDel;

cout<<"Enter the number to be deleted :"<<endl;

cin>>numDel;

//Locate the node.

ptrDelete = searchNode(head,numDel);

if(ptrDelete==NULL)

cout<<"Element not found";

else

{

if(delNode(&head,ptrDelete))

cout << "Node "<< numDel << " deleted!\n";

}

break;

//Serach the item in the list.

case 'c' :

cout<<"Enter the element to be searched :";

cout<<endl;

cin>>s;

ptr = searchNode(head,s);

if(ptr==NULL)

cout<<"Element not found";

else

cout<<"Element found";

break;

//Display the list.

case 'd' :

display(head);

break;

default :

cout << "Invalid choice" << endl;

break;

}

//Ask user to run the program again

cout<<endl<<"Enter y to countinue: ";

cin>>ch;

}while(ch=='y'||ch=='Y');

return 0;

}

output:

You might be interested in
True or false A ClassB fire involves live electrical equipment
MAVERICK [17]
This is false. A Class B fire consists of highly combustible/flammable liquids and gases such as petroleum oil and propane.
4 0
3 years ago
Typically, what form do most database designers consider a database structure to be normalized?
klemol [59]
Hello,

Answer is Third 

Hope This Help!!!
6 0
3 years ago
Martha pays $3,335 every month for her house payment. She has made 86 payments up till now. How much has Martha paid for her hou
kotegsom [21]

Answer:

286810

Explanation:

86*3335=286810

8 0
3 years ago
I need the answers. i don’t get this
bixtya [17]
Run,lazy,turtles,slow,wandering, jump,45,weird,pigs, cows, cousins, pale,rude,candles

There are many answers for this but this is what first came to my mind

Hope this helps
4 0
3 years ago
Write a program called DeliveryCharges for the package delivery service. The program should use an array that holds the 10 zip c
Vlad [161]

Answer:

import java.util.Scanner;

public class DeliveryCharges

{

public static void main(String[] args) {

 

       String[] zips = {"01234", "11234", "21234", "31234", "41234", "51234", "61234", "71234", "81234", "91234"};

    double[] prices = {2.2, 1.0, 3.6, 6, 9, 7.1, 0.8, 4.7, 3.3, 5.2};

    int index = -1;

    Scanner ob = new Scanner(System.in);

    System.out.print("Enter the zip code for your delivery: ");

    String zip = ob.next();

   

    for (int i=0; i<10; i++) {

        if (zip.equals(zips[i])) {

            index = i;

        }

    }

       

    if (index!= -1)

       System.out.println("Delivery charge to " + zips[index] + " is: " + prices[index]);

    else

       System.out.println("No delivery to " + zip);

}

}

Explanation:

Initialize the zips and prices

Initialize index that represents the index of the zip code. If the entered zip code is in the array

Ask the user to enter the zip code

Create a for loop that iterates through the zips. If the entered zip is in the zips, set its index to index

When the loop is done, check if the index. If the index is not -1, that means zip code was found in the zips array, print the corresponding price from the price array. Otherwise, print no delivery for the entered zip code

8 0
4 years ago
Other questions:
  • James was having coffee and overhead a conversation between two men who talked about the fact that their friend could not find a
    10·1 answer
  • Assume there is a Mac and a HP computer each has an Intel microprocessor. Would an assembly program produced using the Mac compu
    13·1 answer
  • Binary data is written in hexadecimal. For example, when creating a graphic for a website, colors are represented by six hexadec
    12·1 answer
  • How to import any csv from internet in phyton 3 and R ?
    12·1 answer
  • True or false: a game design document is like a diary for game developers
    13·2 answers
  • When it's time to change career paths, it's a good idea to first​
    7·2 answers
  • What is the output of the following code snippet? int s1 = 20; if (s1 &lt;= 20) { System.out.print("1"); } if (s1 &lt;= 40) { Sy
    6·1 answer
  • Explain the procedure you will undertake to create a new partition​
    6·1 answer
  • You recently created a new network segment for the development department. Because the hosts are now on a different network segm
    6·1 answer
  • Let A and B be regular languages. Is the set of strings of odd length from A beginning with 0 concatenated with the set of strin
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!