Solved

nietod...Help with algorithm

Posted on 2000-02-14
4
286 Views
Last Modified: 2010-04-02
You solved the josephus algorithm i posted. I'm trying to implement it now with a circular linked list. An have no idea where to start it. this is the class im using:

// Circular Linked List Class
#ifndef CIRCLIST_H
#define CIRCLIST_H

#include <iostream.h>

template <class T>
class CircularList {
public:
   CircularList();
   ~CircularList();
   int Add(T item);                     // return -1 if out of memory
   int Remove(int index);
   int Get(int index, T&item) const;    // return -1 if not gotten
   int Find(T item) const;              // return -1 if not found
   int Empty() const { return size==0; }
   int Size() const { return size; }
   struct Node {
      T data;
      Node *left, *right;
   };
private:
   Node *head;
   int size;
};


//----------------------------//
template <class T>
CircularList<T>::CircularList() {
   head=new Node;
   head->right=head;
   head->left=head;
   size=0;
}


//----------------------------//
template <class T>
CircularList<T>::~CircularList() {
   while (Size()>0) Remove(0);
   delete head;
}


//----------------------------//
template <class T>
int CircularList<T>::Add(T item) {
   Node *temp, *cur;
   temp = new Node;                    // get a new node and fill with data
   if (temp==NULL) { cout << "Out of memory for linked list node allocation: Add() failed." << endl; return -1; }
   temp->data = item;
   cur = head->right;

   while(cur != head  &&  cur->data < temp->data) {
      cur=cur->right;
   }

   temp->right = cur;
   temp->left = cur->left;
   cur->left->right = temp;
   cur->left = temp;

   size++;                             // adjust size
   return 1;
}



//----------------------------//
template <class T>
int CircularList<T>::Remove(int index) {
   if (Empty() || index<0 || index>=Size() ) return -1;
   Node *cur;
   cur = head->right;
   for (int i=0; i<index; i++) {
      cur=cur->right;
   }                                   // at end of loop, cur is pointing
                                       // to the node to delete
   cur->left->right = cur->right;
   cur->right->left = cur->left;

   delete cur;                         // free up space
   size--;                             // adjust size
   return size;
}


//----------------------------//
template <class T>
int CircularList<T>::Get(int index, T &item) const {     // similar to Remove
   if (Empty() || index<0 || index>=Size() ) return -1;
   Node *cur;
   cur = head->right;
   for (int i=0; i<index; i++) {
      cur=cur->right;
   }                                   // at end of loop, cur is pointing
                                       // to the node to get
   item=cur->data;                     // transfer data
   return 1;
}


//----------------------------//
template <class T>
int CircularList<T>::Find(T item) const {
   if (Empty()) return -1;
   int index=0;
   Node *cur=head->right;
   while (cur!=head && item!=cur->data) {
      cur=cur->right;
      index++;
   }
   return (cur==head ? -1 : index);
}

#endif



0
Comment
Question by:milalik
[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
  • 3
4 Comments
 
LVL 22

Expert Comment

by:nietod
ID: 2523033
What exactly is the question?

I assume the josephus algorithm needs to store data (I'm not familar with the algorithm) so the data will be stored in this circular queue.
0
 
LVL 22

Accepted Solution

by:
nietod earned 50 total points
ID: 2523172
I found the algorithm from your other question.

Since this is probably accademic, I can't give you the answer--that is unethical--but I'll give you some help to get you started.  Ask if you have questions or need more help.

Start with all the items in the queue  Then use a loop to remove items from the queue.  Each item removed is placed back in the queue until you come to an item that should be "skipped".  If the item should be skipped don't place it back in.  When you come to a case where you remove an item from the queue and the queue is left empty (i.e. there had been 1 item in the queue), then you are done.  The item you just removed is te last one.
0
 

Author Comment

by:milalik
ID: 2523442
Okay so I add the items to the list in the main. Then perform the Remove with the conditions every m-time..and when the only one number ends I'm done?
0
 
LVL 22

Expert Comment

by:nietod
ID: 2523463
That's it.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Fast, free, private online code repository / editor 7 113
How to gracefully close the c++ 11 thread? 3 123
Add values of each row in an array 3 70
learn programming 8 70
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

733 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