Chinaunix首页 | 论坛 | 博客
  • 博客访问: 347155
  • 博文数量: 78
  • 博客积分: 3380
  • 博客等级: 中校
  • 技术积分: 857
  • 用 户 组: 普通用户
  • 注册时间: 2010-06-16 19:39
文章分类

全部博文(78)

文章存档

2011年(31)

2010年(47)

分类: C/C++

2010-08-31 21:32:01

/* ************************************************************************
 *       Filename:  main.c
 *    Description: 
 *        Version:  1.0
 *        Created:  08/31/2010 05:13:53 PM
 *       Revision:  none
 *       Compiler:  gcc
 *         Author:  chengbin_liu,
 *        Company: 
 * ************************************************************************/
#include
#include
#include
#include
typedef struct node
{
 int data;
    struct node *next;
}node;
//==============================================================================
node *create()
{
 int i=0;
 node *head,*p,*q;
 int x=0;
 head=(node *)malloc(sizeof(node));
 while(1)
 {
  printf("input the data:");
  scanf("%d",&x);
  if(x==0)
   break;
  p=(node *)malloc(sizeof(node));
  p->data=x;
  if(++i==1)
  {
   head->next=p;
  }
  else
  {
   q->next=p;
  }
  q=p;
 }
 q->next=NULL; 
 return head;  
}
//=================================================================================
void print(node *head)
{
 node *p;
 int index=0;
 if(head->next==NULL)
  return;
 p=head->next;
 while(p!=NULL)
 {
  printf("the %d node is %d\n",++index,p->data);
  p=p->next;
 }
}
//================================================================================
node *search_node(node *head,int pos)
{
 node *p=head;
 if(pos<0)
 {
  return NULL;
 }
 if(pos==0)
 {
  return head;
 }
 if(p==NULL)
 {
  return NULL;
 }
 while(--pos)
 {
  if((p=p->next)==NULL)
  {
   break;
  }
 }
 return p;
}
//=============================================================================
node *insert_node(node *head,int pos, int data)
{
 node *item=NULL;
 node *p;
 item=(node *)malloc(sizeof(node));
 item->data=data;
 if(pos==0)
 { 
  head->next=item;
  return head;
 }
 p=search_node(head,pos);
 if(p!=NULL)
 {
  item->next=p->next;
  p->next=item;
 }
 return head;
}
//=============================================================================
node *delete_node(node *head,int pos)
{
 node *item=NULL;
 node *p=head->next;
 if(p==NULL)
 {
  printf("link is empty!\n");
  return NULL;
 }
 p=search_node(head,pos-1);
 if(p != NULL && p->next != NULL)
 {
  item = p->next;
  p->next = item->next;
  // delete item;
 }
 return head;
}
//=============================================================================
node *reverse(node *head)
{
 node *p,*q,*r;
 if(head->next==NULL)
 {
  return head;
 }
 p=head->next;
 q=p->next;
 p->next=NULL;
 while(q!=NULL)
 {
  r=q->next;
  q->next=p;
  p=q;
  q=r;
 }
 head->next=p;
 return head;
}
//==============================================================================
node *search_mid(node *head)
{
 int i=0;
 int j=0;
 node *current=NULL;
 node *middle=NULL;
 current=middle=head->next;
 while(current!=NULL)
 {
  if(i/2 > j)
  {
   j++;
   middle=middle->next;
  }
  i++;
  current=current->next;
 }
 return middle;
}
//============================================================================
bool IsLoop(node *head,node **start)
{
 node *p1=*p2=head;
 if(head==NULL || head->next==NULL)
 {
  return false;
 }
 do
 {
  p1=p1->next;
  p2=p2->next->next;
 }while(p2 && p2->next && p1!=p2);
 if(p1=p2)
 {
  *start=p1;
  return true;
 }
 else
 return false;
}
//=============================================================================
int main()
{
 //int pos,data;
// int pos;
 node *head=create();
 printf("djkfhjkshdfj\n");
// printf("length=%d\n",Length(head));
// printf("input the delete node:");
// scanf("%d",&pos);
 
 //head=search_node(head,pos);
 //head=insert_node(head,pos,data);
 //head=delete_node(head,pos);
 //head=reverse(head);
 //head=search_mid(head);
//=================================================================
 bool bloop=false;
 node *start=head->next->next->next->next;
 start->next=head->next;
 node *loopstart=NULL;
 bloop=IsLoop(head,&loopstart);
 printf("bloop=%d\n",bloop);
//=================================================================
 print(head);
 return 0;
}
 
阅读(607) | 评论(0) | 转发(0) |
0

上一篇:linux网络编程 源码

下一篇:循环链表

给主人留下些什么吧!~~