菜鸟学习数据结构算法之路之链表学习

重新回顾了一下数据结构,也当做是复习吧,把代码发在这里也方便保存。

链表的头文件声明与定义

#ifndef _LINKLIST__H
#define _LINKLIST__H
#include <iostream>
using namespace std;

//第一个节点也就是头节点的下一个结点的标号为1


typedef struct Node
{
    int data;
    Node *next;
};

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

    void clearList();   //清空链表
    bool isEmpty(); //判空
    int listLength();   //链表长度
    bool getElem(int i, Node *pNode);   //获取指定节点数据
    int LocateElem(Node *pNode);    //判断数据域相同的返回节点下标
    void ListTraverse();        //遍历
    bool ListInsert(int i, Node *pNode);    //指定位置插入节点
    bool ListDelete(int i, Node *pNode);    //指定位置删除节点
    bool ListInsertHead(Node *pNode);   //头节点插入
    bool ListInsertTail(Node *pNode);   //从尾部插入
private:
    Node *head; //头节点
    int len;
};

#endif  //_LINKLIST__H

链表的各个接口实现

#include "LinkList.h"

LinkList::LinkList(){       //构造函数里初始化链表的东西
    head = new Node();
    head->data = 0;
    head->next = NULL;
    len = 0;
}
LinkList::~LinkList(){     //析构函数里清空链表,释放链表空间
    clearList();
    delete head;
    head = NULL;
}

void LinkList::clearList(){ //清空整个链表
    Node *p = head->next;
    while (p != NULL){
        Node *q = p->next;
        delete p;
        p = q;
    }
    head->next = NULL;
}
bool LinkList::isEmpty(){   //判空
    if (0 == len){
        return true;
    }
    return false;
}
int LinkList::listLength(){     //链表长度
    return len;
}
bool LinkList::ListInsertHead(Node *pNode){     //从头部插入
    Node *temp = head->next;    //先保存第一个节点的地址
    Node *newNode = new Node(); //创建新结点保存pnode
    if (newNode == NULL){
        return false;
    }
    newNode->data = pNode->data;    //数据域赋值
    newNode->next = temp;   //把第一个节点的地址给插入节点的指针域,连接
    head->next = newNode;   //头节点的指针域指向插入节点
    len++;
    return true;
}
bool LinkList::ListInsertTail(Node *pNode){
    Node *temp = head;      //头节点
    while (temp != NULL){   //循环遍历。如果跳出循环说明temp是最后一个节点
        temp = temp->next;
    }
    Node *newNode = new Node;
    newNode->data = pNode->data;    
    newNode->next = NULL;   
    temp->next = newNode; 
    len++;
    return true;
}
void LinkList::ListTraverse(){      //遍历
    Node *temp = head->next;
    while (temp != NULL){
        cout << temp->data << " ";
        temp = temp->next;
    }
}
bool LinkList::ListInsert(int i, Node *pNode){      //指定位置插入节点
    if (i<0 || i>len){
        cout << "请在正确的位置上插入数据" << endl;
        return false;
    }
    Node *p = head;
    while (p->next != NULL){
        p = p->next;
    }
    Node *newNode = new Node();
    if (newNode == NULL){
        cout << "内存申请失败" << endl;
        return false;
    }
    newNode->data = pNode->data;    //数据域赋值
    newNode->next = p->next;        //新结点的指针域指向头结点的下一个节点
    p->next = newNode;          //头结点的指针域指向新插入的节点
    len++;
    return true;
}
bool LinkList::ListDelete(int i, Node *pNode){          //指定位置删除节点
    if (i < 0 || i >= len){
        return false;
    }
    Node *p = head;                         //头结点
    for (int k = 0; k < i; k++){            //遍历到待删除节点的上一个节点
        p = p->next;
    }
    Node *q = p->next;                      //把删除节点赋值给q,p是删除节点的上一个节点
    p->next = q->next;                      //上一个节点的指针域指向待删除节点的下一个节点,隔离q连接
    pNode->data = q->data;                  //数值赋值
    delete q;                               //释放内存
    q = NULL;
    len--;
    return true;
}
bool LinkList::getElem(int i, Node *pNode){
    if (i < 0 || i >= len){
        return false;
    }
    Node *p = head;
    for (int k = 0; k <= i; k++){       //遍历到想要查询的这个节点的位置
        p = p->next;
    }
    pNode->data = p->data;
    return true;
}
int LinkList::LocateElem(Node *pNode){
    int j = 0;
    Node *p = head;
    for (int i = 0; i < len; i++){
        p = p->next;
        if (p->data == pNode->data){
            return j;
        }
        j++;
    }
    return -1;  //找不到
}

链表测试

#include "LinkList.h"

int main(){
    Node node1;
    node1.data = 3;
    Node node2;
    node2.data = 6;
    Node node3;
    node3.data = 6;
    LinkList *list = new LinkList();

    list->ListInsertHead(&node1);
    list->ListInsert(1, &node2);
    list->ListTraverse();

    cout << endl;
    int i = list->listLength();
    cout << "链表的长度是:" << i << endl;

    /*list->getElem(1, &node3);
    cout << node3.data << endl;*/
    int k = list->LocateElem(&node3);
    cout << "相同元素的下标是:" << k << endl;
    /*list->ListDelete(1, &node2);
    int j = list->listLength();
    cout << "删除后链表的长度是:" << j << endl;
    list->ListTraverse();*/

    system("pause");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值