I have this problem that states that: LL be a doubly linked list that has 2 headers L and R. Each record has: llink, data, and rlink as field.

So llink is the prev pointer and rlink is the next pointer. Here is the code to reverse the list, but I am confused with how to change the code so that "LL be a doubly linked list that has 2 headers L and R", how do I make this?

Node *revDD(Node *LL)
{
  Node* start_prt = LL;
  Node *tmp1, *tmp2;
  Node* rlink= LL->next;
  Node* llink= LL->prev;
 
  while(start_prt  && rlink)
  {
    tmp2 = rlink->next;
    tmp2 = rlink;
 
    rlink->next = start_prt;
    start_prt->prev = rlink;
 
    rlink = tmp1;
    start_prt = tmp2;
  }
  
  start_prt->prev = NULL;
 
  
  start_prt = LL;
 

  while(llink && start_prt)
  {
    tmp1 = llink->prev;
    tmp2 = llink;
 
    llink->prev = start_prt;
    start_prt->next = llink;
 
    llink= tmp1;
    start_prt = tmp2;
  }
 
  start_prt->next = NULL;
 
  return LL;
}

Recommended Answers

All 2 Replies

i think what they are going for is....

L<=>n<=>n<=>n<=>R

This might help you...

#include <iostream>
#include <stdio.h>
#include <conio.h>

using namespace std;

typedef struct node *linklist;
struct node
{
        node *head;
        int info;
        node *tail;
};

struct dqueue
{
       linklist front, rear;
};

void insert(dqueue &, int, char);
int remove(dqueue &, char);
void display(dqueue &);
void menu();

int main()
{
    menu();
    return 0;
}

void menu()
{
 dqueue Q={NULL, NULL};
 int info;
 for(;;)
    {
        system("cls");
        cout<<"Dequeue\n"
            <<"\n1. Insert to Front"
            <<"\n2. Insert to Back"
            <<"\n3. Remove from Front"
            <<"\n4. Remove from Back"
            <<"\n5. Display"
            <<"\n\n Enter your choice: ";
        switch(getch())
               {
                          case '1':
                               system("cls");
                               cout<<"Enter any no: "; cin>>info;
                               insert(Q,info,'f');
                               break;
                          
                          case '2':
                               system("cls");
                               cout<<"Enter any no: "; cin>>info;
                               insert(Q,info,'r');
                               break;
                          
                          case '3':
                               system("cls");
                               remove(Q,'f');
                               break;
                          
                          case '4':
                               system("cls");
                               remove(Q,'r');
                               break;
                          
                          case '5':
                               system("cls");                               
                               display(Q);
                               break;
                          
                          case 27: return;
               };
        }
}

void insert(dqueue &ptr, int info, char ch)
{
     linklist temp=new node;
     temp->info=info;
     
     switch(ch)
           {
            case 'f':
                 temp->head=NULL;
                 if(ptr.front==NULL)
                   {
                    temp->tail=NULL;
                    ptr.rear=temp;                    
                   }
                 else
                   {
                    temp->tail=ptr.front;
                    ptr.front->head=temp;                 
                   }
                 ptr.front=temp;
                 break;
                              
            case 'r':
                 temp->tail=NULL;
                 if(ptr.front==NULL)
                   {
                    temp->head=NULL;
                    ptr.front=temp;                                        
                   }
                 else 
                   {
                    temp->head=ptr.rear;
                    ptr.rear->tail=temp;
                   }
                 ptr.rear=temp;
                 break;
           };
}

int remove(dqueue &ptr, char ch)
{
     if(ptr.front==NULL)
       {
        cout<<"Queue is Empty";
        getch();
        return '\0';
        }
      
      linklist temp;
      int tem;

      switch(ch)
           {
            case 'f':
                 temp=ptr.front;
                 ptr.front->head=NULL;
                 ptr.front=ptr.front->tail;                
                 tem=temp->info;                
                 break;
                 
            case 'r':
                 temp=ptr.rear;
                 ptr.rear=ptr.rear->head;
                 ptr.rear->tail=NULL;
                 tem=temp->info;                 
                 break;
           };
      
      delete temp;
      cout<<"The Element "<<tem<<" is deleted";
      getch();
      return tem;
}

void display(dqueue &ptr)
{
   linklist temp=ptr.front;
    
   while(temp!=NULL)
          {
           cout<<temp->info<<"<->";
           temp=temp->tail;
          }
    cout<<"NULL";
    getch();
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.