question archive How do you develop a C++ menu driven program that lets the user to Add a number Print out all the numbers entered by the user

How do you develop a C++ menu driven program that lets the user to Add a number Print out all the numbers entered by the user

Subject:Computer SciencePrice:3.86 Bought8

How do you develop a C++ menu driven program that lets the user to

  • Add a number
  • Print out all the numbers entered by the user.?

The program should maintain a linked list to store all the numbers. When the user wants to add a number, a new node is created to store the number and the new node should be added to the linked list properly. You can choose to do that in the following 3 ways:

  • Always add the new node to the front of the linked list. Then the first number entered by the user will be printed out last.
  • Always add the new node to the end of the linked list. Then the numbers will be printed out in the order they had been entered.
  • Maintain the list as a sorted linked list. Then the numbers will be printed out in ascending order.

When a user wants to print all the numbers, your program should traverse the linked list, visit each node and print out the number stored in each node.

 

I can make a program that meets the requirements but I can't seem to implement a linked list to store all the numbers.

 

pur-new-sol

Purchase A New Answer

Custom new solution created by our subject matter experts

GET A QUOTE

Answer Preview

CODE:

#include<iostream>
using namespace std;


struct Node{
	int data;
	Node *next;
};


/*this function insert the node at first position*/
struct Node * insert_node(struct Node *head,int num){
	//allocate space to new node
	struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
	//store num as data of new Node
	newNode->data = num;
	
	//we have to insert at first poisiton
	//so newNode next will be head
	//and newNode will be assigned as head
	newNode->next = head;
	head=newNode;
	
	//return the head
	return head;
}


//function to insert the node at end position
/*struct Node * insert_node(struct Node *head,int num){
	struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
	newNode->data = num;
	
	//if the list is empty
	//new node will become head
	if(head==NULL){
		newNode->next = NULL;
		head = newNode;
	}
	//else traverse to the end of list and insert
	else{
		struct Node *temp = head;
		//traverse to the end of list
		while(temp->next!=NULL){
			temp = temp->next;
		}
		
		//insert the node at insert
		temp->next = newNode;
		newNode->next = NULL;
	}
	return head;
}*/


//function to insert the node at sorted position
/*
struct Node * insert_node(struct Node *head,int num){
	struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
	newNode->data = num;
	
	//if the list is empty
	//assign head to new node
	if(head==NULL){
		newNode->next = NULL;
		head = newNode;
	}
	
	//if the head is greater than num
	//than num will become new head
	else if(head->data>num){
		newNode->next = head;
		head = newNode;
	}
	//else traverse to correct position
	else{
		struct Node *temp = head;
		//traverse until end of list is found
		//or a node having value greater than num is found
		while(temp->next!=NULL && temp->next->data<num){
			temp = temp->next;
		}
		//insert the new node at correct position
		newNode->next = temp->next;
		temp->next = newNode;
	}
	return head;
}
*/
void printList(struct Node *head){
	struct Node *temp = head;
	while(temp!=NULL){
		cout<<temp->data<<" ";
		temp = temp->next;
	}
	cout<<endl;
}
int main(){
	struct Node *head=NULL;
	int choice=0;
	do{
		cout<<"1. Add a number: "<<endl;
		cout<<"2. Print out all the numbers: "<<endl;
		cout<<"3. Exit: "<<endl;
		cin>>choice;
		
		switch(choice){
			case 1:
				int num;
				cout<<"Enter the number to insert: ";
				cin>>num;
				head = insert_node(head,num);
				break;
			case 2:
				printList(head);
				break;
			case 3:
				break;
			default:
				cout<<"Enter correct choice: "<<endl;
		}
	}while(choice!=3);
}

 

OUTPUT USING INSERT AT FIRST:-

1. Add a number:

2. Print out all the numbers:

3. Exit:

1

Enter the number to insert: 14

1. Add a number:

2. Print out all the numbers:

3. Exit:

1

Enter the number to insert: 12

1. Add a number:

2. Print out all the numbers:

3. Exit:

1

Enter the number to insert: 19

1. Add a number:

2. Print out all the numbers:

3. Exit:

1

Enter the number to insert: 24

1. Add a number:

2. Print out all the numbers:

3. Exit:

2

24 19 12 14

1. Add a number:

2. Print out all the numbers:

3. Exit:

3

 

 

OUTPUT USING INSERT AT LAST:-

1. Add a number:

2. Print out all the numbers:

3. Exit:

1

Enter the number to insert: 2

1. Add a number:

2. Print out all the numbers:

3. Exit:

1

Enter the number to insert: 5

1. Add a number:

2. Print out all the numbers:

3. Exit:

1

Enter the number to insert: 4

1. Add a number:

2. Print out all the numbers:

3. Exit:

1

Enter the number to insert: 10

1. Add a number:

2. Print out all the numbers:

3. Exit:

2

2 5 4 10

1. Add a number:

2. Print out all the numbers:

3. Exit:

1

Enter the number to insert: 6

1. Add a number:

2. Print out all the numbers:

3. Exit:

1

Enter the number to insert: 8

1. Add a number:

2. Print out all the numbers:

3. Exit:

2

2 5 4 10 6 8

1. Add a number:

2. Print out all the numbers:

3. Exit:

3

 

OUTPUT USING INSERT AT SORTED :-

?1. Add a number:

2. Print out all the numbers:

3. Exit:

1

Enter the number to insert: 14

1. Add a number:

2. Print out all the numbers:

3. Exit:

1

Enter the number to insert: 10

1. Add a number:

2. Print out all the numbers:

3. Exit:

1

Enter the number to insert: 12

1. Add a number:

2. Print out all the numbers:

3. Exit:

1

Enter the number to insert: 19

1. Add a number:

2. Print out all the numbers:

3. Exit:

2

10 12 14 19

1. Add a number:

2. Print out all the numbers:

3. Exit:

3