• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >C语言 > C++ 双链表的基本操作(详解)

C++ 双链表的基本操作(详解)

作者: 字体:[增加 减小] 来源:互联网 时间:2017-05-28

通过本文主要向大家介绍了c++链表排序,链表c++,c++链表的创建,c++链表类,c++链表教程等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

1.概念

双向链表也叫双链表,是链表的一种,它的每个数据结点中都有两个指针,分别指向直接后继和直接前驱。所以,从双向链表中的任意一个结点开始,都可以很方便地访问它的前驱结点和后继结点。一般我们都构造双向循环链表。

结构图如下所示:

  

  

2.基本操作实例

DoubleList.cpp

#include "stdafx.h"
#include "DoubleList.h"
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
DoubleList::DoubleList()
{
    pDoubleListNode pDouList = NULL;
    // 创建双链表
    CreateDouList(pDouList);
    PrintDouList(pDouList);
    // 打印逆序链表
    PrintDouReverseList(pDouList);
    // 节点后插入节点
    InsertNodeAfter(pDouList);
    PrintDouList(pDouList);
    // 节点前插入节点
    InsertNodeBefore(pDouList);
    PrintDouList(pDouList);
    // 删除节点
    DeleteNode(pDouList);
    PrintDouList(pDouList);
    // 删除链表
    DeleteDouList(pDouList);
    PrintDouList(pDouList);
    system("PAUSE");
}
DoubleList::~DoubleList()
{
}
//创建双向链表
void DoubleList::CreateDouList(pDoubleListNode &head)
{
  char x;     // 定义成char型是用于输入'q'时可以退出,其实定义成int也能退出
  pDoubleListNode p, s;
  head = (pDoubleListNode)malloc(sizeof(DoubleListNode));
  head->next = NULL;
  head->prior = NULL;    // 构造头结点p
  p = head;
  printf("\n输入双向链表的元素,每输入一个元素后按回车,输入q表示结束.\n");
  fflush(stdin);  //清空输入缓冲区
  x = getchar();
  while (x != 'q')
  {
    s = (pDoubleListNode)malloc(sizeof(DoubleListNode));
    s->data = x - '0'; // 得到的是输入字符的ASCII码,减去30H就变成想要的数字
    s->next = NULL;
    s->prior = p;
    p->next = s;
    p = s;
    fflush(stdin);
    x = getchar();
  }
  if (x == 'q')
  {
    printf("双向链表构造完毕!\n");
  }
}
//打印双向链表
void DoubleList::PrintDouList(pDoubleListNode &head)
{
  pDoubleListNode p;
  printf("\n打印出双向链表数据为:\n");
  if (!IsDouListEmpty(head))
  {
    p = head->next;
    while (p)
    {
      printf("%d\n", p->data);
      p = p->next;
    }
  }
}
//逆序打印双向链表
void DoubleList::PrintDouReverseList(pDoubleListNode &head)
{
  pDoubleListNode p;
  printf("\n打印出逆序双向链表数据为:\n");
  if (!IsDouListEmpty(head))
  {
    p = head->next;
    while (p->next)
    {
      p = p->next;
    }
    while (p->prior)
    {
      printf("%d \n", p->data);
      p = p->prior;
    }
  }
}
//求链表长度
int DoubleList::GetDouListLength(pDoubleListNode &head)
{
  int length = 0;
  if (head == NULL)
  {
    printf("链表不存在,请先初始化!\n");
  }
  else
  {
    pDoubleListNode p = head->next;
    while (p)
    {
      length++;
      p = p->next;
    }
  }
  return length;
}
//判断链表是否为空
bool DoubleList::IsDouListEmpty(pDoubleListNode &head)
{
  if (head == NULL)
  {
    printf("链表不存在,请先初始化!\n");
    return true;
  }
  else if (head->next == NULL)
  {
    printf("链表为空!\n");
    return true;
  }
  
  return false;
}
//把双向链表置空
void DoubleList::ClearDouList(pDoubleListNode &head)
{
  if (head == NULL)
  {
    printf("链表不存在,请先初始化!\n");
  }
  else
  {
    pDoubleListNode p, q;
    p = q = head->next;  //是p、q指向第一个元素
    head->next = NULL;
    while (p)     //逐个释放元素所占内存
    {
      p = p->next;
      free(q);
      q = p;
    }
  }
}
// 删除双向链表
void DoubleList::DeleteDouList(pDoubleListNode &head)
{
  printf("\n删除双向链表\n");
  ClearDouList(head);
  free(head);
  head = NULL;
}
// 在双向链表中第i个位置后面插入元素
void DoubleList::InsertNodeAfter(pDoubleListNode &head)
{
  int data, pos;
  pDoubleListNode p, s;
  p = head;
  int i = 0;
  printf("\n在双向链表中第i个位置后面插入元素\n");
  printf("请输入要插入的元素和位置:\n");
  scanf_s("%d%d", &data, &pos, 100);
  if (head == NULL)
  {
    printf("链表不存在,请先初始化!\n");
  }
  else if (head->next == NULL)
  {
    printf("链表为空,插入第一个元素!\n");
    s = (pDoubleListNode)malloc(sizeof(DoubleListNode));
    s->data = data;
    s->prior = NULL;  
    s->next = NULL;
    head->next = s;    // 将新结点插入head后 
  }
  else if (pos<1 || pos>GetDouListLength(head) + 1)
  {
    printf("插入位置错误!\n");
  }
  else
  {
    while (i < pos)
    {
      p = p->next;
      i++;
    }
    if (i == GetDouListLength(head))   //如果在最后一个元素后面插入data
    {
      s = (pDoubleListNode)malloc(sizeof(DoubleListNode));
      s->data = data;
      s->next = NULL;
      s->prior = p;
      p->next = s;
    }
    else
    {
      s = (pDoubleListNode)malloc(sizeof(DoubleListNode));
      s->data = data;
      s->next = p->next;
      p->next->prior = s;
      p->next = s;
      s->prior = p;
    }
  }
}
// 在双向链表中第i个位置前面插入元素
void DoubleList::InsertNodeBefore(pDoubleListNode &head)
{
  int data, pos;
  pDoubleListNode p, s;
  p = head;
  int i = 0;
  printf("\n在双向链表中第i个位置前面插入元素\n");
  printf("请输入要插入的元素和位置:\n");
  scanf_s("%d%d", &data, &pos, 100);
  if (head == NULL)
  {
    printf("链表不存在,请先初始化!\n");
  }
  else if (head->next == NULL)
  {
    printf("链表为空,插入第一个元素!\n");
    s = (pDoubleListNode)malloc(sizeof(DoubleListNode));
    s->data = data;
    s->prior = NULL;
    s->next = NULL;
    head->next = s;    // 将新结点插入head后 
  }
  else if (pos<1 || pos>GetDouListLength(head) + 1)
  {
    printf("插入位置错误!\n");
  }
  else
  {
    while (i < pos)
    {
      p = p->next;
      i++;
    }
    if (i == 1)   // 如果在第一个元素前面插入data
    {
      s = (pDoubleListNode)malloc(sizeof(DoubleListNode));
      s->data = data;
      head->next = s;    // 将新结点插入head后 
      s->prior = head;    // 新结点的前结点指向头结点 
      s->next = p;            // 新结点的后结点指向原head的后结点 
      p->prior = s ;          // 原第一个结点的前结点指向新结点 
    }
    else
    {
      s = (pDoubleListNode)malloc(sizeof(DoubleListNode));
      s->data = data;
      s->prior = p->prior;
      s->next = p;
      p->prior->next = s;
      p->prior = s;
    }
  }
}
//删除双向链表中的第i个元素
void DoubleList::DeleteNode(pDoubleListNode &head)
{
  int pos;
  int i = 0;
  pDoubleListNode p = head;
  printf("\n在双向链表中删除第i个位置的元素\n");
  printf("请输入要删除的位置:");
  scanf_s("%d", &pos, 100);
  
  if (IsDouListEmpty(head))
  {
    return;
  }
  else if (pos<1 || pos>GetDouListLength(head))
  {
    printf("删除的位置不存在!\n");
  }
  else
  {
    while (i < pos)
    {
      p = p->next;
      i++;
    }
    if (i == GetDouListLength(head))
    {
      p->prior->next = NULL;
      free(p);
    }
    else
    {
      p->prior->next = p->next;
      p->next->prior = p->prior;
      free(p);
    }
  }
}
</div>

DoubleList.h

#pragma once
typedef struct DoubleListNode
{
  int data;       //数据
  struct DoubleListNode *prior; //前驱
  struct DoubleListNode *next; //后继
}DoubleListNode, *pDoubleListNode;
class DoubleList
{
public:
  DoubleList();
  ~DoubleList();
  //初始化双向链表
  void DoubleList::CreateDouList(pDoubleListNode &head);
  //打印双向链表
  void DoubleList::PrintDouList(pDoubleListNode &head);
  //逆序打印双向链表



 
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • C++删除链表中间节点的方法
  • C++ 单链表的基本操作(详解)
  • C++ 双链表的基本操作(详解)
  • c++双向链表操作示例(创建双向链、双向链表中查找数据、插入数据等)

相关文章

  • 2017-05-28用C++实现DBSCAN聚类算法
  • 2017-05-28详谈c++11 final与override说明符
  • 2017-05-28详细对比C语言中的chmod()函数和fchmod()函数
  • 2017-05-28VC判断进程是否具有administrator权限的方法
  • 2017-05-28tinyxml 常用的C++ XML解析器非常优秀
  • 2017-05-28二维指针动态分配内存连续问题深入分析
  • 2017-08-27最长公共子序列LCS C++实现
  • 2017-05-28剖析C++中的常量表达式与省略号的相关作用
  • 2017-05-28C++中的函数指针与函数对象的总结
  • 2017-05-28C++ 中lambda表达式的编译器实现原理

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • C++ COM编程之QueryInterface函数(二)
    • c语言网络编程-标准步骤(比较简单)
    • 详解C语言中的#define宏定义命令用法
    • 谈谈C++学习之Pair的使用方法
    • C++面试题之数a、b的值互换(不使用中间变量)
    • C语言指针数组(数组每个元素都是指针)详解
    • 实例详解C/C++中extern关键字
    • C语言辗转相除法求2个数的最小公约数
    • 最小生成树算法之Prim算法
    • c语言中用字符串数组显示菜单的解决方法

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有