?
Solved

Contents displaying incorrectly

Posted on 2003-03-19
2
Medium Priority
?
267 Views
Last Modified: 2010-04-17
The following code reads in characters from a file, stores them in a list, displays them and the lenght of the list. I have written a function named tothescreen that is to display the contents of the remaining list. The object of the code is to be able to select the number of spaces to skip before you delete a character. The code is to continue to skip that same number of spaces until there is only one remaining character. That character is to be displayed with the word survived following. With each cycle through the list the code is display which character caused the other character to be deleted. An example is as follows:
               Skip number: 4
               Enter the file name: //wherever the text file is located
                       size = 7 :  (j,t,l,w,b,m,x)
               Enter the first killer: b
                    B kills T  size=6:  (j,l,w,b,m,x)
                    L kills X  size=5:  (j,l,w,b,m)
                    J kills M  size=4:  (j,l,w,b)
                    J kills J  size=3:  (l,w,b)
                    L kills W  size=2:  (l,b)
                    B kills B  size=1:  (l)
                    L survived.

The problem with my code is that when I read the contents of the text file into my list, I get the correct size but the contents are all displayed as -1. I am unable to see if the retrieves and deletes are happening as they are suppose due to the fact that all of the values are negative 1. My code is as follows:

//************************************
// Header File of CIRCLINKEDLIST.CPP
//
//
//************************************

#include <cstddef>
#ifndef _CIRCLINKEDLIST_H
#define _CIRCLINKEDLIST_H
using namespace std;
   // the type of object that the list will be store
   typedef char ItemType;

   struct Node {
        ItemType item;
        Node* next;
       
        Node() { next = NULL; };
        Node(ItemType _item, Node* _next) { item = _item; next = _next; };
   };

class List {
public:

   
   // an empty linked-list is created
   List();
   
   // cleanup the list
   ~List();

   // add the element e to the head of the list
   void Insert(ItemType e);

   // delete an element from the list
   void Delete(ItemType e);

   // if found, return the cost (at least 1)
   // if not found, return -1
   int Retrieve(ItemType item);
   
   // print out the whole list from head to tail
   void Print();

   // the size of the linked list
   int Size();

   //returns true if list is empty
   bool isEmpty() const;

private:

   
   // insert newNode after node
   // (... -> node -> newNode -> ...)
   void InsertAfter(Node* node, Node* newNode);
   
   // delete node->next
   bool DeleteNextNode(Node* node);
   

   // head always points to a dummy head
   Node* head;
   
 // cur keeps track of where you are
 Node* cur;

 // points to the end of the list
 Node* tail;

   // the size of the list
   int size;
};

#endif

//*****************************************
//Implementation File for CIRCLINKEDLIST.H
//
//
//******************************************
#include <iostream>
#include "CIRCLINKEDLIST.H"

////////////////////////////////////////////////////////////////
// constructor and destructor
////////////////////////////////////////////////////////////////

List::List() {
   size = 0;
   
   // insert a dummy head,
   // the value does not mean anything
   head = new Node;
   head->next = head;
   tail=head;
   cur=head;
};

List::~List() {
   while(head->next != head) DeleteNextNode(head);
   delete head; // delete the dummy node
};
///////////////////////////////////////////////////////////////
// insert
///////////////////////////////////////////////////////////////
void List::Insert(ItemType item) {
  InsertAfter(tail, new Node(item, NULL));
};

///////////////////////////////////////////////////////////////
// delete
///////////////////////////////////////////////////////////////
void List::Delete(ItemType item){
   DeleteNextNode(cur);
};
////////////////////////////////////////////////////////////////
// size
////////////////////////////////////////////////////////////////
int List::Size() {
   return size;
};

////////////////////////////////////////////////////////////////
// isEmpty
////////////////////////////////////////////////////////////////
bool List::isEmpty() const
{
    return bool(size==0);
};

////////////////////////////////////////////////////////////////
// print
////////////////////////////////////////////////////////////////

void List::Print() {
  Node* n = head->next; // points to the real first element
   cout << '<';
   while (n != head) {
        cout << n->item << ' ';
        n = n->next;
   };
   cout << "> size = " << Size() << endl;
};

////////////////////////////////////////////////////////////////
// retrieve member function
////////////////////////////////////////////////////////////////

int List::Retrieve(ItemType item) {
   Node* curr = head->next; // points to the real first element
   int cost = 1;
   
   while ((curr != head) && (curr->item != item)) {
        curr = curr->next; // advance to next element
        cost++;
   };
   
   if (curr == head) {
        // cannot find item in the list
        cost = -1;
   };
   
   return cost;
};

////////////////////////////////////////////////////////////////
// insert member functions
////////////////////////////////////////////////////////////////
/*
void List::InsertAfter(Node* node, Node* newNode) {
   Node* afterNode = node->next;
   node->next = new Node(newNode->item, afterNode);
   afterNode = NULL;
   size++;
};
*/
void List::InsertAfter(Node* node, Node* newNode) {
  Node* afterNode = node->next;
  node->next = newNode;
  newNode->next = afterNode;
  if (node == tail)
     tail = newNode;
  size++;
};
////////////////////////////////////////////////////////////////
// delete member functions
////////////////////////////////////////////////////////////////

bool List::DeleteNextNode(Node* node) {
   if (NULL == node || head == node->next) {
        // the head is the dummy node, and never delete it!
        return false; // nothing can be deleted
   };
   
   Node* nextNode = node->next;
   node->next = nextNode->next;
   delete nextNode;
   size--;
   
   //return true;
};

//******************************
//    Main
//
//
//******************************


#include <string>
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
#include "CIRCLINKEDLIST.H"

////////////////////////////////////
//    function to print           //
////////////////////////////////////

void toscreen(List& list,int size,int remove)
{
   cout<<"(";
   for(int i=1;i<=size;i++)
   {
       if(i==remove)
       {
           if(i==size)cout<<")\n";
           else i++;
       }
       if(i!=remove)
       {
          cout<<list.Retrieve(i);
          if(i!=size)
          {
              if ((i+1==size)&&(i+1==remove));
              else cout<<",";
          }

          if(i==size)cout<<")\n";
       }
   }
}

////////////////////////////////////////////////
//  function to destroy characters            //
////////////////////////////////////////////////

void destroy(List& list,char& killer, int skip)
{
   char killed;
   int position=list.Retrieve(killer);
   int size=list.Size();

   for(int x=1; x<=skip;x++)
   {
       position++;
       if(position>size)
       {
           position=position%size;
       }
   }

   killed=list.Retrieve(position);
   int remove=list.Retrieve(killed);
   position++;

   if(position>size)
   {
       position=position%size;
   }

   if(size>=2)
   {
       cout<<killer<<" kills "<<killed<<"  size="<<cout.width(3)<<size-1<<": ";
       //list.Print();
       toscreen(list,size,remove);
   }
   else
   cout<<killer<<" survived.\n";

   killer=list.Retrieve(position);
   list.Delete(killed);

}


int main() {
 List list;

 int size=list.Size();


////////////////////////////////////////////////////
//read in characters from a file into the list    //
////////////////////////////////////////////////////

   char readfile[25];
   cout<<"Enter the file name: ";
   cin >> readfile;
   ifstream fin;
   fin.open(readfile);
   char next;

   if(fin.fail())
   {
       cout<<"Cannot open file.\n";
       exit(1);
   }
   while (fin >> next)
   {
         list.Insert(next);
         size++;
   }
   fin.close();
   list.Print();  //not allowed to do this/////////////////////////////////////////


////////////////////////////////////////////////
// determines who is to be killed and by whom
////////////////////////////////////////////////
   char killer;      //first killer
   int skip;        //skip number

   int length=size;
   int listempty=0;
   if(length!=0)
   {

       cout<<"           size="<<size<<"   ";
       toscreen(list,size,listempty);
       cout<<"Enter the first killer: " ;
       cin>>killer;
       cout<<"Enter the skip number: ";
       cin>>skip;

       for(int x=1;x<=size;x++)
       {
            destroy(list,killer,skip);
       }
   }
  else cout<<"List is empty "<<endl;

  system("PAUSE");
   return(0);
}
0
Comment
Question by:dat1113
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 

Author Comment

by:dat1113
ID: 8171889
I believe that I have traced the problem down to the retrieve function, but I do not know the proper way to correct it.
0
 

Accepted Solution

by:
tejas_pathak earned 120 total points
ID: 8172882
Hi,

Though i dont have much details about your problem, but from the information that you have given, the problem is with your 'curr' variable not getting modified again to the new location.
Because in the constructor of class List, you had set curr=head , by default.

List::List() {
  size = 0;
 
  // insert a dummy head,
  // the value does not mean anything
  head = new Node;
  head->next = head;
  tail=head;
  cur=head;
};




Now that curr value should be changed according to current location in the linked list i.e after inserting or deleting a item, the curr value should be refreshed. That is not happening here, so hence, the condition in retrieve fucntion

if (curr == head) {
       // cannot find item in the list
       cost = -1;
  };
 
gets satisfied  and shows -1.

Tell me more on this.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Simple Linear Regression
Six Sigma Control Plans

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question