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;...

  • The project I have is a link list that asks for a name and phone number...

    The project I have is a link list that asks for a name and phone number and it allows the user to insert ,delete ,modify ,and find an element in the structure. The program has a menu in which the user can choose which function they want to use I need to use  switch case to do this but it has not worked I need help with this. Thank you! #include <iostream> #include <string> #include <iomanip> using namespace std; const int...

  • Using C++ 11, Create the following function based on the criteria mentioned below: node.hpp             ○This...

    Using C++ 11, Create the following function based on the criteria mentioned below: node.hpp             ○This will contain your ​node struct○value ​is type int             ○next​ is type Node*             ○(optional) ​prev​ is type Node* nodeFunctions.hpp           ○This will contain the headers for all the functions you need to display, add, and remove                nodes from your list             ■displayList ­ displays the entire list from beginning to end. If no elements let the user       know the list is...

  • Please help me finish my code. Before the final pause in the main function, create an...

    Please help me finish my code. Before the final pause in the main function, create an ArrayList of another type such as double, char, short, bool, float, etc. Your choice. Add five data items to your array as we did for the int and string array lists. Print them out with a for loop as we did before. Print out the count and capacity of your new array list. Source.cpp #include #include #include #include "ArrayList.h" using namespace std; /// Entry...

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