单向循环链表操作

#include 
#include 
#include 
#include 
typedef struct _Node
{
  int data;
  struct _Node *next; 
}node;
node *CreatNode_Head(int n)
{
   node *head,*p;
   head=(node*)malloc(sizeof(node));
   if(head==NULL)
        return NULL;
   head->next=head;
   srand(time(0));
   for(int i=0;idata=rand()%100+1;
	  printf("p->data:%d\t",p->data);
   	  p->next=head->next;
   	  head->next=p;
   }
   return head;
}
node *CreatNode_Tail(int n)
{
   int i=0;
   node *head,*p,*q;
   head=(node*)malloc(sizeof(node));
   if(head==NULL)
        return NULL;
   head->next=head;
   q=head;
   srand(time(0));
   for(int i=0;idata=rand()%100+1;
   	  p->next=head;
	  printf("p->data:%d\t",p->data);
   	  q->next=p;
   	  q=p;
   }
   return head;
}
bool isempty(node *head)
{
  if(head->next==head)
    {
       printf("此链表是空的!!!:\n");
       return true;
    }
  else
    {
      printf("此链表不是空的:\n");
      return false;
    }
} 
int length(node *head)
{
	if(head==NULL)
	   return -1;
    int len=0;
    node *p=head;
    while(p->next!=head)
    {
       len++;
       p=p->next;
    }
    return len;
}
void printnode(node *p)
{
  if(p==NULL)
     return;
  node *q;
  if(isempty(p))
     return;
  else
  {
      q=p->next;
      while(q!=p)
      {
          printf("%d\t",q->data);
          q=q->next; 
     }
  }
  printf("\n"); 
  printf("print over:\n");
}


int SearchNode(node *head,int pos)
{
    if(head==NULL)
    {
    	printf("the node is NULL\n");
    	return -1;
    }
        
   if(pos<=0)
   {
        printf("the pos can not smaller than 0\n");
        return -1;
   }
   else if(head->next==head)
   {
      printf("the node is empty\n");
      return -1;
   }
   else if(pos>length(head))
   {
      printf("the pos over the length of the node\n");
      return -1;
   }
   else
   {
      while(pos--)
      {
        head=head->next;
      }
   }
   return head->data; 
}

node * SearchNode1(node *head,int pos)
{
   if(pos<0)
   {
        printf("the pos can not smaller than 0\n");
        return NULL;
   }
   else if(head->next==head)
   {
      printf("the node is empty\n");
      return NULL;
   }
   else if(pos>length(head))
   {
      printf("the pos over the length of the node\n");
      return NULL;
   }
   else
   {
      while(pos--)
      {
        head=head->next;
      }
   }
   return head; 
}

bool DeleteNode(node *head,int pos,int &data)
{
   if(head==NULL)
     return false;
   node *item=NULL;
   node *p=head;
   node *q=SearchNode1(head,pos-1);
   if((!q)||(q->next==head))
   {
   	   printf("delete failed:\n");
   	   return false;
   }
   item=q->next;
   data=item->data;
   q->next=item->next;
   free(item);
   item=NULL;
   printf("Delete success:\n");
   return true;
}

bool InsertNode(node *head,int pos,int data)
{
   if(head==NULL)
     return false;
   node *item=NULL;
   node *p=head;
   node *q=SearchNode1(head,pos-1);
   if(!q)
   {
   	   printf("Insert failed:\n");
   	   return false;
   }
   item=(node*)malloc(sizeof(node));
   item->data=data;
   item->next=q->next;
   q->next=item;
   printf("Insert success:\n");
   return true;
}

void ReverseNode(node *head)
{
	if(head==NULL)
	   return ;
    if(head->next==head)
       return ; 
	node *p,*q,*r;
	p=head->next;
	q=p->next;
	p->next=head;
	while(q!=head)
	{
		r=q->next;
		q->next=p;
		p=q;
		q=r;
	}
	head->next=p;
}

int main()
{
  int len=0;
  node *head,*head1;
  node *head2=NULL;
  printf("please input some date and creat a node\n");
  head=CreatNode_Head(10);
  printnode(head);
  printf("the length of the list is:%d\n",length(head));
  head1=CreatNode_Tail(10);
  printnode(head1);
  printf("the length of the list is:%d\n",length(head1));
  
  
  
  for(int i=0;i

 

你可能感兴趣的:(单向循环链表操作)