Question

/* I'm trying to write this program here but my problem is getting the program to...

/* I'm trying to write this program here but my problem is getting the program to save the user input to "int data" but cannot because it is part of the struct. What do I have to do in order to get this to work? (Language is C++)

Write a program that prompts the user to enter numbers on the screen and keep adding those numbers to a linked list.

The program stops when user enters -999. Hint: Use a while loop and include your cin statement in that loop. Stop the loop when user enters -999.

On each iteration, add the node containing the number at the end of the list.
Also write a function to find the smallest number in the linked list you created.*/

#include <iostream>
using namespace std;

struct node {

   int data;
   node *nextNode;


};
void printList(node *currentNode);
int main()
{

   cout << "This program will add numbers to a linked list that you enter, until you enter -999. Then the program will end." << endl;
   cout << "Enter what numbers you wish this program to remember: ";
   cin >> data;


   node *p = new node;
   p->data;
   p->nextNode = NULL;

  

   node *currentNode = p;
   while (currentNode != -999)
   {
       cout << currentNode->data << " ";
       currentNode = currentNode->nextNode;
   }

  
   return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<iostream>

#include<cstdlib>

using namespace std;

// structure of a node

struct node {

   int data;

   node *nextNode;

};

// display the list

void printList(struct node *);

// insert node in the list

void append(struct node **, int);

int main()

{

    cout << "This program will add numbers to a linked list that you enter, until you enter -999. Then the program will end." << endl;

   

    // create the head of the list

    struct node *head = NULL;

    // infinite loop

    while(1)

    {

        cout << "Enter what numbers you wish this program to remember: ";

        int data;

        cin>>data;

       

        // if user wants to quit

        if( data == -999 )

            break;

           

        // append element to the list

        append(&head, data);

    }

    cout<<"List : ";

    printList(head);

    return 0;

}

// print the elements of the list

void printList(struct node *first)

{

    struct node *temp = first;

   

    // traverse the list

    while(temp!=NULL)

    {

        cout<<temp->data<<" ";

       

        // go to the next node

        temp=temp->nextNode;

    }

   

    cout<<endl;

}

void append(struct node **head, int val)

{

    // create a new node

    struct node *new_node = (struct node *)malloc(sizeof(struct node));

    new_node->data = val;

    new_node->nextNode = NULL;

   

    if((*head) == NULL)

        (*head) = new_node;

    else

    {

        // point trav to head

        struct node *trav = (*head);

       

       // go to the last node

        while(trav->nextNode)

        {

            // go to next node

            trav = trav->nextNode;

        }

       

        // append new_node to the end of the list

        trav->nextNode = new_node;

    }

}


Sample Output

Add a comment
Know the answer?
Add Answer to:
/* I'm trying to write this program here but my problem is getting the program to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please fill in this code to reverse a linked list: (written in C/C++) #include #include #include...

    Please fill in this code to reverse a linked list: (written in C/C++) #include #include #include using namespace std; /* Link list node */ struct Node { int data; // your code here }; /* Function to reverse the linked list */ static void reverse(struct Node** head_ref) { // your code here } /* Function to push a node */ void push(struct Node** head_ref, int new_data) { // your code here } /* Function to print linked list */ void...

  • Working in C++, Complete the code. // Lab 9 code /* Insert leading comments with your...

    Working in C++, Complete the code. // Lab 9 code /* Insert leading comments with your name and the date. Describe the purpose of the code. Also, list each pointer and describe how it is used to enable the selection sort for the linked list structure. */ /* Insert code as described by the comments. */ /* Add comments to each line of code that uses a pointer, describing how it is being used. */ #include <iostream> using namespace std;...

  • C++ NEED HELP WITH MY REVERSE STRING FUNCTION IN LINK LIST A function Reverse, that traverses...

    C++ NEED HELP WITH MY REVERSE STRING FUNCTION IN LINK LIST A function Reverse, that traverses the linked list and prints the reverse text to the standard output, without changing the linked list. ( Pass the linked list by value, you have the freedom to create a doubly linked list that is a copy of the original list in your program before you call the function) #include "pch.h" #include <iostream> #include <string.h> #include <string> using namespace std; #define MAX 512...

  • Having code issues wth my C++ program. My program checks if two binary trees are similar...

    Having code issues wth my C++ program. My program checks if two binary trees are similar and if they're not they return false. My program is return true with different binary trees. Could use some help thanks #include <iostream> #include <string> using namespace std; //Struct of Nodes struct BinarySearchTree { int data; BinarySearchTree *left; BinarySearchTree *right; }; // Inserting nodes into BST BinarySearchTree* insert( BinarySearchTree* node, int val) { if (node == NULL) { BinarySearchTree *newNode = new BinarySearchTree(); newNode->data...

  • I'm just a beginner in programming,how to make this program more simple without using #include<iostream> and #include<redex> here is the question Terms to know: If-else statement,for.....

    I'm just a beginner in programming,how to make this program more simple without using #include<iostream> and #include<redex> here is the question Terms to know: If-else statement,for..while..do while,loops,pointer,address,continue,return,break. Create a C++ program which contain a function to ask user to enter user ID and password. The passwordmust contain at least 6 alphanumeric characters, 1 of the symbols !.@,#,$,%,^,&,* and 1 capital letter.The maximum allowable password is 20. Save the information. Test the program by entering the User ID and password. The...

  • #C++, Programming: Program Design Including Data Structures, 7th Edition, it is too long so i can't...

    #C++, Programming: Program Design Including Data Structures, 7th Edition, it is too long so i can't add the these two file.(unorderedlinked.h and linkedlist.h) as well as the output Please use the file names listed below since your file will have the following components: Ch16_Ex5_MainProgram.cpp - given file //22 34 56 4 19 2 89 90 0 14 32 88 125 56 11 43 55 -999 #include #include "unorderedLinkedList.h" using namespace std; int main() { unorderedLinkedList list, subList; int num;                                                                                                                                   ...

  • can someone please double check my code here are the requirements please help me fulfill the...

    can someone please double check my code here are the requirements please help me fulfill the requirements Using the material in the textbook (NumberList) as a sample, design your own dynamic linked list class (using pointers) to hold a series of capital letters. The class should have the following member functions: append, insert (at a specific position, return -1 if that position doesn't exist), delete (at a specific position, return -1 if that position doesn't exist), print, reverse (which rearranges...

  • Hi I got error C2660 : 'valuein' : function does not take 2 parameters in visual...

    Hi I got error C2660 : 'valuein' : function does not take 2 parameters in visual studio 2013 this is my code so far #include <cstdlib> #include <list> #include <iostream> using namespace std; struct node {    int data;    node* next; }; void initialize(node*p); void insert(int data,int n); void remove(int b,int pos); bool empty(node*); void length(); bool valuein(int c); int reportvalue(int value); void ptintlist(); int menu(); int main() {    struct node*head=NULL;    char commands;    int value;   ...

  • Using C, I need help debugging this program. I have a few error messages that I'm...

    Using C, I need help debugging this program. I have a few error messages that I'm not sure how to fix. Here is the code I have: /* * C Program to Print a Linked List in Reverse Order */ #include <stdio.h> #include <stdlib.h> struct node { int num; struct node *next; }; int main() { struct node *p = NULL; struct node_occur *head = NULL; int n; printf("Enter data into the list\n"); create(&p); printf("Displaying the nodes in the list:\n");...

  • Q) Modify the class Linked List below to make it a Doubly Linked List. Name your...

    Q) Modify the class Linked List below to make it a Doubly Linked List. Name your class DoublyLinkedList. Add a method addEnd to add an integer at the end of the list and a method displayInReverse to print the list backwards. void addEnd(int x): create this method to add x to the end of the list. void displayInReverse(): create this method to display the list elements from the last item to the first one. Create a main() function to test...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT