Chinaunix首页 | 论坛 | 博客
  • 博客访问: 187866
  • 博文数量: 36
  • 博客积分: 230
  • 博客等级: 二等列兵
  • 技术积分: 352
  • 用 户 组: 普通用户
  • 注册时间: 2010-10-22 18:09
文章分类

全部博文(36)

文章存档

2013年(29)

2011年(5)

2010年(2)

我的朋友

分类: C/C++

2013-03-19 16:36:46

#include
#include

typedef struct list
{
 int data;
 struct list *prior, *next;
} List;

List *creat_list(void)
{
  List *head = (List *)malloc(sizeof(List));
  if (head == NULL)
  {
    printf("Create fail \n");
    return NULL;
  }
  head->prior = head->next = head;
  return head;
}

//-----

int InsertList(List *head, int value)
{
  List *newnode, *p;
   if (head == NULL)
  {
    printf("No head Node !\n");
    return -1;
  }
  newnode = (List *)malloc(sizeof(List));
  if (newnode == NULL)
  {
    printf("Create fail \n");
    return -1;
  }
  newnode->data = value;
  p = head;
  while (p->next!=head)  
  {
    p = p->next;
  }
  // insert node
  newnode->next = p->next;
  p->next = newnode;
  newnode->prior = p;
  head->next = newnode; // 自己加的 
  return 0;
}

void Find(List *head, int find) 
{
  List *p;
  int i = 0;
  int flag = 0;
  p = head;
  //while(p!=head)
  while (p->next != head)
  {
    p=p->next;
    i++;
  if(p->data == find)
   {
     printf("Data: %d is No.%d\n", find, i);
     flag = 1;
   }
  }
 // if(p->data!=find && p->next==head)
  if (flag == 0)
    printf("No data found\n");
}
//---
void print(List *head)
{
  int i = 0;
  List *p = head->next;
  while(p!=head)
  {
   printf("%d \n", p->data);
   p=p->next;
   i++;
  }
  printf("Length is %d \n", i);
}

List *Delete(List *head, int x)

{
  List *p;
  int i=0;
  p=head->next;
  p=p->next;
  while(p->next!=head)
  {
    p= p->next;
    i++;
    if(p->data==x)
   {
     p->prior->next = p->next;
     p->next->prior = p->prior;
     p=p->next;
    // free(p);
     printf("Data %d is No.%d been delete! \n",x,i);
   }
  }
 return head;
}

int main()
{
  List *head = NULL;
  head = creat_list();
  InsertList(head,10);
  print(head);
  Delete(head,10);
  print(head);
  Find(head,10);

  return 0;
}
/*--  E  --*/
谢谢欣赏!



阅读(949) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~