#include <iostream.h>
#include <ctype.h>
#include <string.h>

#define MAX_NAME_LEN 30


typedef struct Sheet
{
 char Name[MAX_NAME_LEN + 1];
 struct Sheet *Next, *Prev;
}Sheet;

 

//prototypes
void initialise(Sheet *&, char arr[], Sheet *&);
void right(Sheet *);
void left(Sheet *);
void first(Sheet *&);
void last(Sheet *&);
void erase(Sheet *);
void insert(char word[], Sheet *&, Sheet *&);
void rename();
void display(Sheet *&);
void strToLower(char s[]);

 

 

void main()
{

	Sheet *Head = NULL, *Tail = NULL;
	Sheet *mvlr=NULL;
	
 char init1[MAX_NAME_LEN+1]="sheet1";
	char init2[MAX_NAME_LEN+1]="sheet2";
	char init3[MAX_NAME_LEN+1]="sheet3";

  
 initialise(Head, init1, Tail);
	initialise(Head, init2, Tail);
	initialise(Head, init3, Tail);

	char Option; 
 char OldName[MAX_NAME_LEN+1], NewName[MAX_NAME_LEN+1];
	
	
 cout<<"************ Selection Options *************"<<endl;
 cout<<endl;

 while(Option != 'E')
 {
 cout<<"Press 'R' to move to the next sheet\n";
 cout<<"Press 'L' to move to the previous sheet\n";
 cout<<"Press 'H' to jump to the first sheet\n";
 cout<<"Press 'T' to jump to the last sheet\n";
 cout<<"Press 'S' to add a new sheet to the list\n";
 cout<<"Press 'D' to delete a sheet from the list\n";
 cout<<"Press 'V' to view the sheets currently in the list\n";
 cout<<"Press 'N' to rename a sheet\n";
 cout<<"Press 'E' to exit\n";
 cout<<endl;
 
 cout<<"Enter your option: ";
 cin>>Option;
 Option = toupper(Option); 
 
 switch(Option)
 {
 case 'R':
  right(Head);
	 break;
 
 case 'L':
  left(Head);
  break;
 
 case 'H':
  first(Head);
  break;
 
 case 'T':
  last(Tail);
  break;
 
 case 'S':
  cout<<"Enter a name for this new sheet: ";
  cin>>NewName;
  strToLower(NewName);
  insert(NewName, Head, Tail);
  cout<<endl;
  break;
 
 case 'D':
  cout<<"Enter the name of the sheet you wish to delete: "; 
		cin>>OldName;
  strToLower(OldName);
  erase(Head);
  break;

 case 'V':
  cout<<"Displaying list: "<<endl;
  display(Head);
  cout<<endl;
  break;
 
 case 'N':
  cout<<"Enter name to change: ";
		cin>>OldName[MAX_NAME_LEN + 1];
  strToLower(OldName);
  cout<<"Enter new name for sheet: ";
		cin>>NewName[MAX_NAME_LEN + 1];
  strToLower(NewName);
  rename();
  break;
 
 case 'E':
  break;

 default:
  cout<<endl;
  cout<<"Please select an option from the list"<<endl;
  cout<<endl;
  break;

	 }//end switch
 }//end while 


}//end main

void initialise(Sheet *&L, char arr[], Sheet *&T) 
{
	Sheet *e, *curr, *prev;

	e = new Sheet;
	strcpy(e->Name, arr);
	e->Next = NULL;
	e->Prev = NULL;

	if(L == NULL)
 {
		L = e;
	}
 else
 {
	prev = curr = L;
		 
 while(curr != NULL)
 {
	   prev = curr;
	   curr = curr->Next;
	}
		e->Next = NULL;
		prev->Next = e;
		e->Prev = prev;
		T = e;
	}

}


void right(Sheet *Head)
{
 Sheet *Current, *Previous;

 Current = Head;
 Current = Current->Next;
 Previous = Current->Prev;

 cout<<"Current Sheet: "<<Current->Name;
 cout<<endl;
}

void left(Sheet *mvlr)
{
 Sheet *Current = new Sheet, *Previous = new Sheet;

 cout<<"Previous Sheet: "<<Current->Name<<endl;
 Current = Current->Prev;
 Previous = Current->Next;
 cout<<"Current Sheet: "<<Current->Name<<endl;


 if(Current->Prev == NULL)
 {
  cout<<"No more sheets!!!11!one"<<endl;
 }
 cout<<endl;
}


void first(Sheet *&L)
{
	Sheet *e;
	e = L;
	cout<<"The first sheet in the list is:"<<'\t';
 cout<<e->Name<<endl;
 cout<<endl;
}

void last(Sheet *&L)
{
	Sheet *e;
	e = L;
	cout<<"The last sheet in the list is: "<<'\t';
	cout<<e->Name<<endl;
 cout<<endl;
}
 

void erase(Sheet *L)
{
 Sheet *Current = L;

 while(Current != NULL || (!strcmp("OldName", Current->Name)))
 {
  Current = Current->Next;
  delete Current;
  L = Current;
 }
}

	
//function to insert a new sheet
//inserts at end of list only.
void insert(char word[], Sheet *&L, Sheet *&T)
{
	Sheet *e, *curr, *prev;
  
		e = new Sheet;
		strcpy(e->Name, word);
		e->Next = NULL;
		e->Prev = NULL;

		if(L == NULL)
  {
			L = e;
		}
  else
  {
		prev = curr = L;
  
  while(curr != NULL)
  {
		   prev=curr;
		   curr=curr->Next;
		}
		   e->Next = NULL;
		   prev->Next = e;
		   e->Prev = prev;
		   T = e;
	}
}


//function to display the current sheets in list
void display(Sheet *&L)
{
	Sheet *fuck;
	fuck = L;

	while(fuck != NULL)
 {
		cout<<fuck->Name<<endl;
		fuck = fuck->Next;
	}
}


//function to rename a sheet
void rename()
{
 Sheet *Current;
 char OldName[MAX_NAME_LEN + 1], NewName[MAX_NAME_LEN + 1];

 if(!strcmp(Current->Name, OldName))
 {
  strcpy(NewName, Current->Name);
  cout<<Current->Name;
 }
}


void strToLower(char s[])
{ int i ;
 for(i=0 ; s[i] != '\0' ; i++) 
 {
  s[i] = tolower(s[i]) ;
 }
}

right, the code above partially works and I'm hoping somebody out there can help me understand why some of the functions and code wont work.

It's a double linked list.
user has to be able to

  1. move right throught the list and back left.
  2. jump to the head and jump to the tail.
  3. insert a new sheet anywhere and be in alphabetical order
  4. delete any sheet from anywhere
  5. rename a sheet
  6. display the current sheets in the list
  7. name are case insensitive

There are a few of us working on this together and so far, the display, jump to head/tails and insert [but only at tail] works.

MS Visual C++ 6.0 just crashes when I press 'R' or 'L' to move left or right.
Delete doesn't seem to like searching for the sheet we want to delete.
Rename I think is the comparing strings.

Any help or ideas is greatly appreciated.

thanks
/InvalidDLL

Hello,

I am not an expert programmer, but it looks to me that you should define the struct before you do the typdef statement. It might be possible to combine them as shown-- I am not sure.

You should also have a head pointer, and a tail pointer, and be using nulls to end your code. I'd be curious to see the whole code piece, and see what your headptr and tailptrs are doing. I think your heads are getting misused.

It might be handy while you are coding to actually draw out the list on paper. Draw out the head pointer. Define it in your code. Draw your structure. Define it in your code. Code in the head pointer assignment to the structure -- on your paper, make the arrow. Make the first data structure prev socket == null. Add that to your paper. Flow out the whole process on your paper, to help you out. Don't draw the arrows, or initialize any of the boxes until you manufacture the code.

Christian

after doing some more work, it is apparent that the head pointer is gettin fucked over somewhere :confused:

I'm back with the group after the weekend so we'll put own collective heads together and try and fix this.

I'll post back anyway with anything we get working just for reference :)

/InvalidDLL

/* * *   A R R   * * */
//using c++
class CSheet{
  public:
    CString  Name;
    CSheet * Nxt;
    CSheet * Prv;
   //default constructor 
    CSheet():Nxt(NULL),Prv(NULL){}
   // destructor binds objects Nxt and Prv
    ~CShett(){
         if(Nxt)Nxt->Prv=Prv; 
         if(Prv)Prv->Nxt=Nxt;
      }
};

// if Nxt is NULL then this is last 
// if Prv is NULL then this is first


class CSheetPtr{
   CSheet *ptr;
 public:

   void operator= (CSheet * p){ptr=p;}
   CString GetName(){return Name;}
   void SetName(CString nm){Name=nm}
   CSheet* GetSheetAddr(){return ptr;}

   CSheet* MoveNext(){return ptr=ptr->Nxt;}
   CSheet* MovePrev(){return ptr=ptr->Prv;} 
   CSheet* MoveLast()
      {
       while(ptr->Nxt)
          ptr=ptr->Nxt;
       return ptr;
      }

   CSheet* MoveFirst()
      {
       while(ptr->Prv)
          ptr=ptr->Prv;
       return ptr;
      }   

};


/*Just rote this so may not be correct but using c++ classes is better*/

A R R
you may name CSheetPtr::MoveNext CSheetPtr::operator++ so it will work as real pointers

The following code is an example of how to implement a Doubly Linked List. Let me know what you think of the code and if it has helped you in any way.

// "DoublyLinkedList.cpp"
#include<iostream>
#include<conio.h>
using namespace std;
class node
{ 
public:
 int data;
 node *right_link, *left_link;
};
class friend_node
{
private:
 
 node *head, *current_node;
 void add_to_list(int);
 int move_right();
 int move_left();
 void display_list();
 void delete_record();
 void dispose_list();
 void print_options();
public:
 
 friend_node();
 ~friend_node();
 void handle_choice();
};
friend_node::friend_node()
{
 head = new node;
 head->data = 0;
 head->left_link = head;
 head->right_link = head;
 current_node = head;
}
friend_node::~friend_node()
{
 delete head;
}
void main()
{
 friend_node go;
 cout << "Welcome to the Doubly Linked List Simulation\n\n";
 cout << "1 - Exit program\n";
 cout << "2 - Add\n";
 cout << "3 - Move left\n";
 cout << "4 - Move right\n";
 cout << "5 - Print\n";
 cout << "6 - Delete\n";
 cout << "7 - Dispose\n";
 cout << "8 - Show these options again\n";
 go.handle_choice();
}
void friend_node::handle_choice()
{
 friend_node free_memory;
 int value;
 int choice;
 while (choice != 1)
 {
  cout << "\nEnter choice: ";
  cin >> choice;
  int temp = 0;
  switch(choice)
  {
   case 1:
   free_memory.~friend_node();
   cout << "\nThank you!\n\n";
   getch();
   exit(1);
   break;	  
  
   case 2:
   cout << "\nEnter a number: ";
   cin >> value;
   temp = value;
   cout << "\n" << temp << " has been added!\n";
   getch();
   add_to_list(value); 
   break;	  
   
   case 3:
   cout << "\nCurrent data to the left is:  " << move_left() << "\n\n";
   getch();
   break;	  
   
   case 4:
   cout << "\nCurrent data to the right is:  " << move_right() << "\n\n";
   getch();
   break;
   case 5:
   display_list();
   break;
   
   case 6:
   delete_record();
   break;
   case 7:
   dispose_list();
   break;
   
   case 8:
   print_options();
   break;
   default:
   cout << "\nInvalid choice!\n\n";
   getch();
   break;
  }
 }
}
void friend_node::add_to_list(int add_data)
{
 node *add = new node;
 add->data = add_data;
 add->left_link = current_node;
 add->right_link = current_node->right_link;
 current_node->right_link->left_link = add;
 current_node->right_link = add;
 current_node = add;
}
int friend_node::move_left()
{
 char empty[30] = {"\n\n\a\a\a\aThe list is empty!\n\n"};
 if(head->right_link == head)
 {
  cout << empty << endl;
  getch();
  handle_choice();
 }
 else
 {
  current_node = current_node->left_link;
   
  if(current_node == head)
  {
   current_node = current_node->left_link;
   return (current_node->data);
  }
  else
   return(current_node->data);
 }
 return (0);
}
int friend_node::move_right()
{
 char empty[30] = {"\n\n\a\a\a\aThe list is empty!\n\n"};
 
 if(head->right_link == head)
 {
  cout << empty << endl;
  getch();
  handle_choice();
 }
 
 else
 {
  current_node = current_node->right_link;
  if(current_node == head)
  {
	current_node = current_node->right_link;
	return (current_node->data);
  }
  else
   return(current_node->data);
 }
 return (0);
}
void friend_node::display_list(void)
{
 char empty[30] = {"\n\n\a\a\a\aNothing to print!\n\n"};
 node *print;
 if(head->right_link == head)
 {
  cout << empty << endl;
  getch();
  handle_choice();
 }
 else
 {
  cout << "\nCurrent data in the list:  ";
  print = head->right_link;
  while(print != head)
  {
   cout << print->data <<" ";
   print = print->right_link;
  }
 }
 getch();
 cout <<"\n\n";
}
void friend_node::delete_record()
{
 char empty[30] = {"\n\n\a\a\a\aNothing to delete!\n\n"};
 node *temp_current = new node;
 node *temp_current2 = temp_current;
 temp_current = current_node;
 if (head->right_link == head)
 {
  cout << empty << endl;
  getch();
  handle_choice();
 }
 while(current_node != head)
 {
  cout << "\n\n" << current_node->data << " has been deleted!\n\n";
  current_node->left_link->right_link = current_node->right_link;
  current_node->right_link->left_link = current_node->left_link;
  current_node = current_node->right_link;
  current_node = current_node->left_link;
  getch();
  handle_choice();
 }
}
void friend_node::dispose_list()
{
 char empty[30] = {"\n\n\a\a\a\aNothing to dispose!\n\n"};
 if(head->right_link == head)
 {
  cout << empty << endl;
  getch();
  handle_choice();
 }
 while(current_node != head)
 {
  current_node->left_link->right_link = current_node->right_link;
  current_node->right_link->left_link = current_node->left_link;
  current_node = current_node->right_link;
  current_node = current_node->left_link;
  if(current_node == head)
  {
   cout << "\n\nThe list has been disposed!\n\n";
   getch();
  }
 }
}
void friend_node::print_options()
{
 cout << "\n\n1 - Exit program\n";
 cout << "2 - Add\n";
 cout << "3 - Move left\n";
 cout << "4 - Move right\n";
 cout << "5 - Print\n";
 cout << "6 - Delete\n";
 cout << "7 - Dispose\n";
 cout << "8 - Show these options again\n\n";
}
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.