Chinaunix首页 | 论坛 | 博客
  • 博客访问: 48336
  • 博文数量: 10
  • 博客积分: 229
  • 博客等级: 二等列兵
  • 技术积分: 110
  • 用 户 组: 普通用户
  • 注册时间: 2012-03-05 15:47
文章分类
文章存档

2012年(10)

我的朋友

分类: C/C++

2012-03-09 16:10:50

创建

点击(此处)折叠或打开

  1. typedef struct node{
  2.    int data;
  3.    struct node *next;
  4.   }node_t;
  5.  
  6.    static node_t *mk_node(int val)
  7.   {
  8.   node_t *p = malloc(sizeof(node_t));
  9.   if(p != NULL){
  10.  p->data = val;
  11.   p->next = NULL;
  12.   }
  13.   return p;
  14.   }
插入

点击(此处)折叠或打开

  1. node_t *link_insert(node_t *head, int val)
  2.   {
  3.   node_t *cur = mk_node(val);
  4.   node_t *tail;
  5.  
  6.   if(cur == NULL || head == NULL){
  7.   if(head == NULL)
  8.   head = cur;
  9.   return head;
  10.   }
  11.  
  12.   for(tail = head; tail->next != NULL; tail = tail->next)
  13.   ;
  14.   tail->next = cur;
  15.   return head;
  16.   }
显示

点击(此处)折叠或打开

  1. void link_show(node_t *head)
  2.   {
  3.   node_t *cur;
  4.  
  5.   for(cur = head; cur != NULL; cur = cur->next)
  6.   printf("--%d--\n", cur->data);
  7.   }
销毁

点击(此处)折叠或打开

  1. void link_print_destroy(node_t *head)
  2.   {
  3.   node_t *cur, *next;
  4.  
  5.   for(cur = head; cur != NULL; cur = next){
  6.   next = cur->next;
  7.   printf("--%d--\n", cur->data);
  8.   free(cur);
  9.   }
  10.   }
删除结点

点击(此处)折叠或打开

  1. node_t *del_node(node_t *head)
  2.   {
  3.   char str[20];
  4.   node_t *cur, *pre, *del;
  5.  
  6.   printf("input a name:");
  7.   scanf("%s", str);
  8.  
  9.   pre = cur = head;
  10.   while(cur){
  11.   if(!strcmp(cur->name, str)){
  12.   del = cur;
  13.   if(cur == head){
  14.   head = pre = cur = cur->next;
  15.   }else{
  16.   pre->next = cur = cur->next;
  17.  }
  18.   free(del);
  19.   }else{
  20.   pre = cur;
  21.   cur = cur->next;
  22.   }
  23.      }
  24.   return head;
  25.   }
创建双链表

点击(此处)折叠或打开

  1. typedef struct node *link_t;

  2.   typedef struct node{
  3.   unsigned char item;
  4.   link_t prev, next;
  5.   }node_t;
  6. link_t make_node(unsigned char item)
  7.   {
  8.   link_t p = malloc(sizeof(node_t));
  9.   if(p != NULL){
  10.   p->item = item;
  11.   p->prev = p->next = NULL;
  12.   }
  13.   return p;
  14.   }
创建二
  1. node_t *make_node(int data, node_t *next)
  2.  13 {
  3.  14 node_t *cur = malloc(sizeof(node_t));
  4.  15 cur->data = data;
  5.  16 cur->next = next;
  6.  17 return cur;
  7.  18 }
  8.  19 int i;
  9.  23 node_t *cur, *del;
  10.  24
  11.  25 cur = make_node(1, NULL);
  12.  26 cur->next = cur;
  13.  27
  14.  28 for(i = 2; i <= PERSONS; i++)
  15.  29 cur = cur->next = make_node(i, cur->next);







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