hi all

am having hard times since yesterday trying to solve this problem.

Declare the following two integer lists:
int a[] = {5, 8, 4, 1, 7}, b[] = {12, 3, 15, 6, 23, 1, 2};
int sizeA = sizeof(a)/sizeof(int), sizeB = sizeof(b)/sizeof(int);
list<int> list1(a, a+sizeA), list2(b, b+sizeB);
Use the function splice() from the library "d_list1.h" to copy list2 onto the end of list1.
Use writeList() to output list1.


Please help me guys i must have it in saturday class....tommorow.......in class they give assigments but they dont give knowlege

Dani AI

Generated

raised a classic beginner question about STL lists. was right to point to the STL container: list<int> is std::list<int>, a doubly‑linked container that stores integers and exposes iterator-based operations. The important semantic difference to note is that splice does not copy elements — it relinks nodes. That makes the common overloads very useful: move all elements, move a single element, or move a range. Using the "move all" overload to append one list to another leaves the source list empty and runs in constant time.

A minimal, modern example that demonstrates the append-with-splice pattern:

#include <iostream>
#include <list>

int main() {
    std::list<int> left  = {10, 20, 30};
    std::list<int> right = {40, 50, 60, 70};

    left.splice(left.end(), right); // move all nodes from right to end of left

    for (int v : left) std::cout << v << ' ';
    std::cout << '\n';
    return 0;
}

Practical troubleshooting notes:

  • Remember to #include <list> and use std::list (old <iostream.h> style headers are obsolete).
  • splice takes an insertion iterator (e.g., left.end() to append) and one of the overloads described above — passing the wrong type triggers template errors.
  • Because splice transfers nodes, any pointers/iterators into the moved nodes remain valid but iterators into the emptied list no longer refer to those elements.
  • If the supplied d_list1.h already provides a writeList helper, call that to print; otherwise the loop above or std::copy with an ostream_iterator is fine.

As suggested, posting the exact code and compiler messages makes targeted help possible. ’s advice to work through the steps is good: understanding the container semantics (what splice actually does) is the key to completing this assignment correctly.

Recommended Answers

All 8 Replies

Read this first.

Read this first.

Dear Sir

they really didnt give us the knowlege to do such code

i will appreciate u will give me the knowlege on how to start so i dont need to ask some one to solve me my homework again........i really wanna be a proggramer........but they are not giving us the chance in here

Hello,

I suggest that you talk to your Dean at your college then, as I am sure you have already gone to your professor and asked questions in class. If the Dean is unresponsive, then you are not getting your money's worth at your institution.

Coding C++ requires knowledge of the header files, and you did not post them. The best we could do is guess, and that is a waste of everyone's time.

Please post to us what you have come up with so far for code. If you don't have any code to speak of, then there are larger issues. People here will help with syntax and design flow, but will not complete the project for you. It would not be ethical for you to hand it in as your own work.

Christian

hi all

am having hard times since yesterday trying to solve this problem.

Declare the following two integer lists:
int a[] = {5, 8, 4, 1, 7}, b[] = {12, 3, 15, 6, 23, 1, 2};
int sizeA = sizeof(a)/sizeof(int), sizeB = sizeof(b)/sizeof(int);
list<int> list1(a, a+sizeA), list2(b, b+sizeB);
Use the function splice() from the library "d_list1.h" to copy list2 onto the end of list1.
Use writeList() to output list1.


Please help me guys i must have it in saturday class....tommorow.......in class they give assigments but they dont give knowlege

Looks like you are supposed to use two STL list containers, load them with the a and b array, and then merge them with the splice() function. Simple stuff, but maybe a little early in your lessons.

Looks like you are supposed to use two STL list containers, load them with the a and b array, and then merge them with the splice() function. Simple stuff, but maybe a little early in your lessons.

i tried to and here is what i have reached

#include<iostream.h>
main()
{

int a[] = {5, 8, 4, 1, 7};
int b[] = {12, 3, 15, 6, 23, 1, 2};
int sizeA = sizeof(a)/sizeof(int);
int sizeB = sizeof(b)/sizeof(int);


then what will i do i cant under stand the meaning of list<int> and the concept of merging them toghether.

Looks like you are supposed to use two STL list containers, load them with the a and b array, and then merge them with the splice() function. Simple stuff, but maybe a little early in your lessons.

and how can i use library "d_list1.h" to copy lists

well, there's probably a function in there that you can use.
But as noone here except maybe you (and I doubt you took the trouble...) has ever seen it we're not going to be able to help you further.

Do your own homework and do your own THINKING. Way too many kids come here expecting to be turned into programming gurus by simply copying sourcecode produced by others and never having to exercise their brains.

Take a look at the code snippet called "Experimenting with the STL list" here on DaniWeb at:
http://www.daniweb.com/code/snippet100.html
This tells you about the concept of the list. It actually has just about the answer, but you have to figure it out yourself!

Clue: check the true meaning of sizeA and sizeB

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.