Dubly Linked List in C++ Example

General 2013. 7. 15. 11:42
반응형

Doubly Linked List 








//

//  main.cpp

//  Test_01

//

//  Created by Jang sang wook on 7/13/13.

//  Copyright (c) 2013 Jang sang wook. All rights reserved.

//


#include <iostream>

#include <cstdlib>


using namespace std;


int main(int argc, const char * argv[])

{


    

    struct node{

        int data;

        node* next;

        node* prev;

    };

    

    node* head;

    node* tail;

    node* n;

    node* temp;

    

    

    n = new node;

    n->data = 1;

    n->prev = NULL;

    head = n;

    tail = n;

    

    n = new node;

    n->data = 2;

    n->prev = tail;

    tail->next = n;

    tail = tail->next;


    n = new node;

    n->data = 3;

    n->prev = tail;

    tail->next = n;

    tail = tail->next;


    n = new node;

    n->data = 4;

    n->prev = tail;

    tail->next = n;

    tail = tail->next;


    temp = head;

    

    cout<<"Printing from head to tail\n";

    

    while(temp!=NULL){

        cout<< temp->data <<" ";

        temp = temp->next;

    }


    cout<<endl;

    

    temp = tail;

    cout<<"\nPrinting from tail to head\n";

    while(temp!= NULL){

        cout<<temp->data<<" ";

        temp = temp->prev;

    }

    

    return 0;

}






반응형
: