[EDIT]Fixed my code. Is while(temp != NULL), not while(temp->next != NULL). Sorry to insert wrong code.
Today I've participated an online programming test. The interviewer used Codility to evaluate my code and the other interviewees.
At some moment a question about Linked list was made. It's about to count how many items a linked list has.
I did the only possible approach to do this, AFAIK:
//This is struct declaration
struct SomeStruct
{
int value;
SomeStruct* next;
}
int elementCount(SomeStruct* list)
{
int count = 0;
if(list != NULL)
{
SomeStruct* temp = list;
while(temp != NULL)
{
count++;
temp = temp->next;
}
}
return count;
}
I remember when I send this code as answer for this question, Codility points me out that this solution is wrong because its consume too much time to execute the task.
In my head and in this thread on SO there's no other way to get size of linked list without traversing it, not in a simple way.
Is there a problem with Codility when it says this solution is wrong? Or there are another approaches?
PS: the test allowed using of STL
Your solution is incorrect, since it returns 1 less than the actual count. Just try applying it to a list with 1 element.
Why did you come up with this strange two-tiered structure with an if and and a cycle that checks temp->next? Why not just
unsigned elementCount(const SomeStruct *list)
{
unsigned count = 0;
for (const SomeStruct *temp = list; temp != NULL; temp = temp->next)
++count;
return count;
}
I suspect that you decided to treat the element pointed by the list as the unused and reserved "header" element. Indeed, sometimes it might make sense to do implement lists that way. But I see nothing like that stated in your post. Did they tell you to treat it that way specifically?
well you don't have to evaluate the indirection temp->next twice for each iteration.
you can simply do
int count( SomeStruct const* pNode )
{
int result = 0;
while( pNode != 0 )
{
++result;
pNode = pNode->next;
}
return result;
}
Also, as WhozCraig notes, your code was logically wrong (yielding an off by one result), not just potentially inefficient.
Codility may be using a circularly linked list to check, in this case, your code will never end.
Using STL trivilailzes this though, as it has a List<> with a size method.
Related
I wrote a program to merge two sorted linked list into one and this function was the one I used to do it but it's not working. The code of the function is as follows is as follows:
void combine(Node **temp, Node *temp_1, Node *temp_2){
while(temp_1 != NULL || temp_2 != NULL){
if(temp_1->data > temp_2->data){
push(temp, temp_2->data);
temp_2 = temp_2->next;
}
else{
push(temp, temp_1->data);
temp_1 = temp_1->next;
}
}
while(temp_1 != NULL){
push(temp, temp_1->data);
temp_1 = temp_1->next;
}
while(temp_2 != NULL){
push(temp, temp_2->data);
temp_2 = temp_2->next;
}
}
Now, this code doesn't add anything to the final linked list. If I write something like
push(temp, temp_1->data);
it will add elements just fine so the problem isn't definitely with the push function. Can someone tell me what is the problem with the above code?
The full code is in the following URL:
https://ide.geeksforgeeks.org/FZ8IS4PADE
The issue is the while condition:
while(temp_1 != NULL || temp_2 != NULL){
This will allow the execution of the body of the loop when just one of those two pointers is null, and this will result in undefined behaviour on the first statement in that body:
if(temp_1->data > temp_2->data){
The || should be an &&. This will fix your issue.
Other remarks on your code
Don't use NULL for comparing your pointer variables against, but nullptr
The use of push makes your code inefficient: at every push, your code is starting an iteration through the whole list to find the end of it. Since you actually know what is the last node (since it was created in the previous iteration of the loop) this is a waste of time. Instead, keep a reference to the tail of the list that is being created. As there is no tail at the start of the combine process, it might be useful to create a "sentinel" node that comes before the real list that will be returned.
Use better variable names. temp is not temporary at all. It is the result that the caller wants to get: this name is misleading.
Avoid code repetition. The last two loops are the same except for the list that is copied from, and this code is again similar to the parts in the main loop. So create a function that does this job of copying a node from a source list to the end of another list, and that advances both pointers.
Here is what that would look like:
void copyNode(Node **source, Node **targetTail) {
*targetTail = (*targetTail)->next = new Node((*source)->data);
*source = (*source)->next;
}
void combine(Node **result, Node *head_1, Node *head_2){
Node *sentinel = new Node(0); // Dummy
Node *current = sentinel;
while(head_1 != nullptr && head_2 != nullptr){
if(head_1->data > head_2->data){
copyNode(&head_2, ¤t);
}
else{
copyNode(&head_1, ¤t);
}
}
if (head_1 == nullptr) {
head_1 = head_2;
}
while (head_1 != NULL) {
copyNode(&head_1, ¤t);
}
*result = sentinel->next;
delete sentinel;
}
void LinkedList<T>::mergeSort(Node*& curr) {
if (curr->next != nullptr) { //Thread 1: EXC_BAD_ACCESS (code=2, address=0x7ffeef3ffff8)
Node *ptr1 = nullptr;
Node *ptr2 = curr;
//splits linked list
for (int i = 0; i < getLength() / 2; i++) {
ptr1 = ptr2;
ptr2 = ptr2->next;
}
ptr1->next = nullptr;
ptr1 = curr;
//recursive call for sorting
mergeSort(ptr1); //Thread 1: EXC_BAD_ACCESS (code=2, address=0x7ffeef3ffff8)
mergeSort(ptr2);
//merge lists back together
if (ptr1 == nullptr)
curr = ptr2
else if (ptr2 == nullptr)
curr = ptr1
Node *reff = ptr1;
while (reff->next != nullptr) {
reff = reff->next;
}
reff->next = ptr2;
curr = reff;
}
}
Everything seems to be working, expect this function. I always get a segmentation error and I'm confused why it happens.
Also, I'm in college so there might be a more efficient way but this is the way I can do it, without looking ahead into the course.
I have a variable called length that holds the length. That part was implemented by the teacher.
So it gives me this error: Thread 1: EXC_BAD_ACCESS (code=2, address=0x7ffeef3ffff8). How can I figure out what the error means by code=2 and other numbers?
There are prob. numerous things wrong. This shows how it can be done with std::list. I don't know whether the API was given, but lets make it separate function that takes a list.
template<typename T>
void mergesort( std::list<T>& list ){
There is only work to do if we have more than one element
auto const size = list.size();
if( size > 1) {
The list is then split into two lists.
auto mid = list.begin();
std::advance( mid, size/2 );
std::list<T> other;
other.splice( other.begin(), list, list.begin(), mid );
Now that we have two sub-lists, mergesort can be called recursively on them.
mergesort( list );
mergesort( other );
The partial results then need to be merged.
list.merge( other );
And we are done. See working version here
for(int i=0;i<getLength()/2;i++)
I have a variable called length that holds the length. That part was implemented by the teacher.
So, getLength() is a member function of the LinkedList. When we make the recursive calls, it will always tell us the stored length of the entire LinkedList. But that's not what we want - we want the number of nodes in the chain of Nodes that we passed in. Instead, the first time we make a recursive call, we try to split it into the same number of nodes as it already has (in the first half, and zero nodes in the second half). Since this makes no progress, we will eventually blow up the stack with recursive calls.
I have an exercise using linked lists for my class. I am fairly new to the language, but I've given it an attempt. The instructions tell us to "iterate through until NodeData is found, then replace the data using the sets."
What are "the sets" in C++? I've looked online and I couldn't find anything. The only thing I can think of is setting the nodes to point somewhere else. For example head->NULL. But is this really necessary if I am simply replacing the data? To replace the data I've tried temp->order = NEWDATA. Is that the correct implementation? It did not seem to work. Maybe it was an error in a different part of the code.
bool OrderLL::Modify(Order * NodeData) {
OrderNode *temp = head;
if (temp == NULL) {
cout << "Empty List";
}
else {
while (temp != NULL) {
if (temp->order == NodeData) {
//not sure if this is the proper way of deleting data inside a node
delete anOrder;
//HOW DO I REPLACE THE DATA IN THIS PART?
}
temp = temp->next;
}
}
return false;
}
On a side note, I really do not understand why I continue to recieve downvotes on all my questions. Is it because they're basic C++ questions? They're not so basic to me. I know this website looks down upon "offtopic/chat discussions" but I just don't understand what is wrong with my questions.
You mentioned "replace" in your question, so just taking a guess but might be you are expected to replace the node itself and not just the data. In which case, it will be something like this
if(curr_node->data == to_replace_data){
curr_node->next = new_node;
new_node->next = curr_node->next->next;
free( curr_node->next); //or return curr_node->next depending on what
// you are trying to do.
}
I'm currently working on a project and the last piece of functionality I have to write is to shuffle a linked list using the rand function.
I'm very confused on how it works.
Could someone clarify on how exactly I could implement this?
I've looked at my past code examples and what I did to shuffle an array but the arrays and linked lists are pretty different.
Edit:
For further clarifications my Professor is making us shuffle using a linked list because he is 'awesome' like that.
You can always add another level of indirection... ;)
(see Fundamental theorem of software engineering in Wikipedia)
Just create an array of pointers, sized to the list's length, unlink items from the list and put their pointers to the array, then shuffle the array and re-construct the list.
EDIT
If you must use lists you might use an approach similar to merge-sort:
split the list into halves,
shuffle both sublists recursively,
merge them, picking randomly next item from one or the other sublist.
I don't know if it gives a reasonable random distribution :D
bool randcomp(int, int)
{
return (rand()%2) != 0;
}
mylist.sort(randcomp);
You can try iterate over list several times and swap adjacent nodes with certain probablity. Something like this:
const float swapchance = 0.25;
const int itercount = 100;
struct node
{
int val;
node *next;
};
node *fisrt;
{ // Creating example list
node *ptr = 0;
for (int i = 0; i < 20; i++)
{
node *tmp = new node;
tmp.val = i;
tmp.next = ptr;
ptr = tmp;
}
}
// Shuffling
for (int i = 0; i < itercount; i++)
{
node *ptr = first;
node *prev = 0;
while (ptr && ptr->next)
{
if (std::rand() % 1000 / 1000.0 < swapchance)
{
prev->next = ptr->next;
node *t = ptr->next->next;
ptr->next->next = ptr;
ptr->next = t;
}
prev = ptr;
ptr = ptr->next;
}
}
The big difference between an array and a linked list is that when you use an array you can directly access a given element using pointer arithmetic which is how the operator[] works.
That however does not preclude you writing your own operator[] or similar where you walk the list and count out the nth element of the list. Once you got this far, removing the element and placing it into a new list is quite simple.
The big difference is where the complexity is O(n) for an array it becomes O(n^2) for a linked list.
Oh dear; I seem to have misthought this.
I would like to split a singly-linked list 10,000 times, but evidently (and I didn't know this before you guys helped me) it causes a stack overflow.
I'm really new to this, so is there any way I could still do this and not cause a stack overflow? Using references or something?
Here's the method:
Node* Node::Split()
{
if(next == NULL)
{
return this;
}
Node *newNode = this->next;
if(this->next != NULL)
{
this->next = newNode->next;
}
if(newNode->next != NULL)
{
newNode->next = newNode->next->Split();
}
return newNode;
}
You'll have to write this as a loop rather than a recursive call. Keep track of your position in the original list, and both ends of the new lists, and append nodes alternately to each list.
Make sure your recursion does stop at some point (try a small data set). If it does then you have no problems there and the next thing to do is ask your compiler to increase the stack size for you. The default is quite small (I think it is one megabyte on vc++ 10).