Question

The screenshot below states "The singly linked list STL container std::forward_list has an erase function, which can be directly used here. But that function erases AFTER the item pointed to, not the one pointed to like the other containers. A singly linked list cannot look backwards, only forward. You need to convert the zero-based offset from the top to an iterator by advancing _groceries_sl_list.before_begin() offsetFromTop times. The STL has a function called std::next() that does that, or you can write your own loop." How do I code this? If this isn't specific enough please try to use some kind of generalized code. Also this is C++.

A t W [Groceryltem.hpp - -/Desk... [Assignment - Mozilla Firefox] - [to do4.png - Image Viewer ... GroceryList.cpp - Sequence

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

Hi,

void remove_item(forward_list<int> &ll):

1.Asks user to enter the number to be deleted

2. Searches item in forward_list, "iterator it" will store the pointer

3. If "it" points to ends of the list, then no element in forward list.

4. Else using iterator we go through the forward_list and search for "it" pointer using next(start,1).

5.Delete that start when we found the pointer "it".

next(start,1): This function returns the Iterator after advancing the positions mentioned in its arguments...

here it advance one location from where start pointed i.e., next element of start...

C++ code:

//program starts here..

#include <iostream>
#include<forward_list>
#include<algorithm>
using namespace std;

void remove_item(forward_list<int> &ll)
{
cout << "Enter number to be delted:" << endl;
int item;
cin >> item;
forward_list<int> :: iterator it;
forward_list<int> :: iterator start;
it = find(ll.begin(),ll.end(),item);//item to be deleted pointed by it
if(it == ll.end())
{
cout << "item not found" << endl;
return;
}
//From here actual code start... All deletions all pointer based
if(it == ll.begin())//if the front element to be removed
{
ll.pop_front();//using inBuilt function
}
else//Here searching for pointer not the value
{
for(start = ll.begin() ; start != ll.end() ; start++)
{
if(it == next(start,1))//if next element need to be deleted
{
ll.erase_after(start);//after start is the element to be deleted.
break;
}
}
}
}


int main() {
forward_list<int> ll = {10, 20, 30, 40, 50};
remove_item(ll);
forward_list<int> :: iterator start;
for(start = ll.begin();start != ll.end() ; start++)
cout << *start << " ";
cout << endl;
}

//Program ends here..

main.cpp saved 1 #include <iostream> 2 #include<vector> #include<algorithm> using namespace std; int find_item(vector<string>./main enter number of items:5 Enter item names: apple coconut banana grapes oranges enter item to be searched : banana item

Add a comment
Know the answer?
Add Answer to:
The screenshot below states "The singly linked list STL container std::forward_list has an erase function, which...
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
  • (The SortedLinkedList class template) Complete the SortedLinkedList class template which is a doubly linked list and...

    (The SortedLinkedList class template) Complete the SortedLinkedList class template which is a doubly linked list and is implemented with a header node and a tail node. // SortedLinkedList.h // SortedLinkedList.h // A collection of data are stored in the list by ascending order #ifndef SORTEDLIST_H #define SORTEDLIST_H using namespace std; template <typename T> class SortedList { private: // The basic single linked list node type. // Nested inside of SortedList. struct NodeType { T data; NodeType* next; NodeType* prev; NodeType(const...

  • Given a singly-linked list interface and linked list node class, implement the singly-linked list which has...

    Given a singly-linked list interface and linked list node class, implement the singly-linked list which has the following methods in Java: 1. Implement 3 add() methods. One will add to the front (must be O(1)), one will add to the back (must be O(1)), and one will add anywhere in the list according to given index (must be O(1) for index 0 and O(n) for all other indices). They are: void addAtIndex(int index, T data), void addToFront(T data), void addToBack(T...

  • Part 1: Implement a singly linked list -------------------------------------- (a) Your job is to implement a generic...

    Part 1: Implement a singly linked list -------------------------------------- (a) Your job is to implement a generic singly linked list that can hold any data type. The interface has been specified and provided to you in a header file called mylist.h. So your job is to write mylist.c that implements each function whose prototype is included in mylist.h. Specifically, you are asked to write the following functions: struct Node *addFront(struct List *list, void *data) void traverseList(struct List *list, void (*f)(void *))...

  • 8.9 Coding lab #5: create a dynamic array ADT and a singly linked list ADT. Honor Code...

    8.9 Coding lab #5: create a dynamic array ADT and a singly linked list ADT. Honor Code Your answers to this homework must be your own work.You are not allowed to share your solutions.You may not engage in any other activities that will dishonestly improve your results or dishonestly improve or damage the results of others. Plagiarism Plagiarism is when you copy words, ideas, or any other materials from another source without giving credit. Plagiarism is unacceptable in any academic environment....

  • Language: C++ Complete this function 1.Object &raw_front() { // Return the element at the front of...

    Language: C++ Complete this function 1.Object &raw_front() { // Return the element at the front of the list *without* using the iterator classes // (You may assume the list is not empty) // Place your code here. Code: #ifndef LIST_H #define LIST_H #include using namespace std; template class List { private: // The basic doubly linked list node. // Nested inside of List, can be public // because the Node is itself private struct Node { Object data; Node *prev;...

  • In this assignment, you will implement a sort method on singly-linked and doubly-linked lists. Implement the...

    In this assignment, you will implement a sort method on singly-linked and doubly-linked lists. Implement the following sort member function on a singly-linked list: void sort(bool(*comp)(const T &, const T &) = defaultCompare); Implement the following sort member function on a doubly-linked list: void sort(bool(*comp)(const T &, const T &) = defaultCompare); The sort(…) methods take as a parameter a comparator function, having a default assignment of defaultCompare, a static function defined as follows: template <typename T> static bool defaultCompare(const...

  • Requirements Print a range Write a bag member function with two parameters. The two parameters are...

    Requirements Print a range Write a bag member function with two parameters. The two parameters are Items x and y. The function should write to the console all Items in the bag that are between the first occurrence of x and the first occurrence of y. You may assume that items can be compared for equality using ==. Use the following header for the function: void print_value_range(const Item& x, const Item& y); print_value_range can be interpreted in a number of...

  • in c++ please include all of the following " template class, template function, singly linked list,...

    in c++ please include all of the following " template class, template function, singly linked list, the ADT stack, copy constructor, operator overloading, "try catch"and pointers Modify the code named "Stack using a Singly Linked List" to make the ADT Stack that is a template class has the code of the destructor in which each node is directly deleted without using any member function. As each node is deleted, the destructor displays the address of the node that is being...

  • ***CODE MUST BE IN C++*** Using the linked list in "basic linked list" which has a...

    ***CODE MUST BE IN C++*** Using the linked list in "basic linked list" which has a STRUCTURE for each node, write FUNCTION which starts at the head and outputs the value for each node until the last node is reached. Note: your function should work with the structure that is in this program. Please do not use the example which is for a class, but this example canbe helkpful.  Also note that the function must work no matter how many nodes...

  • The purpose of this program is to help reinforce container class concepts and linked list concepts...

    The purpose of this program is to help reinforce container class concepts and linked list concepts in C++. Specifically, the task is to implement the sequence class using a linked list. You need to use the author's file sequence3.h to create your implementation file named sequence3.cpp. Please make your code as efficient and reusable as possible. Please make sure code can pass any tests. sequence3.h // FILE: sequence3.h // CLASS PROVIDED: sequence (part of the namespace main_savitch_5) // This is...

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