Question

In: Computer Science

I am having trouble with printing the linked list from user input. Can someone tell me...

I am having trouble with printing the linked list from user input. Can someone tell me what I am doing wrong.

#include <iostream>
using namespace std;
struct node
{
   int info;
   node*link;
   node*current;
   node*prev;
   node * head;
   node * last;
}
;

void Insert(node*&head,node*&last,int n);
int SplitList(node*&head);
void print(node*&head);
int main()
{
   int num;
   node*h;
   node*l;
   cout << "Enter numbers ending with -999" << endl;
   cin >> num;
   Insert(h, l, num);
   print(h);
}

void Insert(node *&head,node*&last,int n)
{
   int count = 0;
   if (head == NULL)
   {
       head = new node;
       head->info = n;
       head->link = NULL;
       last = head;
       count++;
   }
   else {
       last->link = new node;
       last->link->info = n;
       last->link->link = NULL;
       last = last->link;
       count++;
   }
}
void print(node*&head)
{
   node *current; //pointer to traverse the list
   current = head; //set current so that it points to
                   //the first node
   while (current->link!= NULL) //while more data to print
   {
       cout << current->info << " ";
       current = current->link;
   }
}//end print

Solutions

Expert Solution

#include <iostream>
using namespace std;
struct node
{
int info;
node*link;
node*current;
node*prev;
node * head;
node * last;
}
;

void Insert(node *&head,node *&last,int n);
int SplitList(node* head);
void print(node* head);
int main()
{
int num;
node*h=NULL;
node*l=NULL;
cout << "Enter numbers ending with -999" << endl;
while(1)//added loop to run until user enters -999
{
cin >> num;
if(num==-999)//if -999 is entered
break;
Insert(h, l, num);

}
print(h);
}

void Insert(node *&head,node *&last,int n)
{
int count = 0;
if (head == NULL)
{
head = new node;
head->info = n;
head->link = NULL;
last = head;
count++;
}
else {
last->link = new node;
last->link->info = n;
last->link->link = NULL;
last = last->link;
count++;
}
}
void print(node *head)
{
node *current; //pointer to traverse the list
current = head; //set current so that it points to
//the first node
while (current!= NULL) //while more data to print
{
cout << current->info << " ";
current = current->link;
}
}//end print

output:

Enter numbers ending with -999
1
2
3
-999
1 2 3

Process exited normally.
Press any key to continue . . .



Related Solutions

can someone tell me if i am wrong on any of these???? THANKS In order to...
can someone tell me if i am wrong on any of these???? THANKS In order to be able to deliver an effective persuasive speech, you need to be able to detect fallacies in your own as well as others’ speeches. The following statements of reasoning are all examples of the following fallacies: Hasty generalization, mistaken cause, invalid analogy, red herring, Ad hominem, false dilemma, bandwagon or slippery slope. 1. __________bandwagon fallacy_______ I don’t see any reason to wear a helmet...
I am having trouble figuring out how to To-Do list using java. Can anyone guide me...
I am having trouble figuring out how to To-Do list using java. Can anyone guide me through the steps for achieving this?
Hi! Can someone summarize what the reaction article is saying? I am having trouble understanding it....
Hi! Can someone summarize what the reaction article is saying? I am having trouble understanding it. The purpose of this experiment was to investigate the catalytic performance of solid acid catalyst developed from modification of coal combustion fly ash to synthesize methyl 4-aminobenzoate (MAB) by Fischer esterification between 4-aminobenzoic acid and methanol. Upon acid modification of fly ash, surface acidity of solid acid catalyst (SAC) is greatly enhanced as a result of increased surface area, augmented silica content, and fabricated...
Can someone please tell me why I am getting errors. I declared the map and it's...
Can someone please tell me why I am getting errors. I declared the map and it's values like instructed but it's telling me I'm wrong. #include <iostream> #include <stdio.h> #include <time.h> #include <chrono> #include <string> #include <cctype> #include <set> #include <map> #include "d_state.h" using namespace std; int main() { string name; map<string,string> s; map<string,string>::iterator it; s[“Maryland”] = "Salisbury"; s[“Arizona”] = "Phoenix"; s[“Florida”] = "Orlando"; s[“Califonia”] = "Sacramento"; s[“Virginia”] = "Richmond"; cout << "Enter a state:" << endl; cin >> name;...
Okay, can someone please tell me what I am doing wrong?? I will show the code...
Okay, can someone please tell me what I am doing wrong?? I will show the code I submitted for the assignment. However, according to my instructor I did it incorrectly but I am not understanding why. I will show the instructor's comment after providing my original code for the assignment. Thank you in advance. * * * * * HourlyTest Class * * * * * import java.util.Scanner; public class HourlyTest {    public static void main(String[] args)     {        ...
can someone explain to me what osmolality is.? i am having a hard time understanding it
can someone explain to me what osmolality is.? i am having a hard time understanding it
I am having a trouble with a python program. I am to create a program that...
I am having a trouble with a python program. I am to create a program that calculates the estimated hours and mintutes. Here is my code. #!/usr/bin/env python3 #Arrival Date/Time Estimator # # from datetime import datetime import locale mph = 0 miles = 0 def get_departure_time():     while True:         date_str = input("Estimated time of departure (HH:MM AM/PM): ")         try:             depart_time = datetime.strptime(date_str, "%H:%M %p")         except ValueError:             print("Invalid date format. Try again.")             continue        ...
I was able to calculate (a) but I am having trouble with the calculations of (b)....
I was able to calculate (a) but I am having trouble with the calculations of (b). Thanks! The following are New York closing rates for A$/US$ and $/£:                                     A$/$ = 1.5150;               $/£ = $1.2950             (a) Calculate the cross rate for £ in terms of A$ (A$/£).             (b) If £ is trading at A$1.95/£ in London (cross market) on the same day, is there an arbitrage opportunity?  If so, show how arbitrageurs with £ could profit from this opportunity and calculate the arbitrage...
I have to complete a template for pathophysiology , can someone tell me the pathophysiology of...
I have to complete a template for pathophysiology , can someone tell me the pathophysiology of pain. This is for a pathophysiology class
Can someone please tell me on how can I have a double and character as one...
Can someone please tell me on how can I have a double and character as one of the items on my list? Thanks! This is what I got so far and the values I am getting are all integers. #pragma once #include <iostream> class Node { public:    int data;    Node* next;       // self-referential    Node()    {        data = 0;        next = nullptr;    }    Node(int value)    {        data...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT