Question

The question I want answered is in C++. How can I pass a value to a function and create a new linked list out of it? For example I have: void getIntegerInput(List<int> list) {    int s;    std::...

The question I want answered is in C++.

How can I pass a value to a function and create a new linked list out of it? For example I have:

void getIntegerInput(List<int> list)
{
   int s;
   std::cout << "Type a positive integer then click enter to add it to the linked list\n";
   std::cout << "Type -1 to stop\nType -2 to delete an item from the list\n";
   std::cin >> s;
   while (s != -1)
   {
       if (s == -2) {
           int t;
           std::cout << "This is your list" << std::endl;
           std::cout << "______________________" << std::endl;
           list.displayList();
           std::cout << "Choose from the list that you would like to delete" << std::endl;
           std::cin >> t; std::cout << std::endl;
           list.delNode(t);
           std::cout << "Type an integer then click enter to add it to the linked list\n";
           std::cout << "Type -1 to stop\nType -2 to delete an item from the list\n";
           std::cin >> s;
       }
       if (s >= 0) {
       list.addNode(s);
       std::cin >> s;
   }
   }
   list.displayList();
}

Whenever I use this function I can create a linked list, however as soon as I run another function my linked list disappears. I know I need to use pointers but I'm not sure how to implement them. How would I go about doing this?

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

#include <iostream>
#include <list>
#include <iterator>
using namespace std;

//function for printing the elements in a list
void displayList(list <int> g)
{
   list <int> :: iterator it;
   for(it = g.begin(); it != g.end(); ++it)
       cout << '\t' << *it;
   cout << '\n';
}

int main()
{

   list <int> list1;
int i;

     
  
   while(i>-1)
   {
       cout<<"ENter a positive value,Enter -1 to stop,Enter -2 to delete";
   cin>>i;
   if(i>-1)
   {
  
       list1.push_back(i);
       cout << "\nThis is Your List : ";
   displayList(list1);
}
   }
   if (i==-2)
   {
       list1.pop_front();
       displayList(list1);
      
   }
     


   return 0;

}

CAUsersldellNDocumentsVinklisttemplate.cpp [Executing) - Dev-C++ 5.11 File Edit Search View Project Execute Tools AStyleWindo

Add a comment
Know the answer?
Add Answer to:
The question I want answered is in C++. How can I pass a value to a function and create a new linked list out of it? For example I have: void getIntegerInput(List<int> list) {    int s;    std::...
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
  • 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...

  • CSCI 2010 Lab11 Link-Lists Lab 11A Linked-Lists Preparation Create a Visual Studio C++ Project C...

    CSCI 2010 Lab11 Link-Lists Lab 11A Linked-Lists Preparation Create a Visual Studio C++ Project C2010Lab11A Add the following to the project. //LinkedList.cpp #include <cstdlib> #include "LinkedList.h" using namespace std; //--------------------------------------------------- //List Element Members //--------------------------------------------------- ListElement::ListElement(int d, ListElement * n) {    datum=d;    next=n; } int ListElement::getDatum () const {    return datum; } ListElement const* ListElement::getNext () const {    return next; } //--------------------------------------------------- //LinkedList Members //--------------------------------------------------- LinkedList::LinkedList () {    head=NULL; } void LinkedList::insertItem(int item) {    ListElement *currPtr = head;    ListElement *prevPtr =...

  • C++ - I have a doubly linked list, but I haven't been able to get the...

    C++ - I have a doubly linked list, but I haven't been able to get the "reverse list" option in the code to work(It's option #in the menu in the program). I received this guidance for testing: Test 4 cases by entering (in this order) c,a,z,k,l,m This tests empty list, head of list, end of list and middle of list. Then delete (in this order) a,z,l. This tests beginning, end and middle deletes. This exhaustively tests for pointer errors. #include...

  • Hello, I have some errors in my C++ code when I try to debug it. I...

    Hello, I have some errors in my C++ code when I try to debug it. I tried to follow the requirements stated below: Code: // Linked.h #ifndef INTLINKEDQUEUE #define INTLINKEDQUEUE #include <iostream> usingnamespace std; class IntLinkedQueue { private: struct Node { int data; Node *next; }; Node *front; // -> first item Node *rear; // -> last item Node *p; // traversal position Node *pp ; // previous position int size; // number of elements in the queue public: IntLinkedQueue();...

  • #include <iostream> using namespace std; int main(void) {    int SIZE;    cout<<"Enter the size of the...

    #include <iostream> using namespace std; int main(void) {    int SIZE;    cout<<"Enter the size of the array"<<endl;    cin>>SIZE;     int *numlist = new int[SIZE];     // Read SIZE integers from the keyboard     for (int i = 0; i<SIZE; i++ )    {        cout << "Enter value #" << i+1 << ": ";        cin >> numlist[i];     }     // Display the numbers in a reverse order     for (int i = SIZE; i > 0; i--...

  • How do can I update this code (Code A): Code (A) #include using namespace std; int fibonacci(int n) { int a = 0,...

    How do can I update this code (Code A): Code (A) #include using namespace std; int fibonacci(int n) { int a = 0, b = 1, c; if (n <= 1) return n; for (int i = 2; i <= n; i++) { c = a + b; a = b; b = c; } return b; } int fibonacciRecursive(int n) { if (n <= 1) { return n; } return fibonacciRecursive(n-1) + fibonacciRecursive(n-2); } int main() { int n;...

  • using the code above my instructor just want me to delete name from a list of...

    using the code above my instructor just want me to delete name from a list of students name. the function needs to be called delete_name. It's in c++. please no changing the code above just adding. this is pointer and dynamic array. // This is chapter 9 Pointers and Dynamic array #include< iostream> #include<string> using namespace std; //Gv //function declaration string* add_name(string*,int&); void display_names (string*,int); //main int main() //local var int size-3; string *students_names-new string[size]; //code cout<<"Enter "<<size< students names:"<<endl;...

  • This program illustrates dynamic variables #include <iostream> using namespace std; int main () {    int...

    This program illustrates dynamic variables #include <iostream> using namespace std; int main () {    int *p1;    p1 = new int;             // Variables created using the new operator are called dynamic variables    cout << "Enter an integer \n";    cin >> *p1;    *p1 = *p1 + 7;    cout << << "Your input + 7 = " << *p1 << endl;    delete p1;                // Delete the dynamic variable p1 and return the memory occupied by p1 to...

  • /* Array expander: Write a function that accepts an int array as argument. The function should...

    /* Array expander: Write a function that accepts an int array as argument. The function should create a new array that is n times the size of the argument array, where n is a user input. The function should copy the contents of the argument array to the new array, and initialize the unused elements of the second array with 0. The function should return a pointer to the new array */ #include <iostream> using namespace std; const int NUM_ELEM...

  • Code in C++: Please help me fix the error in function: void write_account(); //function to write...

    Code in C++: Please help me fix the error in function: void write_account(); //function to write record in binary file (NOTE: I able to store data to a txt file but however the data get error when I try to add on data the second time) void display_all(); //function to display all account details (NOTE: This function is to display all the info that save account.txt which is ID, Name, and Type) void modify_account(int); //function to modify record of file...

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