Friday, September 23, 2016

Data structures- doubly linked lists

#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
struct node
{
    int info;
    node *prev;
    node *next;
};
int main()
{
    char ch;
   struct node *start= NULL;
    struct node *save, *last;
    struct node *new_node;
   
    do
    {
        new_node=(node *)malloc(sizeof(node));
        cout<<"\nEnter info"<<endl;
        cin>>new_node->info;
    if(start==NULL)
    {
        new_node->prev=NULL;
        new_node->next=NULL;
        start=new_node;
        last = start;
    }
    else
    {
            save=start;
        new_node->next=save;
            new_node->prev=NULL;
        save->prev=new_node;
        start=new_node;
    }
     printf("\nDo you want to create another : (y/n))");
        ch=getche();
   }while(ch!='n');
    cout<<endl<<"Display using next pointer"<<endl;
    while(new_node!=NULL)
    {
        cout<<new_node->info<<"->";

    new_node = new_node->next;
    }
    cout<<"NULL\n";
    new_node = last;
     cout<<endl<<"Display using prev pointer"<<endl;
     cout<<"NULL";
while(new_node!=NULL)
    {
        cout<<"<-"<<new_node->info;
     new_node=new_node->prev;
    }
  
getch();
return 0;
}

No comments:

Post a Comment