Well, "singly-linked list" in and of itself defines a well-known data structure who's properties and behaviors should be understood by most (if not all) developers.
The key here is to define an efficient *algorithm* that can be applied to the problem, regardless of the implementation language. That is, you should be able to define a list of steps that can be expressed in C, C++, Java, PHP, Python, whatever.
When dealing with container algorithms, the "things" the container holds should bear little or no effect on the algorithms used to manipulate the containers. After all, could you imagine the headache it would cause if someone couldn't write a generic container structure (such as a linked list, binary tree, etc..)? That is, if every time someone needed a container structure, they had to rewrite it from scratch specific to the object being contained?
C++ templates provide an excellent example for writing generic containers. Java states that all types are derived from Object, so a generic container would simply hold Objects.
My problem in answering this question is that it sounds wayyyy too much like a homework assignment, and it's against my morals to answer someone's homework questions. Also, most schools have "academic integrity" agreements which are essentially contracts between a student and the school that limits where a student can get help or answers to their programming assignments.
That said, let's take a look at your problems:
Quote:
quote:
1) Given an singly linked list and poisition "n", we have to remove the nth item from end.
2) Remove the duplicates in an array.
|
The first problem is challenging because a singly-linked list only allows you to traverse the list from Front to Back. So, while traversing the node, you don't have any idea how far from the end of the list you are -- you only know how far from the beginning you are.
It might make sense, then, to store pointers or references to each node in the linked list in some numerically-indexed container, such as a vector or array. One woudl traverse the entire list, storing the pointer or reference in the numerically indexed parallel structure. When the entier list is traversed, you know how many items are in it. And, since you have a numerically-indexed container, you can very efficiently access any element in the list by accessing the pointer/reference in the numerically-indexed container.
The second problem is challenging because you'll need to keep track of the items you've already seen in the list as you traverse it. As you traverse the list, if the item being stored exists in this secondary container, you know you've seen it before and can safely delete the current list node. If the item does NOT exist in the lookup structure, you know you have NOT seen it before, so you add it to the secondary structure.
The key here is to pick a data structure that makes lookups of "things" very efficient. A hash table or dictionary comes to mind, where the key is the "thing" you're storing.
I can't in good conscience give you more details than this... you should have enough information now to implement the above, or if not, at least be able to look through your data structures book or talk to your teacher about these ideas.
Take care,
Nik
http://www.bigaction.org/