Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi I need to take user input instead of predefined values below. I know its "cin" but dont know in lists.

#include <iostream>
using namespace std;
 
/* Link list node */
struct Node {
   int data;
   struct Node* next;
   };
 
//delete first node in the linked list
Node* deleteFirstNode(struct Node* head)
{
   if (head == NULL)
   return NULL;
 
   // Move the head pointer to the next node
   Node* tempNode = head;
   head = head->next;
   delete tempNode;
 
   return head;
}
//delete last node from linked list
Node* removeLastNode(struct Node* head)
{
   if (head == NULL)
   return NULL;
 
   if (head->next == NULL) {
      delete head;
      return NULL;
   }
 
// first find second last node
Node* second_last = head;
while (second_last->next->next != NULL)
second_last = second_last->next;
 
// Delete the last node
delete (second_last->next);
 
// set next of second_last to null
second_last->next = NULL;
 
return head;
}
 
// create linked list by adding nodes at head
void push(struct Node** head, int new_data)
{
   struct Node* newNode = new Node;
   newNode->data = new_data;
   newNode->next = (*head);
   (*head) = newNode;
}
 
// main function
int main()
{
   /* Start with the empty list */
   Node* head = NULL;
 
   // create linked list
   push(&head, 2);
   push(&head, 4);
   push(&head, 6);
   push(&head, 8);
   push(&head, 10);
 
         Node* temp;
 
   cout<<"Linked list created "<<endl; for (temp = head; temp != NULL; temp = temp->next)
   cout << temp->data << "-->";
   if(temp == NULL)
   cout<<"NULL"<<endl;
 
       //delete first node
   head = deleteFirstNode(head);
   cout<<"Linked list after deleting head node"<<endl; for (temp = head; temp != NULL; temp = temp->next)
   cout << temp->data << "-->";
   if(temp == NULL)
   cout<<"NULL"<<endl;
 
      //delete last node
   head = removeLastNode(head);
   cout<<"Linked list after deleting last node"<<endl; for (temp = head; temp != NULL; temp = temp->next)
   cout << temp->data << "-->";
   if(temp == NULL)
   cout<<"NULL";
 
   return 0;
}


What I have tried:

.
<pre>#include <iostream>
using namespace std;
 
/* Link list node */
struct Node {
   int data;
   struct Node* next;
   };
 
//delete first node in the linked list
Node* deleteFirstNode(struct Node* head)
{
   if (head == NULL)
   return NULL;
 
   // Move the head pointer to the next node
   Node* tempNode = head;
   head = head->next;
   delete tempNode;
 
   return head;
}
//delete last node from linked list
Node* removeLastNode(struct Node* head)
{
   if (head == NULL)
   return NULL;
 
   if (head->next == NULL) {
      delete head;
      return NULL;
   }
 
// first find second last node
Node* second_last = head;
while (second_last->next->next != NULL)
second_last = second_last->next;
 
// Delete the last node
delete (second_last->next);
 
// set next of second_last to null
second_last->next = NULL;
 
return head;
}
 
// create linked list by adding nodes at head
void push(struct Node** head, int new_data)
{
   struct Node* newNode = new Node;
   newNode->data = new_data;
   newNode->next = (*head);
   (*head) = newNode;
}
 
// main function
int main()
{
   /* Start with the empty list */
   Node* head = NULL;
 
   // create linked list
   push(&head, 2);
   push(&head, 4);
   push(&head, 6);
   push(&head, 8);
   push(&head, 10);
 
         Node* temp;
 
   cout<<"Linked list created "<<endl; for (temp = head; temp != NULL; temp = temp->next)
   cout << temp->data << "-->";
   if(temp == NULL)
   cout<<"NULL"<<endl;
 
       //delete first node
   head = deleteFirstNode(head);
   cout<<"Linked list after deleting head node"<<endl; for (temp = head; temp != NULL; temp = temp->next)
   cout << temp->data << "-->";
   if(temp == NULL)
   cout<<"NULL"<<endl;
 
      //delete last node
   head = removeLastNode(head);
   cout<<"Linked list after deleting last node"<<endl; for (temp = head; temp != NULL; temp = temp->next)
   cout << temp->data << "-->";
   if(temp == NULL)
   cout<<"NULL";
 
   return 0;
}
Posted
Updated 10-Jan-22 5:44am
Comments
jeron1 10-Jan-22 11:42am    
Using a loop (while or for) that has data entry (cin) inside?
jasmeet singh 2021 10-Jan-22 11:44am    
Linked list has to be from user input
jeron1 10-Jan-22 11:49am    
OK, so have the user enter it, using a loop with cin.

1 solution

Try this:
C++
int count = 0;
int value = 0;

cout << "Please enter the number of values: ";
cin >> count; // the number of values to be input below

while (count > 0)
{
    cout << "Please enter the next value: ";
    cin >> value; // get next value
                  // and push it into the list
    push(&head, value);
    count--;
}
 
Share this answer
 
Comments
jasmeet singh 2021 10-Jan-22 15:12pm    
Do I need to write this?
Node* head = NULL
Richard MacCutchan 10-Jan-22 15:23pm    
Yes; the node list must be initialised before you add the first entry.
jasmeet singh 2021 10-Jan-22 15:29pm    
many thanks
jasmeet singh 2021 10-Jan-22 18:52pm    
Could you also help me in user input for STL list?

#include<iostream>
#include<list> // for list operations
using namespace std;

int main()
{
// initializing list of integers
list<int> list1={10,15,20,25,30,35};

// declaring list iterators
list<int>::iterator it = list1.begin();
list<int>::iterator it1 = list1.begin();

// incrementing the positions of iterators
advance(it,2);
advance(it1,5);

// printing original list
cout << "The original list is : ";
for (list<int>::iterator i=list1.begin(); i!=list1.end(); i++)
cout << *i << " ";

cout << endl;

// using erase() to erase single element
// erases 20
list1.erase(it);

// list after deletion 1 element
cout << "The list after deleting 1 element using erase() : ";
for (list<int>::iterator i=list1.begin(); i!=list1.end(); i++)
cout << *i << " ";

cout << endl;

it = list1.begin();

// incrementing the positions of iterators
advance(it,2);

// using erase() to erase multiple elements
// erases 25,30
list1.erase(it,it1);

// list after deletion of multiple elements
cout << "The list after deleting multiple elements using erase() : ";
for (list<int>::iterator i=list1.begin(); i!=list1.end(); i++)
cout << *i << " ";

cout << endl;


}
Richard MacCutchan 11-Jan-22 3:38am    
Help you how? You have not explained your problem.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900