单向循环链表操作

#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <time.h>
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;i<n;++i)
   {
   	  p=(node*)malloc(sizeof(node));
   	  p->data=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;i<n;++i)
   {
   	  p=(node*)malloc(sizeof(node));
   	  p->data=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<length(head1);i++)
  {
  	  printf("SearchNode:index:%d,data:%d\n",i+1,SearchNode(head1,i+1));
  	  //printnode(SearchNode1(head1,i+1));//这里打印就有问题 
  }
  
  InsertNode(head1,1,101); 
  printnode(head1);
  InsertNode(head1,12,102); 
  printnode(head1);
  InsertNode(head1,14,103); 
  printnode(head1);
  
  
  
  int data=0;
  DeleteNode(head1,1,data);
  printf("the data is:%d\n",data);
  printnode(head1);
  
  DeleteNode(head1,12,data);
  printf("the data is:%d\n",data);
  printnode(head1);

  
  ReverseNode(head1);
  printnode(head1);
  return 0;
} 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值