单链表基本使用

本文介绍单链表的简单使用:

  1. 单链表的创建

  1. 单链表的按位置插入

  1. 单链表的按位置删除

  1. 单链表的翻转

其他如按值删除等操作方法都是相似的,这里和代码均不没有说明和实现。

主要介绍下单链表的反转(图片来源于网络):

此处用的方法核心思想:将单链表分为两个部分,头结点和首节点为一组,后面的节点为一组。后一组的各个节点一个个往前一组移动。具体实现见代码。

当然,翻转单链表的实现方法有很多种,具体可参考下面链接:

http://c.biancheng.net/view/8105.html

linklist.h

#pragma one

#include <iostream>
using namespace std;

#define OK 1
#define FLASE -1

typedef int ElemType;

struct Node {
    ElemType data;
    Node *next;
};

typedef Node* LinkList;

void ListCreate(LinkList *L);

// insert
int ListInsert(LinkList *L, int sp, int data);

int ListLength(LinkList *L);

int ListDelete(LinkList *L, int sp);

int ListDisplay(LinkList *L);

void ListInvert(LinkList *L);

linklist.cpp

#include "linklist.h"

void ListCreate(LinkList *L)
{
    Node *head = new Node;
    head->next = NULL;
    *L = head;
}

int ListInsert(LinkList *L, int sp, int data)
{
    Node * temp = *L;
    int length = ListLength(L);
    
    if (sp < 1 || sp > length + 1) {
        cout << "space error" << endl;
        return FLASE;
    }

    Node *m_node = *L;
    for (int i = 1 ; i < sp; i++) {
        m_node = m_node->next;
    }
    
    Node *node = new Node;
        node->data = data;
    node->next = m_node->next;
    m_node->next = node;

    return OK;
}

int ListLength(LinkList *L)
{
    Node *node = *L;
    int length = 0;
    while(node->next != NULL)
    {
        length++;
        node = node->next;
    }
    return length;
}

int ListDisplay(LinkList *L)
{
    Node *node = *L;
    while (node->next != NULL) {
        cout << node->next->data << " ";
        node = node->next;
    }
    cout << endl;

    return OK;
}

int ListDelete(LinkList *L,int sp)
{
    Node *node = *L;
    for (int i = 0; i < sp -1 ; i++)
    {
        node = node->next;
    }
    node ->next = node->next->next;
    // delete node->next;

    return OK;
}

void ListInvert(LinkList *L)
{
    if (ListLength(L) < 2) {
        cout << "no need invert" << endl;
    }

    Node *p_start = (*L)->next->next;
    (*L)->next->next = NULL;
    Node *node;
    while (p_start != NULL) {
        node = p_start;
        p_start = p_start->next;
        node ->next = (*L)->next;
        (*L)->next = node;
        // p_start = p_start->next;
    }
}

int main()
{
    int TAG = 0;
    LinkList lethe = NULL;
    ListCreate(&lethe);

    TAG = ListInsert(&lethe, 1, 50);
    TAG = ListInsert(&lethe, 2, 100);
    TAG = ListInsert(&lethe, 3, 150);
    TAG = ListInsert(&lethe, 4, 200);
    TAG = ListInsert(&lethe, 4, 250);
    TAG = ListInsert(&lethe, 4, 300);
    int length = ListLength(&lethe);
    cout << "ListLength = " << length << endl;

    cout << "ListDisplay : ";
    TAG = ListDisplay(&lethe);
    
    ListDelete(&lethe, 2);
    ListDelete(&lethe, 2);

    length = ListLength(&lethe);
    cout << "ListLength = " << length << endl;
    cout << "ListDisplay : ";
    TAG = ListDisplay(&lethe);

    ListInvert(&lethe);
    cout << "ListDisplay : ";
    TAG = ListDisplay(&lethe);

    return 0;
}

执行上述程序结果如下:

ListLength = 6
ListDisplay : 50 100 150 300 250 200 
ListLength = 4
ListDisplay : 50 300 250 200 
ListDisplay : 200 250 300 50

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
单链表是一种常见的数据结构,它由一系列节点组成,每个节点包含一个数据元素和一个指向下一个节点的指针。单链表基本操作包括插入、删除和查找等操作。 1. 插入操作:在单链表中插入一个新节点,可以在表头或者表尾插入,也可以在指定位置插入。具体步骤如下: - 在表头插入:创建一个新节点,将新节点的指针指向原来的表头节点,然后将新节点设置为新的表头节点。 - 在表尾插入:创建一个新节点,将新节点的指针指向空,然后将原来的表尾节点的指针指向新节点。 - 在指定位置插入:创建一个新节点,将新节点的指针指向插入位置的下一个节点,然后将插入位置的前一个节点的指针指向新节点。 2. 删除操作:从单链表中删除一个节点,可以删除表头、表尾或者指定位置的节点。具体步骤如下: - 删除表头:将表头节点的指针指向下一个节点,然后释放原来的表头节点。 - 删除表尾:找到倒数第二个节点,将其指针指向空,然后释放原来的表尾节点。 - 删除指定位置:找到要删除节点的前一个节点,将其指针指向要删除节点的下一个节点,然后释放要删除的节点。 3. 查找操作:在单链表中查找指定元素的节点,可以从表头开始遍历整个链表,直到找到目标节点或者遍历到链表末尾。具体步骤如下: - 从表头开始,依次访问每个节点,比较节点的数据元素与目标元素是否相等。 - 如果相等,则找到目标节点;如果不相等,则继续访问下一个节点,直到链表末尾。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值