数据结构------单链表(C/C++)

#include <stdbool.h>
#include "stdio.h"
#include "stdlib.h"


typedef int DataType;


// 链表节点结构体
typedef struct Node{
    DataType data;      // 数据域
    struct Node* next;  // 指针域
}Node;



void listInit(Node ** ppHead);
void listHeadInsert(Node *pHead);
void listTailInsert(Node *pNode);
bool listInsert(Node* head, int index, DataType value);
DataType listDelete(Node * head, int index);
Node* getNodeByValue(Node * head, int value);
Node* getNodeByIndex(Node * head, int index);
int length(Node * head);
void printLink(Node * pHead);


int main() {

    Node *head;
    listInit(&head);

    // 头插法插入 1~100
    // listHeadInsert(head);
    // 尾插法插入 1~100
    listTailInsert(head);

    printf("表长:%d\n", length(head));

    printf("第2个位置插入数据88\n");
    listInsert(head, 2, 88);
    printf("第2个位置数据:%d\n", getNodeByIndex(head, 2)->data);


    printf("表长:%d\n", length(head));

    printf("删除第36个位置数据\n");
    listDelete(head, 36);


    printf("表长:%d\n", length(head));
    printLink(head);

    return 0;
}



/**
 * 尾插法建立单链表
 * @param pHead
 */
void listTailInsert(Node *pHead) {
    pHead->data = 100;      // 头结点中存入表长

    Node *temp;
    for (int i = 1; i <= 100; ++i) {
        temp = (Node *) malloc(sizeof(Node));
        temp->data = i;

        // pHead始终指向最后一个节点
        pHead->next = temp;
        pHead = temp;
    }
}

/**
 * 头插法建立单链表
 * @param pHead
 */
void listHeadInsert(Node *pHead) {
    pHead->data = 100;      // 头结点中存入表长

    Node *temp;
    for (int i = 1; i <= 100; ++i) {
        temp = (Node *) malloc(sizeof(Node));
        temp->data = i;

        // 新节点插入到头结点后面
        temp->next = pHead->next;
        pHead->next = temp;
    }
    pHead->data = 100;      // 头结点中存入表长
}


/**
 * 链表初始化, 注意此处不能直接传指针,而是应该穿指针的地址(传值&传址的区别)
 * @param ppHead
 */
void listInit(Node ** ppHead) {
    *ppHead = (Node *) malloc(sizeof(Node));
    (*ppHead)->data = -1;
    (*ppHead)->next = NULL;

}

/**
 * 输出链表
 * @param pHead
 */
void printLink(Node *pHead) {
    while (pHead->next != NULL) {
        printf("%d", pHead->next->data);
        if (pHead->next->next != NULL) {
            printf(" ===> ");
        }
        pHead = pHead->next;
    }
    printf("\n");
}


/**
 * 返回表长
 * @param seq
 * @return
 */
int length(Node * head) {
    return head->data;
}


/**
 * 按位置查找, 获取第index个Node
 * @param head
 * @param index
 * @return
 */
Node* getNodeByIndex(Node * head, int index) {
    if(index < 1 || index > head->data) {
        return NULL;
    }
    while (index-- != 0) {
        head = head->next;
    }
    return head;
}


/**
 * 根据值获取节点位置
 * @param head
 * @param value
 * @return
 */
int getByValue(Node * head, int value) {
    int i = 0;
    while (head->next != NULL) {
        i++;
        head = head->next;
        if (head->data == value) {
            return i;
        }
    }
    return -1;
}


/**
 * 插入节点
 * @param head
 * @param index
 * @param value
 * @return
 */
bool listInsert(Node* head, int index, DataType value) {
    if (index < 1 || index > head->data + 1) {
        return false;
    }
    // 修改表头中表长数据
    head->data++;

    // 获取待插入位置的前一个节点
    Node* preNode = getNodeByIndex(head, index - 1);

    Node *temp = (Node *) malloc(sizeof(Node));
    temp->data = value;

    // 插入
    temp->next = preNode->next;
    preNode->next = temp;

    return true;
}


/**
 * 删除节点
 * @param head
 * @param index
 * @return
 */
DataType listDelete(Node * head, int index) {
    if(index < 1 || index > head->data) {
        return -1;
    }
    // 修改表头中表长数据
    head->data--;

    // 获取待删除位置的前一个节点
    Node* preNode = getNodeByIndex(head, index - 1);
    Node* temp = preNode->next;
    preNode->next = temp->next;
    free(temp);     // 释放内存
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值