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

全部博文(36)

文章存档

2013年(29)

2011年(5)

2010年(2)

我的朋友

分类: C/C++

2013-03-20 15:39:09

#include
#include
#include
 
typedef struct node
{
  char name[20];
  struct node *next, *prior;
} NODE;

// creat... 
NODE *creat(int n)
{
  NODE *p, *q, *head;
  int i;  
  if ((head = (NODE *)malloc(sizeof(NODE))) == NULL)
    return NULL ;
   head ->name[0] = '\0';
   head ->next = NULL;
   head ->prior = NULL;
   q = head;
   for (i=0; i    {
    if ((p = (NODE *)malloc(sizeof(NODE))) == NULL)
       return NULL ;
     q->next = p;
     scanf("%s", &p->name);
     p->next = NULL;
     p->prior = q;
     q = p;
   } 
   q->next = head;
   head->prior = q;
   return head;
}
// -- print
int print_chain(NODE *head)
{
  NODE *p;
  if (head == NULL)
   return -1;
 p = head->next;
 while (p != head)
 {   printf("Name is %s\n", p->name);
     p=p->next;
 }
 return 0;
}
//--
NODE *search(NODE *head, char *name)
{
  NODE *p;
  p = head->next;
  while(p != head)
  {
   if (strcmp(p->name, name) == 0)
  return p;
   else   p = p->next;
  }
 printf("No name found \n");
 return NULL;
}
// ---insert before p_name
int insert(NODE *head, char *p_name, char *in_name)
{
   NODE *p, *newnode;
    if(head == NULL)
    return -1; 
   if ((p = search(head, p_name)) == NULL)
   {
     printf("No place found !\n");
     return -1;
   }
     
   if ((newnode = (NODE *)malloc(sizeof(NODE))) == NULL)
   { printf("No mem place left!\n");
     return -1;
   } 
   strcpy(newnode->name, in_name);
   newnode->next = p;
   newnode->prior = p->prior;
   (p->prior)->next = newnode;    
   p->prior = newnode;
   return 0;
}
//--
int del(NODE *head, char *d_name)
{
   NODE *p;
  if (head == NULL)
     return -1; 
   if ((p = search(head, d_name)) == NULL)
   {   printf("Node was not found !\n");
       return -1;
   }  
  p->next->prior = p->prior;
  p->prior->next = p->next;
  return 0;
}
//--
int main()

   NODE *head ;
   char s_name[20];
   char in_name[20];
   char d_name[20];
   int num; 
   printf("Please input the node No you want to create\n");
   scanf("%d", &num);
   head = creat(num);
   print_chain(head);
 
   printf("Please enter the name you want to search:\n");
   scanf("%s", s_name);
   printf("Please enter the name you want to insert:\n");
   scanf("%s", in_name);
   insert(head, s_name,in_name);
   print_chain(head);
   printf("Please enter the name you want to delete:\n");
   scanf("%s", d_name);
   del(head, d_name);
   print_chain(head);
   return 0; 
}
/*--  E  --*/
谢谢欣赏,欢迎指正!
阅读(773) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~