Interesting Way to Traverse a Linked List

 The ordinary traversal of Linked Lists just includes printing the elements of the LL(Linked List) once. But what if we want to dynamically view the LL in the way that we wish to. For this I thought of making this Advanced Traversal Function for the traversal in a LL, this function will allow the user to tell whether they want to go the left elem of the linked list or to it's right elem. For that I simply took an input from the user, in a variable choice and did the necessary operations for both of the conditions. The user can exit this traversal anytime they want by entering "exit" as the choice. This helps the user to check the LL in an effective manner so that they can understand how they have connected the various nodes in the LL.

Apart from this, I made a function that will take a node pointer, data to a new node and last parameter specifying whether they want to insert new node to the left or right of the node which is passed. This function was needed when I was trying to make new nodes for the traversal of my Advanced Traversal function, and that was when I got this idea of doing this so that I don't have to explicitly connect every node and also make every node by myself. This is where functions come into play, when you need to do a similiar thing many times, like in current case... making a new node and connecting it to any existing node.

Well, I have my exams from 24th next week, and for that I had to include academic work to my daily routine too, but I think that I'll have to increase it a bit more, maybe some DBMS more, I am prepared to a fair extent in every subject... if we leave Technical Communication, well, no one studies English! Also, for today I have my duo lessons left, and then maybe some DS questions.

CYA! 

#include<bits/stdc++.h>
using namespace std;
typedef struct node{
    struct node* left;
    struct node* right;
    int data;
} node;

node* make_node(int data){
    node* new_node=(node*)malloc(sizeof(node));
    new_node->left=NULL;
    new_node->right=NULL;
    new_node->data=data;
    return new_node;
}
node* set_node(node* a_node,int data,char c){
    if(c!='l' && c!='r'){
        return NULL;
    }
    node* new_node=(node*)malloc(sizeof(node));
    new_node->left=NULL;
    new_node->right=NULL;
    new_node->data=data;
    if(c=='l'){
        a_node->left=new_node;
        new_node->right=a_node;
    }
    else{
        // For c='r'
        a_node->right=new_node;
        new_node->left=a_node;
    }
    return new_node;
}

void list_traversal(node* n){
    // Normal method for linked list traversal
    node* temp=n;

    while(temp->right!=NULL){
        cout<<temp->data<<" ";
        temp=temp->right;
    }
    cout<<temp->data<<endl;
    // while(temp!=NULL){
    //     cout<<temp->data<<" ";
    //     temp=temp->right;
    // }
    // cout<<endl;
}
void advanced_traversal(node* n){
//Will allow the users to manually traverse the linked list by
// providing whether they want to go left('l') or right('r') in the linked list
//Temp node for traversal without changing the original node
// provided, that is, 'n'(in this case)
    node* temp=n;
    string choice;
    tm1:
    cout<<"Enter your choice: ";
    clrscr();
    cin>>choice;
    // cout<<endl;
    if(choice=="left"){
       
        if(temp->left==NULL){
            cout<<"Not possible, value is NULL\n";
        }
        else{
            temp=temp->left;
            cout<<temp->data<<endl;
        }
       
    }
    else if(choice=="right"){
        if(temp->right==NULL){
            cout<<"Value is NULL at right, not possible!\n";
        }
        else{
            temp=temp->right;
            cout<<temp->data<<endl;
        }
    }else if(choice=="exit"){
        cout<<"No valid choice selected...Getting out of the traversal func!\n";
        cout<<"Thanks for using the Func :)\n";
        goto tm2;
    }
    else{
        cout<<"No valid choice selected...Getting out of the traversal func!\n";
        cout<<"Thanks for using the Func :)\n";
        goto tm2;
    }
    goto tm1;
    tm2:
    cout<<"Exited!\n";
}
int main(){
    node* m_n=make_node(11);
    node* n1=set_node(m_n,10,'l');
    node* n2=set_node(n1,9,'l');
    node* n3=set_node(n2,8,'l');
    node* n4=set_node(n3,7,'l');

    list_traversal(n4);
    advanced_traversal(n4);
    return 0;
}

Comments

Popular Posts