Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2349154
  • 博文数量: 816
  • 博客积分: 10000
  • 博客等级: 上将
  • 技术积分: 5010
  • 用 户 组: 普通用户
  • 注册时间: 2008-12-17 17:57
文章分类

全部博文(816)

文章存档

2011年(1)

2008年(815)

分类:

2008-12-17 18:08:47

#include

enum Error_code{overflow,underflow,success};
struct Node  // define a Node!
{
  int entry;
  Node *next;
  Node();
  Node(int item,Node *add_on=NULL);
};
Node::Node()
{ next=NULL; }
Node::Node(int item,Node *add_on)
{
  entry=item;
  next=add_on;
}

class Cirlist  //define a class!
{
  public:
  //    Standard Circular List methods
  Cirlist();  // Constructor function,construct a empty List!
  bool empty() const;
  Error_code append(const int &item);
  Error_code serve();
      Error_code serve_a(int value);
      Error_code retrieve(int &item) const;
  void order();       // Order the Circular List entries.
  Node *gethead()
  { return rear; }
  Node *getnext(Node &n)
  { if(n.next == rear) return rear->next;  return n.next; }

  //    Safety features for linked strucures
      friend ostream& operator<<(ostream&,Cirlist &);  // Overload output
  Cirlist(const Cirlist &original); // copy Constructor function
      void operator=(const Cirlist &original);//Overload assignment
  ~Cirlist(); // Destructor

  protected:
  Node *rear;
};

Cirlist::Cirlist()  // Constructor function,construct a empty List!
// Post: The Circular List has been created and is initialized to be empty.
{
  rear=NULL;
}
Cirlist::Cirlist(const Cirlist &original)  // copy Constructor function
/* Post: Use an available List to construct a new List!
       The Stack is initialized as a copy of Circular List original. */
{
  Node *new_copy,*original_node=original.rear;
  if(original_node==NULL)  rear=NULL;
  else
  {
    rear=new_copy=new Node(original_node->entry);
while(original_node->next != rear)
{
  original_node=original_node->next;
  new_copy->next=new Node(original_node->entry);
  new_copy=new_copy->next;
}
  }
}
Cirlist::~Cirlist() // Destructor
// Post: The Circular List is cleared.
{
  while(!empty())
  serve();
  rear=NULL;
}
bool Cirlist::empty() const // Judge whether the Circular List is empty or not
//Post:Return true if the Circular List is empty,otherwise return false.
{
  if(rear==NULL) return true;
  return false;
}
Error_code Cirlist::append(const int &item)
/* Post:If there is space,x is added to the Circular List as its rear.
   Otherwise an Error-code of overlowd is returned */
{
  Node *new_rear=new Node(item);
  if(new_rear==NULL) return overflow;
  if(rear==NULL)
  {
    rear=new_rear;
rear->next=rear;
  }
  else
  {
new_rear->next=rear->next;
    rear->next=new_rear;
rear=new_rear;
  }
  return success;
}
Error_code Cirlist::serve()
/*Post:If the Circular List is not empty,the front of the Circular List has
       been removed.Otherwise an Error_code of underflow is returned.*/
{
  if(rear==NULL) return overflow;
  Node *old_rear=rear;
  if(rear==rear->next)
  rear=old_rear->next=NULL;
  rear=old_rear->next;
  delete old_rear;
  return success;  
}
Error_code Cirlist::serve_a(int value)
/*Post:If the Circular List is not empty,the first Value of the Circular List has
       been removed.Otherwise an Error_code of underflow is returned.*/
{
  Node *q,*p=rear;
  while(p->next!=rear && p->next->entry!=value)
  p=p->next;
  if(p->next==rear && p->entry!=value)
  return underflow;
  q=p->next;
  p->next=q->next;
  if(q==rear)
  {
    rear=p;
rear->next=rear;
  }
  delete q;
  return success;
}

Error_code Cirlist::retrieve(int &item) const
/*Post:If the Circular List is not empty,the front of the Circular List has
       been recorded as X.Otherwise an Error_code of underflow is returned.*/
{
  if(rear==NULL) return overflow;
  item=rear->entry;
  return success;
}



--------------------next---------------------
void Cirlist::operator=(const Cirlist &original) // Overload assignment
// Post: The Circular List is reset as a copy of Circular List original.
{
  Node *new_rear,*new_copy,*original_node=original.rear;
  if(original_node==NULL) new_rear=NULL;
  else                                        // Duplicate the linked nodes
  {
    new_copy=new_rear=new Node(original_node->entry);
while(original_node != rear)
{
  original_node=original_node->next;
      new_copy->next=new Node(original_node->entry);
  new_copy=new_copy->next;
}
  }
  while(!empty())                           // Clean out old Circular List entries
  serve();
  rear=new_rear;                            // and replace them with new entries.
}
ostream& operator<<(ostream & out,Cirlist &a)  // Overload  ouput
{
  Node *p=a.rear->next;
  while(p!=a.rear)
  {
    out<entry<<" ";
p=p->next;
  }
  out<  return out;
}

void Cirlist::order()   // Order the Circular List entries.
{
  Node *new_rear=rear,*p;
  int temp;
  while(new_rear->next->entry != rear->entry)
  {
  new_rear=new_rear->next;
  p=new_rear->next;
  while(p != rear)
  {
       if(p->entry > new_rear->entry)
   {
   temp=new_rear->entry;
   new_rear->entry=p->entry;
   p->entry=temp;
   }
   p=p->next;
  }
  }
}


void main()
{
  Cirlist clist;
  int m,n;
  cout<<"Please input m,n:\n";
  cin >>m>>n;
  for(int i=1;i<=m+1;i++)
  clist.append(i);
  cout<<"\nThe initialized List:"<  cout<  clist.order();
  cout<<"\nThe ordered List:\n";
  cout<  Node *p=clist.gethead(),*q;
  cout<<"\nThe turn of delete:\n";
  while(m>0)
  {
    for(i=0;i {
  q=p;
  p=clist.getnext(*q);
}
cout<entry<<" ";
clist.serve_a(p->entry);
m--;
p=q;
  }
  cout<}



你自己改一下就可以啦

--------------------next---------------------

阅读(1708) | 评论(2) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2009-11-05 10:03:04

An ugg bailey button better be unborn than untaught,for ignorance is the root of misfortune.Genius17 withoutugg knightsbridge education is like silver in the mine. ugg boots man is not made for defeat. an 回复 | 举报

chinaunix网友2009-11-05 10:02:34

An ugg bailey button better be unborn than untaught,for ignorance is the root of misfortune.Genius17 without ugg knightsbridge education is like silver in the mine. ugg boots man is not made for defeat. an uggs boots man can be destroyed but not defeated.No rational man can die without ugg lo pro button ? uneasy apprehension. http://www.love-ugg.com/specials.html