Click here to Skip to main content
15,662,782 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
#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:

#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;
  
      
}
Posted
Updated 17-Jan-22 10:08am

1 solution

I would start by first sorting your list in which ever order you prefer. Then it's just a matter of deleting items from the back or the front, depending on which order it was sorted in. Here's one function that can sort and there are several others in the STL : sort - C++ Reference[^]
 
Share this answer
 

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