What is the Efficient Way to Perform a Zig-Zag Traversal in a List?

  • MHB
  • Thread starter evinda
  • Start date
  • Tags
    List
In summary, the algorithm in line 3 of the code won't compile, and the algorithm in line 5 might be wrong.
  • #1
evinda
Gold Member
MHB
3,836
0
Hello! (Smile)

Suppose that each node of a list has the following structs:

  • string
    $$$$
  • num
    $$$$
  • next

It is given a string $w$. Suppose that $w$ is in the list at a node $p$, of which the struct num has the value n.
We are looking for the value of the struct string of the node that precedes $p$ for $n$ positions in the list.View attachment 3606

So, do we have to traverse the whole list, till we find a node with struct num equal to $n$ and then with a while loop find the desired value or is there also a better way? (Thinking)
 

Attachments

  • zigzag.png
    zigzag.png
    3.5 KB · Views: 79
Technology news on Phys.org
  • #2
evinda said:
It is given a string $w$. Suppose that $w$ is in the list at a node $p$, of which the struct num has the value n.
We are looking for the value of the struct string of the node that precedes $p$ for $n$ positions in the list.

So, do we have to traverse the whole list, till we find a node with struct num equal to $n$ and then with a while loop find the desired value or is there also a better way? (Thinking)

Hi! (Smirk)

If I understand your problem statement correctly, $n$ is not given, only $w$ is.
So I think we first have to look up $w$ before we can tell what $p$ and $n$ are.
And then we have to find the node that precedes $p$ by $n$ positions.

Or am I wrong? (Wondering)
Is there an example?
 
  • #3
I like Serena said:
Hi! (Smirk)

If I understand your problem statement correctly, $n$ is not given, only $w$ is.
So I think we first have to look up $w$ before we can tell what $p$ and $n$ are.
And then we have to find the node that precedes $p$ by $n$ positions.

I think so... (Thinking)

I like Serena said:
Is there an example?

No, there isn't an example... (Shake)
 
  • #4
evinda said:
I think so... (Thinking)

No, there isn't an example... (Shake)

Then, out of hand, I see 2 approaches.We can iterate through the list until we find $w$, which is when we find $n$.
At the same time we track how many items we have passed, say $m$.
Then we iterate again for $m-n$ items. (Smirk)Alternatively, we can iterate through the list until we find $w$, and store the pointer to each item in an array.
(It would help if we have a clue how big the array should be. (Thinking))
Then we can look up $m-n$ in the array. (Thinking)
 
  • #5
I like Serena said:
Then, out of hand, I see 2 approaches.We can iterate through the list until we find $w$, which is when we find $n$.
At the same time we track how many items we have passed, say $m$.
Then we iterate again for $m-n$ items. (Smirk)Alternatively, we can iterate through the list until we find $w$, and store the pointer to each item in an array.
(It would help if we have a clue how big the array should be. (Thinking))
Then we can look up $m-n$ in the array. (Thinking)

Is it right like that? (Thinking)

Code:
Algorithm(pointer L)
   pointer R;
   *R=L;
    int i;
    while (R->num!=n){   
             R=R->next;
    }
     for (i=0; i<n; i++){
           R=R->next;
           i=i+1;
     }
     printf("%c",R->string)
 
  • #6
evinda said:
Is it right like that? (Thinking)

Code:
1. Algorithm(pointer L)
2.   pointer R;
3.   *R=L;
4.    int i;
5.    while (R->num!=n){   
6.             R=R->next;
7.    }
8.     for (i=0; i<n; i++){
9.           R=R->next;
10.          i=i+1;
11.    }
12.    printf("%c",R->string)

Well... in line 3 you have an assignment that won't compile. (Worried)

In line 5 you are matching $n$... but I thought we had to match $w$. :confused:

Then, in line 8, I presume you should be restarting from the beginning.
But if so, R should be assigned to L yet again. (Doh)
 
  • #7
I retried it... (Wait)
Is it maybe like that? (Thinking)

Code:
    Algorithm(pointer L, string w){
        pointer R=L;
        int times=0;
        if (R==NULL) return;
        while (R!=NULL and R->string!=w){
              times++;
              R=R->next;
        }
        n=R->num;
        if (times>n){
            int i=0;
            p=L;
            while (i<times-n){
                   p=p->next;
                   i++;
            }
            return p->string;
        }
    }
 

FAQ: What is the Efficient Way to Perform a Zig-Zag Traversal in a List?

What is Zig-Zag traversal of a list?

Zig-Zag traversal is a method used to traverse through a list in an alternating pattern, moving in a zig-zag motion instead of a traditional linear fashion.

How does Zig-Zag traversal work?

The Zig-Zag traversal algorithm involves starting at the first element in the list and moving in a diagonal direction to the next element. Then, the traversal direction switches and continues in the opposite diagonal direction until all elements in the list have been visited.

What are the benefits of using Zig-Zag traversal?

Zig-Zag traversal allows for a more efficient and optimized traversal of a list, as it reduces the number of jumps between elements and can potentially save time and resources. It also helps to evenly distribute the traversal pattern throughout the list, avoiding potential bottlenecks.

When is Zig-Zag traversal useful?

Zig-Zag traversal is particularly useful when dealing with large lists or datasets, as it can help improve performance and reduce the time and resources needed for traversal. It can also be useful in scenarios where a balanced traversal pattern is desired.

Are there any limitations to using Zig-Zag traversal?

While Zig-Zag traversal can be beneficial in certain situations, it may not be as effective in lists with a smaller number of elements. Additionally, the effectiveness of Zig-Zag traversal may vary depending on the specific structure and content of the list.

Similar threads

Replies
2
Views
2K
Replies
1
Views
2K
Replies
1
Views
2K
Replies
2
Views
2K
Replies
2
Views
4K
Replies
5
Views
2K
Replies
1
Views
2K
Replies
2
Views
2K
Back
Top