#include <iostream.h>
#include <stdlib.h>
#include <string.h>
#define MAX -1;
class WaitItem {
public:
  int type;              /* type of the message a process is waiting for */
  int sockFd;         /* the socket descriptor the process communicates thru*/
  WaitItem(int ty,int sockId); /* constructor */
  WaitItem(int ty); /* constructor */
};

class WaitListItem      /* an item of the waitlist */
{
public: 
  WaitItem*    waitItemPtr; /*pointer to wait Item*/
  WaitListItem* next;      /* pointer to next element  on list */
  WaitListItem(int ty, int sfd); /* constructor */
  WaitListItem(int ty);         /* constructor*/
  
  WaitListItem();      /* constructor to create a dummy item */
 
};

class WaitList
{                  /* list of waiting processes */
private:
  WaitListItem* first;   /* pointer to the head of the list */
  WaitListItem* last;    /* pointer to the tail */
  WaitListItem* counter; /* counter for iteration */
public:
  WaitList();
  
  void add_item(int key,int sockId); /* add a waiting process to list */
  WaitItem* delete_item(int key);         /* delete a process with type = key */
  int is_empty();                    /* is the list empty */
  WaitItem* next();                        /* return the next key */
  
  void reset();                      /* reset the counter to the head*/
};                                          /* of the list */

WaitItem::WaitItem(int ty,int sockId) 
{
  type=ty;
  sockFd=sockId;
}

WaitItem::WaitItem(int ty) 
{
  type=ty;
}


WaitListItem:: WaitListItem(int ty, int sfd) 
{
  waitItemPtr=new WaitItem(ty,sfd);
  next=0;
}



WaitListItem:: WaitListItem(int ty) 
{ 
  waitItemPtr=new WaitItem(ty);
  next=0;
}

WaitListItem::WaitListItem() {
  next=0;
}
WaitList::WaitList() 
{
  counter=first=last=0;
}

void WaitList::add_item(int key,int sockId) /* add an item of type key and 
                                            and socket descriptor sockID */
{ 
  WaitListItem *waitListItem=new WaitListItem(key,sockId);
  /* if the list is empty then add the item to the list and adjust counter
  to point to the head of the list */
  if (is_empty()) {         
    first=last=waitListItem;
    counter=first;
  }
  else       /* list is not empty */
    {
      last->next=waitListItem; /* add the item to the tail of the list */
      last=waitListItem;    /* set tail to the last item addded */
    }
  return;
}

void WaitList::reset()  /* reset the counter to the head so that 
                     another iteration can be done */
{
  counter=first;
}

 
  
WaitItem* WaitList::delete_item(int key)    /* delete an item of key type */
{ if (first==0) return 0;    /* if list is empty return null */
 /* if the first element has type =key then return the waitItem
   and reset first */
  if ((first->waitItemPtr)->type==key) 
    { WaitItem* waitPtr=first->waitItemPtr; /* store the item required*/
      if (last==first)  /* list has only one element */
	{      
	  first=first->next;
	  last=first;
        }
      else first=first->next;  /* else check the next element */
      return waitPtr;       /* return the item */
    }
  else /* the first element is not what we are looking for */
    {
      WaitListItem  *prev,*temp;
      temp=first;prev=0; /* prev points to the item immediately before temp*/
      while (temp!=0)       /* while we are not at the end of the list */
	{
      /* if we find the item then  set prev's successor to the successor 
     of temp and remove temp from the list */
	  if ((temp->waitItemPtr)->type==key)  
	    { WaitItem* waitPtr=temp->waitItemPtr;
	      prev->next=temp->next;
	      if (last==temp) last=prev;
	      return waitPtr;
	    }
	  else 
	    { /* move on to the next item on the list */
	      prev=temp;
              temp=temp->next;
            }
	}
    }
  return 0;                         
}

int WaitList::is_empty() 
{
  return (first==0);
}             
                       
WaitItem*  WaitList::next() 
{                         /* return the next socket descriptor */
  if (first=0) { /* list os empty */
    cout << " waitlist is empty " ;
    return 0;}
  if (counter==0) 
    { 
      counter=first ;return 0;
    }
  WaitItem* temp= counter->waitItemPtr;
  counter=counter->next;

  return temp;
}
         
    
int main(int argc,char* argv[]) 
{
  WaitList l;
  int i=1;
  
  switch(argc) 
    {
    case 1: cout << " no numbers entered " << endl; exit(0);
	    
	  default: while (argc-- >> 1)
	    l.add_item(atoi(argv[i++]),i); /* add numbers into list*/
	    break;
	  }
    cout << "enter search type " << endl;
    cin >> i;
    WaitItem* waitPtr=l.delete_item(i);
  if (waitPtr==0) cout << "key not present";
     else cout << waitPtr->type << " " << waitPtr->sockFd;
			    
}                      
     
 







































