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

2020-01-06 16:07:28王冬梅

SingleList.h:


#pragma once
typedef struct student
{
  int num;        // 学号
  char name[128]; // 姓名
  struct student *next;
}node;

class SingleList
{
public:
  SingleList();
  ~SingleList();

  //建立单链表 
  node *CreatNode();
  //单链表插入
  node *InsertNode(node *head, int num, char* name);
  // 计算单链表长度
  int GetLength(node *head);
  //单链表删除某个元素 
  node *DeleteNode(node *head, int num);
  //单链表逆序
  node *ReverseList(node *head);
  //打印单链表
  void PrintList(node *head);

};

关于逆序逻辑,研究了一下:

1、主要思路:

假设有单链表A->B->C->D,首先取出首节点A作为新逆序出来的链表

这样,原链表就为:B->C->D,逆序后的新链表为:A

2. 按照上述方法,依次取出B、C、D放入新链表

2、图形表示:

  原始的单链表:

  C++,单链表操作
<!--[endif]-->

初始状态时,单链表如上图所示,head指向头节点A。

1. 取出原始链表的第一个节点A,然后将该节点作为新链表的头节点

原始链表:

  C++,单链表操作
<!--[endif]-->

  新链表:

<!--[if !vml]-->  C++,单链表操作<!--[endif]-->

<!--[if !supportLists]--> 2.然后同上处理:

 原始链表:

<!--[if !vml]--> C++,单链表操作<!--[endif]-->

  新链表:

<!--[if !vml]--> C++,单链表操作<!--[endif]-->

以上这篇C++ 单链表的基本操作(详解)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持ASPKU。


注:相关教程知识阅读请移步到C++教程频道。