亲宝软件园·资讯

展开

C链表操作演示 C语言链表完整操作演示

chulijun3107 人气:0

头文件:link_0505.h

/* 
链表演示 
*/ 
#ifndef __LINK_0505 
#define __LINK_0505 
typedef struct node{ 
  int num; 
  struct node* p_next; 
}node; 
typedef struct  
{ 
  node head,tail; 
}link; 
//链表的初始化函数 
void link_init(link *); 
//链表的清理函数 
void link_deinit(link *); 
//判断链表是否空的函数 
int link_empty(link *); 
//判断链表是否满的函数 
int link_full(link *); 
//统计有效数字个数的函数 
int link_size(link *); 
//在最前边插入数字的函数 
int link_add_head(link *, int ); 
//在最后边插入新的数字的函数 
int link_append(link *, int ); 
//把数字按照顺序插入到链表的函数 
int link_insert(link *, int); 
//删除最前面数字的函数 
int link_remove_head(link *); 
//删除最后一个有效数字 
int link_remove_tail(link *); 
//删除某个给定数字的函数 
int link_remove(link *, int ); 
//获得第一个有效数字的函数 
int link_get_head(link *, int *); 
//获得最后一个有效数字的函数 
int link_get_tail(link *, int *); 
//获得指定编号数字的函数 
int link_get(link *, int *, int ); 
 
#endif 

实现代码: link_0505.cpp

/* 
链表演示 
*/ 
#include "stdlib.h" 
#include "link_0505.h" 
//链表的初始化函数 
void link_init(link *p_link) 
{ 
  p_link->head.p_next = &(p_link->tail); 
} 
//链表的清理函数 
void link_deinit(link *p_link) 
{ 
  while(p_link->head.p_next != &(p_link->tail)) 
  { 
    node *p_first = &(p_link->head); 
    node *p_mid = p_first->p_next; 
    node *p_last = p_mid->p_next; 
    p_first->p_next = p_last; 
    free(p_mid); 
    p_mid = NULL; 
  } 
} 
//判断链表是否空的函数 
int link_empty(link *p_link) 
{ 
  return p_link->head.p_next == &(p_link->tail); 
} 
//判断链表是否满的函数 
int link_full(link *p_link) 
{ 
  return 0; 
} 
//统计有效数字个数的函数 
int link_size(link *p_link) 
{ 
  int cnt = 0; 
  node *p_node = NULL; 
  for (p_node = &(p_link->head);p_node != &(p_link->tail);p_node = p_node->p_next) 
  { 
    node *p_first = p_node; 
    node *p_mid = p_first->p_next; 
    node *p_last = p_mid->p_next; 
    if (p_mid != &(p_link->tail)) 
    { 
      cnt++; 
    } 
  } 
  return cnt; 
} 
//在最前边插入数字的函数 
int link_add_head(link *p_link, int num) 
{ 
  node *p_temp = (node *)malloc(sizeof(node)); 
  if (!p_temp) 
  { 
    return 0; 
  }   
 
  p_temp->num = num; 
  node *p_first = &(p_link->head); 
  node *p_mid = p_first->p_next; 
  node *p_last = p_mid->p_next; 
  p_first->p_next = p_temp; 
  p_temp->p_next = p_mid; 
 
  return 1; 
} 
//在最后边插入新的数字的函数 
int link_append(link *p_link, int num) 
{ 
  node *p_tmp = (node *)malloc(sizeof(node)); 
  node *p_node = NULL; 
  if (!p_tmp) 
  { 
    return 0; 
  } 
  p_tmp->num = num; 
  for (p_node = &(p_link->head);p_node != &(p_link->tail);p_node = p_node->p_next) 
  { 
    node *p_first = p_node; 
    node *p_mid = p_first->p_next; 
    node *p_last = p_mid->p_next; 
    if (p_mid == &(p_link->tail)) 
    { 
      p_first->p_next = p_tmp; 
      p_tmp->p_next = p_mid; 
      break; 
    } 
  } 
  return 1; 
} 
//把数字按照顺序插入到链表的函数 
int link_insert(link *p_link, int num) 
{ 
  node* p_temp = (node *)malloc(sizeof(node)); 
  node* p_node = NULL; 
  if (!p_temp) 
  { 
    return 0; 
  } 
  p_temp->num = num; 
  p_temp->p_next = NULL; 
  for (p_node = &(p_link->head);p_node != &(p_link->tail);p_node = p_node->p_next) 
  { 
    node *p_first = p_node; 
    node *p_mid = p_first->p_next; 
    node *p_last = p_mid->p_next; 
    if (p_mid == &(p_link->tail) || p_mid->num > p_temp->num) 
    { 
      p_first->p_next = p_temp; 
      p_temp->p_next = p_mid; 
      break; 
    } 
  } 
  return 0; 
} 
//删除最前面数字的函数 
int link_remove_head(link *p_link) 
{ 
  node *p_first = &(p_link->head); 
  node *p_mid = p_first->p_next; 
  node *p_last = p_mid->p_next; 
  if (p_link->head.p_next == &(p_link->tail)) 
  { 
    return 0; 
  } 
  p_first->p_next = p_last; 
  free(p_mid); 
  p_mid = NULL; 
} 
//删除最后一个有效数字 
int link_remove_tail(link *p_link) 
{ 
  node *p_node = NULL; 
  for (p_node = &(p_link->head);p_node !=&(p_link->tail);p_node = p_node->p_next) 
  { 
    node *p_first = p_node; 
    node *p_mid = p_first->p_next; 
    node *p_last = p_mid->p_next; 
    if (p_last == &(p_link->tail)) 
    { 
      p_first->p_next = p_last; 
      free(p_mid); 
      p_mid = NULL; 
      return 1; 
    } 
  } 
  return 0; 
} 
//删除某个给定数字的函数 
int link_remove(link *p_link, int num) 
{ 
  node *p_node = NULL; 
  for (p_node = &(p_link->head);p_node != &(p_link->tail);p_node = p_node->p_next) 
  { 
    node *p_first = p_node; 
    node *p_mid = p_first->p_next; 
    node *p_last = p_mid->p_next; 
    if (p_mid != &(p_link->tail) && p_mid->num == num) 
    { 
      p_first->p_next = p_last; 
      free(p_mid); 
      p_mid = NULL; 
      return 1; 
    } 
  } 
  return 0; 
} 
//获得第一个有效数字的函数 
int link_get_head(link *p_link, int *p_num) 
{ 
  if (p_link->head.p_next == &(p_link->tail)) 
  { 
    return 0; 
  } 
  node *p_first = &(p_link->head); 
  node *p_mid = p_first->p_next; 
  node *p_last = p_mid->p_next; 
  p_first->p_next = p_last; 
  *p_num = p_mid->num; 
  return 1; 
} 
//获得最后一个有效数字的函数 
int link_get_tail(link *p_link, int *p_num) 
{ 
  node *p_node = NULL; 
  for (p_node = &(p_link->head);p_node != &(p_link->tail);p_node = p_node->p_next) 
  { 
    node *p_first = p_node; 
    node *p_mid = p_first->p_next; 
    node *p_last = p_mid->p_next; 
    if (p_last == &(p_link->tail)) 
    { 
      *p_num = p_mid->num; 
      return 1; 
    } 
  } 
  return 0; 
} 
//获得指定编号数字的函数 
int link_get(link *p_link, int *p_num, int num) 
{ 
  int cnt = 0; 
  node *p_node = NULL; 
  for (p_node = &(p_link->head);p_node != &(p_link->tail);p_node = p_node->p_next) 
  { 
    node *p_first = p_node; 
    node *p_mid = p_first->p_next; 
    node *p_last = p_mid->p_next; 
    if (p_mid != &(p_link->tail) && cnt == num) 
    { 
      *p_num = p_mid->num; 
      return 1; 
    } 
    cnt++; 
  } 
  return 0; 
} 

测试函数:

/* 
 * 链表测试 
 * */ 
#include <stdio.h> 
#include "link_0505.h" 
int main() { 
  int size = 0, num = 0, val = 0; 
  link lnk = {0}; 
  link_init(&lnk); 
  link_add_head(&lnk, 30); 
  link_add_head(&lnk, 20); 
  link_append(&lnk, 90); 
  link_append(&lnk, 100); 
  link_insert(&lnk, 50); 
  link_insert(&lnk, 60); 
  link_insert(&lnk, 40); 
  link_insert(&lnk, 80); 
  link_insert(&lnk, 70); 
  size = link_size(&lnk); 
  for (num = 0;num <= size - 1;num++) { 
    link_get(&lnk, &val, num); 
    printf("%d ", val); 
  } 
  printf("\n"); 
  printf("------------------"); 
  link_remove_head(&lnk); 
  link_remove_tail(&lnk); 
  link_remove(&lnk, 70); 
  size = link_size(&lnk); 
  for (num = 0;num <= size - 1;num++) { 
    link_get(&lnk, &val, num); 
    printf("%d ", val); 
  } 
  printf("\n"); 
  link_get_head(&lnk, &val); 
  printf("最前面的数字是%d\n", val); 
  link_get_tail(&lnk, &val); 
  printf("最后面的数字是%d\n", val); 
  link_deinit(&lnk); 
  return 0; 
} 

加载全部内容

相关教程
猜你喜欢
用户评论