Linked List From User Input c++ -


i need user input strings , put them linked list coding in c++. have made user input , put part of list, problem saves 1 list. think problem have 1 object in linked list , keep overwriting it, being said i've tried fix can't find out how. have organize list alphabetical order 1 step @ time. here code:

#include<iostream> #include<string> using namespace std;  struct node{     string data;     node *next; };  void addtolist(node *head); void deletefromlist(node *head); void printlist(node *head);  int main(){     bool quit = false;     int choice;     node *head = new node;     head->next = null;      while (!quit){         cout << "1. add list" << endl             << "2. delete list" << endl             << "3. print list" << endl             << "4. quit" << endl;         cin >> choice;         switch(choice){         case 1: addtolist(head);             break;         case 2: deletefromlist(head);             break;         case 3: printlist(head);             break;         case 4: quit = true;             break;         default:             cout << "that not valid input, quitting program";             quit = true;         }     } }  void addtolist(node *head){     bool quit = false;     string temp;     node *current = new node;     current->next = null;      while (!quit){          cout << "enter word(quit stop)";         cin >> temp;          if (temp == "quit"){             quit = true;         }         else{             current->data = temp;             current -> next = current;         }     }     return; }  void deletefromlist(node *head){     string deletion;     cout << "which value want delete list? ";     cin >> deletion;      node *prev = head;     node *current = head->next;      while (current)     {         if (current->data == deletion){             prev->next = current->next;             delete current;             return;         }         prev = current;         current = current->next;     }     if (!current){         cout << "that value not in list" << endl;     } }  void printlist(node *head){     if (!head)     {         cout << "nothing in list." << endl;         return;     }      node *current = new node;     current->next = head;     while (current)       {         cout << current->data << endl;          current = current->next;       } } 

in function addtolist, allocate 1 node if user add more 1 word. need allocate new nodes in loop, when user doesn't enter "quit". following code should correct:

void addtolist(node *head){     bool quit = false;     string temp;     node *current;      while (!quit){          cout << "enter word(quit stop)";         cin >> temp;          if (temp == "quit"){             quit = true;         }         else{             // allocate new node here:             current = new node;             current->data = temp;              // new node inserted after empty head             // because head empty node in implementation:             current->next = head->next;             head -> next = current;         }     }     return; } 

and in fuction printlist, don't need instantiate new node:

void printlist(node *head){     // head->next, because head empty node in implementation,     // carefull, head had never null in program,     // should check if head null.     if (!head->next)     {         cout << "nothing in list." << endl;         return;     }      node *current;     // set current head->next, because head empty in implementation:     current = head->next;     while (current)     {         cout << current->data << endl;         current = current->next;     } } 

Comments

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -