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
In total, how many 8-bit registers are there in the Intel 80x86 CPU design presented in class? Name one of these 8-bit registers
RSB [31]

Answer:

In general the number of  bit registers in Intel 80x86 CPU design when combined together forms a 16 - bit register

An example of the  -bit registers are AH, AL, BH, BL, CH, CL, DH, and DL

Explanation:

Solution

The 8086 CPU design has a total of eight 8-bit registers and these register can be integrated together to make 16- bit register as well.

The 16-bit data is stored by breaking the data into a low-order byte and high order byte.

The name of the 8 bit registers is shown below:

AH, AL, BH, BL, CH, CL, DH, and DL

7 0
2 years ago
You want to create a directory called ~/Documents/papers, but you’re not sure that the ~/Documents directory exists. What comman
SCORPION-xisa [38]

Answer: e) mkdir -p ~/Documents/papers

Explanation:

As the question says we want to create a directory called ~/Documents/papers, but we are not sure whether it exist or not so we would type the command for making directory starting with mkdir.

Here mkdir means make directory. Followed by mkdir we will use -p. Here p is used to create all the directories for getting us the directory we want and its advantage is that it would ignore all the errors if the directory already exist.

Followed by the name of the directory that we want to create.

So the desired answer for the given condition would be mkdir -p ~/Documents/papers

5 0
2 years ago
Ok i dont wanna get bannddddddd so i have to ask a qustion<br> 2 plus 2
Evgesh-ka [11]

Answer:

22 or 4

Explanation:

:)

3 0
2 years ago
Read 2 more answers
Make a hierarchical directory structure under /root that consists of one directory containing three subdirectories.
Alika [10]
You can make a hierarchical directory structure under /root that consists of one directory containing subdirectories  by using cd and mkdir
the mkdir command created the directories while the cd command changes which directory you're currently in
5 0
2 years ago
Based on the information in the table, which of the following tasks is likely to take the longest amount of time when scaled up
kakasveta [241]

Answer:

Task A

Explanation:

8 0
3 years ago
Other questions:
  • What do you call an unsolicited email message that masquerades as coming from a legitimate sender, and attempts to get you to di
    11·1 answer
  • The person who can give a short defination of subroutine will get the brainliest.
    9·1 answer
  • How can you have a safe browser experience
    9·1 answer
  • Write a program that asks the user to input
    11·1 answer
  • How did you generate a random number for the user to guess?
    9·1 answer
  • The table button is present in the ……… tab.​
    14·2 answers
  • Question 1 (1 point)
    9·2 answers
  • What goes in between the &lt; &gt; when declaring a new ArrayList?
    14·1 answer
  • Moving images of real event​
    12·2 answers
  • 1. what is the purpose of giving an id to an html element when using javascript?
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!