数据结构------链表

链表分类

  • 单向、双向
  • 有头、无头
  • 有循环、无循环
  • 可以随机组合

链表与顺序表的区别

  • 顺序表的空间是一段连续的空间,而链表的空间为不连续空间
  • 顺序表支持随机访问,链表不支持随机访问
  • 在任意位置插入/删除元素时,顺序表没有链表方便
  • 顺序表不需要频繁申请空间,而链表需要,因为链表容易造成空间碎片

无头单向不循环链表的相关接口实现

	1 #pragma once                                                                                            
    2 #include <stdio.h>                     
    3 #include <assert.h>
    4 #include <stdlib.h>   
    5    
    6 typedef struct Node
    7 {  
    8   int value;              
    9   struct Node *next;
   10 } Node;                      
   11                  
   12 typedef struct Llist
   13 {  
   14   Node *first;       
   15 } Llist;                
   16    
   17 void LinklistInit(Llist *llist)
   18 {    
   19   llist->first = NULL;     
   20 }                             
   21                  
   22 void Linklistdestory(Llist *llist )
   23 {        
   24  assert(llist);   
   25  Node *cur = llist->first;
   26  while  (cur != NULL)
   27  {
   28    Node *next = cur->next;
   29    free(cur);
   30    cur = next;
   31  }
   32 }
   33 
   34 void LinklistPushFront(Llist *llist , int v)
   35 {
   36   Node *node = (Node*)malloc(sizeof(Node));
   37   node->value = v;
   38 
   39   node->next = llist->first;
   40   llist->first = node;
   41 }
   42 
   43 void LinklistPushBack(Llist *llist ,int v)
   44 {
   45   Node *node = (Node*)malloc(sizeof(Node));
   46   node->value = v;
   47   node->next = NULL;
   48   if(llist->first == NULL)
   49   {                                                                                                     
   50     llist->first = node;
   51   }
   52   else{
   53     Node *cur = llist->first;
   54     while(cur->next !=NULL)
   55     {
   56       cur = cur->next;
   57     }
   58     cur->next = node;
   59   }
   60 }
   61 
   62 void LinklistPopBack(Llist *s)
   63 {
   64   assert(s->first != NULL);
   65   if(s->first->next == NULL)
   66   {
   67     free(s->first);
   68     s->first = NULL;
   69     return;
   70   }
   71   Node *c;
   72   for(c = s->first; c->next->next != NULL ; c = c->next );
   73   free(c->next);
   74   c->next = NULL;
   75 }
   76                                                                                                         
   77 void LinklistPopFront(Llist *s)
   78 {
   79   assert(s);
   80   Node *c = s->first->next;
   81   free(s->first);
   82   s->first = c;
   83 }
   84 
   85 Node *LinklistFind(Llist *s , int v)
   86 {
   87   for(Node *c = s->first ; c != NULL ; c = c->next)
   88   {
   89     if(c->value == v)
   90     {
   91       return c;
   92     }
   93   }
   94 }
   95 
   96 void LinklistInset(Node *pos , int v)
   97 {
   98   Node *node = (Node*)malloc(sizeof(Node));
   99   node->value = v;
  100   node->next = pos->next;
  101   pos->next = node;
  102 }
  103                                                                                                         
  104 void LinklistErase(Node *pos)
  105 {
  106   Node *next = pos->next->next;
  107   free(pos->next);
  108   pos->next = next;
  109 }
  110  
  111 void LinklistRemoveAll(Llist *s, int v)
  112 {
  113   if(s->first == NULL)
  114   {
  115     return;
  116   }
  117   if(s->first->value == v)
  118   {  
  119     Node *c = s->first->next;
  120     s->first = c;
  121     free(s->first);
  122   }
  123   Node *c = s->first;
  124   while(c->next != NULL)
  125   {
  126     if(c->next->value == v)
  127     {
  128       Node *next = c->next;
  129       c->next = c->next->next;
  130       free(next);
  131     }
  132     else{
  133       c = c->next;
  134     }
  135   }
  136 }
 

双向带头循环链表接口实现

 1 #include <stdio.h>                                                                                        
  2 #include <stdlib.h>
  3 #include <assert.h>
  4 #include <string.h>
  5 
  6 typedef struct Node
  7 {
  8   int value;
  9   struct Node *next;
 10   struct Node *prev;
 11 }Node;
 12 
 13 void DListInit(Node **p)
 14 {
 15   Node *node = (Node *)malloc(sizeof(Node));
 16   node ->next = node;
 17   node->prev = node;
 18   *p = node;
 19 }
 20 
 21 void DListPushFront(Node *node ,Node *head )
 22 {
 23   node ->next = head->next;
 24   node->prev = head;
 25   head->next->prev = node;
 26   head->next = node;
 27 }
  28 
 29 void DListPushBack(Node *node , Node *head)
 30 {
 31   node ->next = head;
 32   node ->prev = head->prev;
 33   head->prev->next = node ;
 34   head->prev = node;
 35 }
 36 
 37 void DListInsertAfter(Node *pos , Node *node)
 38 {
 39   node ->next = pos ->next;
 40   node->prev = pos;
 41   pos->next->prev = node;
 42   pos->next = node ;
 43 }
 44 
 45 void DListInsertBefor(Node *pos , Node *node)
 46 {
 47   node->next = pos;
 48   node->prev = pos->prev;
 49   pos->prev->next = node;
 50   pos->prev = node;
 51 }                      

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值