Untitled

 avatar
unknown
plain_text
25 days ago
1.6 kB
3
Indexable
#include<bits/stdc++.h>
using namespace std;
class Node{
    public:
    string val;
    Node* next;
    Node* prev;
    Node(string val){
        this->next = NULL;
        this->val = val;
        this->prev = NULL;
    }
};
Node* cur = NULL;
void insert_at_tail(Node* &head,Node* &tail,string val){
    Node* nw = new Node(val);
    if(head == NULL){
        head = nw;
        tail = nw;
        return;
    }
    tail->next = nw;
    nw->prev = tail;
    tail = nw;
}
void visit(Node* &temp,Node* head,string target ){
    Node* tmp = head;
    while(tmp!=NULL && tmp->val != target){  
        tmp = tmp->next;
    }
    if(tmp){cout<<tmp->val<<endl;
    temp = tmp;
    }
else cout<<"Not Available"<<endl;
}
int main(){
    Node* head = NULL;
    Node* tail = NULL;
    while(true){   
    string val;cin>>val;
    if(val =="end" ) break;
    insert_at_tail(head,tail,val);
    }
    Node* temp = head;
    int t;cin>>t;
    while(t--){
        string com;cin>>com;
        if(com == "visit"){
            string target;cin>>target;
             visit(temp,head,target);
        }
        else if(com == "prev"){
            if(temp->prev!=NULL){ 
             temp = temp->prev;
             cout<<temp->val<<endl;
            }
             else cout<<"Not Available"<<endl;
        }
        else if(com == "next"){
            if(temp->next!=NULL){
                temp = temp->next;
                cout<<temp->val<<endl;
            }
            else cout<<"Not Available"<<endl;
        }
    }
    return 0;
}
Leave a Comment