/*--------------------------------------------------------------------------
	This is dllist.h file for defination of funcations

-------------------------------------------------------------------------*/

#ifndef DLL
#define DLL

class DLLElement {
public:
       DLLElement( void *itemPtr, int sortKey ); // initialize a list element
       DLLElement *next; // next element on list
                         // NULL if this is the last
       DLLElement *prev; // previous element on list
                         // NULL if this is the first
       int key; // priority, for a sorted list
       void *item; // pointer to item on the list
} ;

class DLList {
public:
       DLList(); // initialize the list
       ~DLList(); // de-allocate the list
       void Prepend(void *item); // add to head of list (key = min_key - 1)
       void Append(void *item); // add to tail of list (key = max_key + 1)
       void *Remove(int *keyPtr); // remove from head of list
                                  // set *keyPtr to key of the removed item
                                  // return NULL if list is empty
       bool IsEmpty(); // return true if list has no elements
                       // routines to put/get items on/off list in order (sorted by key)
       void SortedInsert(void *item, int sortKey);
       void *SortedRemove(int sortKey); // remove first item with key==sortKey
                                        // return NULL if no such item exists
       void debugDump() ;
private:
        DLLElement *first; // head of the list, NULL if empty
        DLLElement *last; // last element of the list, NULL if empty
} ;

#endif 


/*--------------------------------------------------------------------------
	This is dllist.cc file for defination of funcations

-------------------------------------------------------------------------*/
#include <cstdlib>
#include <stdio.h>
#include <iostream>
#include "dllist.h"

using namespace std;

// initialize an element
DLLElement::DLLElement( void *itemPtr, int sortKey )
{
    next = NULL;
    prev = NULL;
    key = sortKey;
    cout<<"Value of key : "<<key;
    item = itemPtr;
    cout<<"Value of item : "<<item;
}

// initialize the list
DLList::DLList()
{
   first = NULL;
   last=NULL;         
}

// de-allocate the list                
DLList::~DLList()
{
    DLLElement *pcurrent = first;
    while(pcurrent!=NULL){
            DLLElement *old = pcurrent;
            pcurrent = pcurrent->next;
            delete old;
    }
}

// debug dump the list
void DLList::debugDump()
{
	DLLElement *ptr = this->first ;
	while ( ptr != NULL )
	{
		printf( "[%d] = %s\n", ptr->key, (char*)ptr->item  ) ;
		ptr = ptr->next ;
	}	
 	printf( "\n" ) ;	
}

// add to head of list (key = min_key - 1)
void DLList::Prepend(void *item)
{
    
   //  cout<<name;
     DLLElement *newelement = new DLLElement(item,(first->key)-1);
     if( IsEmpty()){
         last = newelement;
     }
     else
          first->prev = newelement;     
    newelement->next = first;
    first = newelement;    
}

// add to tail of list (key = max_key + 1)
void DLList::Append(void *item)
{
          DLLElement *newelement = new DLLElement (item,last->key+1);
          if(IsEmpty()){
                first = newelement;
          }
          else{
                last->next=newelement;     
                newelement->prev= last;
          }
          last = newelement;
}


// return true if list has no elements
// routines to put/get items on/off list in order (sorted by key)
bool DLList::IsEmpty()
{
     return first==NULL;
}

// remove from head of list
// set *keyPtr to key of the removed item
// return NULL if list is empty
void * DLList::Remove(int *keyPtr)
{
     DLLElement *temp = first;
     if(first->next == NULL)
                    last = NULL;
     else
         first->next->prev = NULL;
     first = first->next;
     delete temp;
}

// remove first item with key==sortKey
// return NULL if no such item exists
void * DLList::SortedRemove(int sortKey)
{
          	
}
                                        
 // insert in sorted order                       
void DLList::SortedInsert(void *item, int sortKey)
{
     DLLElement *current  = first;
     cout<<"value of current->key"<<current->key;
   /*  while(current->key < sortkey){
            current = current->next;
            if(current == NULL)
                        return false;                   
     } 
     
     DLLElement *newtoinsert  = new DLLElement(iterm,sortkey);
     
     if(current == last){
                
     }
     */    
}



/*--------------------------------------------------------------------------
	This is dllist-driver.cc file for defination of funcations

-------------------------------------------------------------------------*/


#include <iostream>
#include <stdio.h>
#include "dllist.h"

using namespace std;

DLList *theList = new DLList() ;

/* these should be implemented...*/

void generate ( int n )
{
	// your implementation here
}

void extract ( int n )
{
	// your implementation here
}

void dump ( void )
{
	theList->debugDump() ;	
}

/* Some basic test cases to start.  You should add more test cases ... */

void testSimpleInserts()
{
	cout << "This is a simple test...\n"  ;	
	/*theList->Append((char*)"This") ;
	theList->Append((char*)"is") ;
	theList->Append((char*)"a") ;
	theList->Append((char*)"test") ;
	theList->debugDump() ; */
	
	theList->Prepend((char*)"X") ;
	theList->Prepend((char*)"Y") ;
	theList->Prepend((char*)"Z") ;
//	theList->debugDump() ;
}


void testSimpleSortedInserts()
{
	cout << "This is a simple sorted test...\n"  ;	
	theList->SortedInsert((char*)"test",400) ;
	theList->SortedInsert((char*)"This",100) ;
	theList->SortedInsert((char*)"a",300) ;
	theList->SortedInsert((char*)"is",200) ;
	theList->debugDump() ;
	theList->SortedRemove(200) ;
	theList->debugDump() ;	
}

int main(int argc, char *argv[])
{
	// your implementation here
      testSimpleInserts();
	return argc;
}


] and
 [/
/*--------------------------------------------------------------------------
	This is dllist.h file for defination of funcations

-------------------------------------------------------------------------*/

#ifndef DLL
#define DLL

class DLLElement {
public:
       DLLElement( void *itemPtr, int sortKey ); // initialize a list element
       DLLElement *next; // next element on list
                         // NULL if this is the last
       DLLElement *prev; // previous element on list
                         // NULL if this is the first
       int key; // priority, for a sorted list
       void *item; // pointer to item on the list
} ;

class DLList {
public:
       DLList(); // initialize the list
       ~DLList(); // de-allocate the list
       void Prepend(void *item); // add to head of list (key = min_key - 1)
       void Append(void *item); // add to tail of list (key = max_key + 1)
       void *Remove(int *keyPtr); // remove from head of list
                                  // set *keyPtr to key of the removed item
                                  // return NULL if list is empty
       bool IsEmpty(); // return true if list has no elements
                       // routines to put/get items on/off list in order (sorted by key)
       void SortedInsert(void *item, int sortKey);
       void *SortedRemove(int sortKey); // remove first item with key==sortKey
                                        // return NULL if no such item exists
       void debugDump() ;
private:
        DLLElement *first; // head of the list, NULL if empty
        DLLElement *last; // last element of the list, NULL if empty
} ;

#endif 


/*--------------------------------------------------------------------------
	This is dllist.cc file for defination of funcations

-------------------------------------------------------------------------*/
#include <cstdlib>
#include <stdio.h>
#include <iostream>
#include "dllist.h"

using namespace std;

// initialize an element
DLLElement::DLLElement( void *itemPtr, int sortKey )
{
    next = NULL;
    prev = NULL;
    key = sortKey;
    cout<<"Value of key : "<<key;
    item = itemPtr;
    cout<<"Value of item : "<<item;
}

// initialize the list
DLList::DLList()
{
   first = NULL;
   last=NULL;         
}

// de-allocate the list                
DLList::~DLList()
{
    DLLElement *pcurrent = first;
    while(pcurrent!=NULL){
            DLLElement *old = pcurrent;
            pcurrent = pcurrent->next;
            delete old;
    }
}

// debug dump the list
void DLList::debugDump()
{
	DLLElement *ptr = this->first ;
	while ( ptr != NULL )
	{
		printf( "[%d] = %s\n", ptr->key, (char*)ptr->item  ) ;
		ptr = ptr->next ;
	}	
 	printf( "\n" ) ;	
}

// add to head of list (key = min_key - 1)
void DLList::Prepend(void *item)
{
    
   //  cout<<name;
     DLLElement *newelement = new DLLElement(item,(first->key)-1);
     if( IsEmpty()){
         last = newelement;
     }
     else
          first->prev = newelement;     
    newelement->next = first;
    first = newelement;    
}

// add to tail of list (key = max_key + 1)
void DLList::Append(void *item)
{
          DLLElement *newelement = new DLLElement (item,last->key+1);
          if(IsEmpty()){
                first = newelement;
          }
          else{
                last->next=newelement;     
                newelement->prev= last;
          }
          last = newelement;
}


// return true if list has no elements
// routines to put/get items on/off list in order (sorted by key)
bool DLList::IsEmpty()
{
     return first==NULL;
}

// remove from head of list
// set *keyPtr to key of the removed item
// return NULL if list is empty
void * DLList::Remove(int *keyPtr)
{
     DLLElement *temp = first;
     if(first->next == NULL)
                    last = NULL;
     else
         first->next->prev = NULL;
     first = first->next;
     delete temp;
}

// remove first item with key==sortKey
// return NULL if no such item exists
void * DLList::SortedRemove(int sortKey)
{
          	
}
                                        
 // insert in sorted order                       
void DLList::SortedInsert(void *item, int sortKey)
{
     DLLElement *current  = first;
     cout<<"value of current->key"<<current->key;
   /*  while(current->key < sortkey){
            current = current->next;
            if(current == NULL)
                        return false;                   
     } 
     
     DLLElement *newtoinsert  = new DLLElement(iterm,sortkey);
     
     if(current == last){
                
     }
     */    
}



/*--------------------------------------------------------------------------
	This is dllist-driver.cc file for defination of funcations

-------------------------------------------------------------------------*/


#include <iostream>
#include <stdio.h>
#include "dllist.h"

using namespace std;

DLList *theList = new DLList() ;

/* these should be implemented...*/

void generate ( int n )
{
	// your implementation here
}

void extract ( int n )
{
	// your implementation here
}

void dump ( void )
{
	theList->debugDump() ;	
}

/* Some basic test cases to start.  You should add more test cases ... */

void testSimpleInserts()
{
	cout << "This is a simple test...\n"  ;	
	/*theList->Append((char*)"This") ;
	theList->Append((char*)"is") ;
	theList->Append((char*)"a") ;
	theList->Append((char*)"test") ;
	theList->debugDump() ; */
	
	theList->Prepend((char*)"X") ;
	theList->Prepend((char*)"Y") ;
	theList->Prepend((char*)"Z") ;
//	theList->debugDump() ;
}


void testSimpleSortedInserts()
{
	cout << "This is a simple sorted test...\n"  ;	
	theList->SortedInsert((char*)"test",400) ;
	theList->SortedInsert((char*)"This",100) ;
	theList->SortedInsert((char*)"a",300) ;
	theList->SortedInsert((char*)"is",200) ;
	theList->debugDump() ;
	theList->SortedRemove(200) ;
	theList->debugDump() ;	
}

int main(int argc, char *argv[])
{
	// your implementation here
      testSimpleInserts();
	return argc;
}

why can't you start the program ? what errors do you get? were there any errors or warnings when you compiled it ? did you fix ALL warnings ?

My program gets compiled. But when I run it. It gives windows messge to close program.

In prepend function in dllist.cc file,
I can't get the first->key value and that's why it crashes.

I have copied the ...source code..2 times by mistake. and you can easily get it...as I have make comments....

This article has been dead for over six months. Start a new discussion instead.