Double Linked List Class Task

 avatarkaziamir
c_cpp
2 months ago
1.1 kB
7
Indexable
Never
#include<bits/stdc++.h>
using namespace std;

#define resetPL  pL=left
#define resetPR  pR=right

typedef struct node{
    int data;
    struct node *next;
    struct node *prev;
}node;

void outputL(node *x){
    while(x != NULL){
        cout<<x->data<<" ";
        x = x->next;
    }
    cout<<endl;
}
void outputR(node *x){
    while(x != NULL){
        cout<<x->data<<" ";
        x = x->prev;
    }
    cout<<endl;
}

void solution(){
    int n;
    cin>>n;
    node *nodes[n];

    for(int i=0;i<n;i++){
        nodes[i] = (node *)malloc(sizeof(node));
        cin>>nodes[i]->data;
    }
    for(int i=0;i<n;i++){
        if(i == n-1) nodes[i]->next = NULL;
        else nodes[i]->next = nodes[i+1];
    }
    for(int i=0;i<n;i++){
        if(i == 0) nodes[i]->prev = NULL;
        else nodes[i]->prev = nodes[i-1];
    }
    node *left = nodes[0];
    node *right = nodes[n-1];
    node *pL = left;
    node *pR = right;
    node *to_free;

    resetPL;
    resetPR;
    cout<<"From Left: "<<endl;
    outputL(pL);
    
    cout<<"From Right: "<<endl;
    outputR(pR);

}

int main(){

	solution();

	return 0;
}