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

  • Context: MHB 
  • Thread starter Thread starter evinda
  • Start date Start date
  • Tags Tags
    List
Click For Summary
SUMMARY

The discussion focuses on efficient methods for performing a zig-zag traversal in a linked list based on a given string w. Participants suggest two primary approaches: the first involves iterating through the list to find w and tracking the number of nodes passed, then iterating again to retrieve the desired node. The second approach suggests storing pointers to each node in an array for quicker access. A proposed algorithm is also provided, which includes a check for null pointers and ensures the traversal logic is correctly implemented.

PREREQUISITES
  • Understanding of linked list data structures
  • Familiarity with pointer manipulation in programming
  • Knowledge of algorithmic complexity and traversal techniques
  • Basic programming skills in C or similar languages
NEXT STEPS
  • Implement the proposed algorithm in C to test its functionality
  • Explore linked list traversal techniques and their time complexities
  • Learn about dynamic memory allocation for storing pointers in arrays
  • Investigate alternative data structures that may optimize traversal
USEFUL FOR

Software developers, computer science students, and anyone interested in optimizing linked list operations and traversal algorithms.

evinda
Gold Member
MHB
Messages
3,741
Reaction score
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: 121
Technology news on Phys.org
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?
 
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)
 
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)
 
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)
 
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)
 
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;
        }
    }
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
Replies
9
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K