#include "stdio.h"
#include "iostream.h"
#include "malloc.h"
struct LINKLIST
{
   int data;
   struct LINKLIST *head,*last,*next;
};
int choose;
/******************************/
/*         按序号查找         */
/******************************/
struct LINKLIST * get(int i, struct LINKLIST *head)
{
   int j;
 struct LINKLIST * p;
   p=head;
   while((j<i)&&(p->next!=NULL))
     {
        p=p->next;
        j++;
     }
   if(j==i)
     return p;
   else
     return NULL;       
}
/******************************/
/*          按值查找          */
/******************************/
struct LINKLIST *locate(int x,struct LINKLIST *head)
{
   struct LINKLIST *p;
   p=head->next;
   while(p!=NULL)
     if(p->data==x)
 return p;
     else
 p=p->next;
   return NULL; 
}
/******************************/
/* 在以知结点的后面插入新结点 */
/******************************/
void insertafter(int x,struct LINKLIST *p)
{
   struct LINKLIST *t;
   t=(struct LINKLIST *)malloc(sizeof(struct LINKLIST));
   t=p->next;
   p->next=t;
}
/******************************/
/* 在以知结点的前面插入新结点 */
/******************************/
int insertbefor(int x,int i,struct LINKLIST *head)
{
   LINKLIST *p;
   int r=1;
   p=get(i-1,head);
   if(p!=NULL)
     insertbefor(x,i,p);
   else
     r=0;
   return r;
}
/******************************/
/*        删除第i个结点       */
/******************************/
int deleteordor(int i,struct LINKLIST *head)
{
   struct LINKLIST *p;
   int r=1;
   p=get(i-1,head);
   if((p!=NULL)&&(p->next!=NULL))
     deleteordor(i,p);
   else
     r=0;
   return r;
}
void main()
{
      printf("*********************************************\n");
    printf("*************单向循环链表的操作**************\n");
    printf("*********************************************\n");
    
    printf("首先,请输入链表,以$结束\n");
    struct LINKLIST *head,*last,*t;
   char ch;
    t=(struct LINKLIST *)malloc(sizeof(struct LINKLIST));    //建立表头结点
   head=t;
   last=t;
   t->next=NULL;
   while(ch=getchar()!='$')
     {
    t=(struct LINKLIST *)malloc(sizeof(struct LINKLIST));  
    t->data=ch;
    last->next=t;
    last=t;
    t->next=head;
     }
    
    printf("现在,请选择您要的操作: \n");
    printf("1..............................按序号查找 \n");
    printf("2..............................按值查找 \n");
    printf("3..............................在以知结点的后面插入新结点\n");
    printf("4..............................在以知结点的前面插入新结点\n");
    printf("5..............................删除第i个结点 \n");
    
    printf("请输入您要进行的操作:\n");
    scanf("%d",&choose);
    while(choose!=1&&choose!=2&&choose!=3&&choose!=4&&choose!=5)
    {    printf(" 错误数字! ");
        scanf("%d",&choose);
    }
    while(choose==1||choose==2||choose==3||choose==4||choose==5)  
{    switch(choose)
    {
    case 1:struct LINKLIST * get(); break;
    case 2:struct LINKLIST *locate(); break;
    case 3:void insertafter(); break;
    case 4:int insertbefor(); break;
    case 5:int deleteordor(); break;
    }
      printf("请输入您要进行的操作:\n");
    scanf("%d",&choose);
}
}



											
	    

	
