美文网首页
链表操作

链表操作

作者: 徐凯_xp | 来源:发表于2021-04-19 23:01 被阅读0次
#include <stdio.h>
#include <stdlib.h>

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

LinkedList insert(LinkedList head, Node *node, int index) {
    if (head == NULL) {//头指针为空
        if (index != 0) {
            return head;
        }
        head = node;
        return head;
    }
    if (index == 0) {//插入结点位置链表首位
        node->next = head;
        head = node;
        return head;
    }
    Node *current_node = head;
    int count = 0;
    while (current_node->next != NULL && count < index - 1) {
        current_node = current_node->next;
        count++;
    }
    if (count == index - 1) {
        node->next = current_node->next;
        current_node->next = node;
    }
    return head;
}

// 请在下面实现输出函数 output
void output(LinkedList head){
    if(head == NULL){
        return ;
    }
    Node *current_node = head;
    while(current_node != NULL){
        printf("%d ",current_node->data);
        current_node = current_node->next;
    }
    printf("\n");
}

void clear(LinkedList head) {
    Node *current_node = head;
    while (current_node != NULL) {
        Node *delete_node = current_node;
        current_node = current_node->next;
        free(delete_node);
    }
}

int main() {
    LinkedList linkedlist = NULL;
    for (int i = 1; i <= 10; i++) {
        Node *node = (Node *)malloc(sizeof(Node));
        node->data = i;
        node->next = NULL;
        linkedlist = insert(linkedlist, node, i - 1);
    }
    output(linkedlist);
    
    clear(linkedlist);
    return 0;
}


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

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

LinkedList insert(LinkedList head, Node *node, int index) {
    if(head == NULL){
        if(index != 0){
            printf("failed\n");
            return head;
        }
        head = node;
        printf("success\n");
        return head;
        
    }
    if(index == 0){
        node->next = head;
        head = node;
        printf("success\n");
        return head;
    }
    Node *current_node = head;
    int count = 0;
    while(current_node->next != NULL && count < index - 1){
        current_node = current_node->next;
        count++;
    }
    if(count == index -1 ){
        node->next = current_node->next;
        current_node->next = node;
        printf("success\n");
        return head;
    }
    printf("failed\n"); 
    return head;
}

void output(LinkedList head) {
    if(head == NULL){
        return;
    }
    Node * current_node = head;
    while(current_node != NULL){
        printf("%d",current_node->data);
        if(current_node->next != NULL){
            printf(" ");
        }
        current_node = current_node->next;
    }
    //printf("\n");
}

void clear(LinkedList head) {
    Node *current_node = head;
    while(current_node != NULL){
        Node *delete_node = current_node;
        current_node = current_node->next;
        free(delete_node);
    }

}

int main() {
    LinkedList linkedlist = NULL;
    int n;
    scanf("%d",&n);
    int p,q;
    for(int i = 0; i < n; i++){
        Node *node =(Node *)malloc(sizeof(node));
        scanf("%d %d", &p, &q);
        node->data = q;
        node->next = NULL;
        linkedlist = insert(linkedlist, node, p);
    }
    output(linkedlist);
    clear(linkedlist);
    return 0;
}
#include <stdio.h>
#include <stdlib.h>

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

LinkedList insert(LinkedList head, Node *node, int index) {
    if (head == NULL) {
        if (index != 0) {
            return head;
        }
        head = node;
        return head;
    }
    if (index == 0) {
        node->next = head;
        head = node;
        return head;
    }
    Node *current_node = head;
    int count = 0;
    while (current_node->next != NULL && count < index - 1) {
        current_node = current_node->next;
        count++;
    }
    if (count == index - 1) {
        node->next = current_node->next;
        current_node->next = node;
    }
    return head;
}

void output(LinkedList head) {
    if (head == NULL) {
        return;
    }
    Node *current_node = head;
    while (current_node != NULL) {
        printf("%d ", current_node->data);
        current_node = current_node->next;
    }
    printf("\n");
}

LinkedList delete_node(LinkedList head, int index) {
    if (head == NULL) {
        return head;
    }
    Node *current_node = head;
    int count = 0;
    if (index == 0) {
        head = head->next;
        free(current_node);
        return head;
    }
    while (current_node->next != NULL && count < index - 1) {
        current_node = current_node->next;
        count++;
    }
    if (count == index - 1 && current_node->next != NULL) {
        Node *delete_node = current_node->next;
        current_node->next = delete_node->next;
        free(delete_node);
    }
    return head;
}

// 请在下面实现链表的反转函数 reverse
LinkedList reverse(LinkedList head){
    if(head == NULL){
        return head;
    }
    Node *next_node, *current_node;
    current_node = head->next;
    head->next = NULL;
    while(current_node != NULL){
        next_node = current_node->next;
        current_node->next = head;
        head = current_node;
        current_node = next_node;
    }
    return head;
}

void clear(LinkedList head) {
    Node *current_node = head;
    while (current_node != NULL) {
        Node *delete_node = current_node;
        current_node = current_node->next;
        free(delete_node);
    }
}

int main() {
    LinkedList linkedlist = NULL;
    for (int i = 1; i <= 10; i++) {
        Node *node = (Node *)malloc(sizeof(Node));
        node->data = i;
        node->next = NULL;
        linkedlist = insert(linkedlist, node, i - 1);
    }
    output(linkedlist);
    linkedlist = delete_node(linkedlist, 3);
    output(linkedlist);
    linkedlist = reverse(linkedlist);
    output(linkedlist);
    clear(linkedlist);
    return 0;
}

相关文章

  • Java双向链表

    链表节点 链表操作

  • Java环形单向链表

    链表节点 链表操作

  • 链表

    文章结构 链表的定义 链表的插入和删除操作 链表的特性 常见的链表结构 自定义链表 链表的经典操作 使用链表实现L...

  • 线性表的链式存储-单链表

    单链表操作 [x] 单链表的创建(尾插法、头插法) [x] 单链表的查找操作 [x] 单链表的删除操作 [x] 单...

  • 重拾算法Day08-链表

    链表 本节主要讲链表,包括链表的构造,链表的插入等操作。

  • Java常用类库与技巧-集合

    一 数据结构常见问题 数组和链表的区别;链表的操作,如反转,链表环路检测,双向链表,循环链表相关操作;队列,栈的应...

  • 大话数据结构之链表(二)

    上一篇《链表概念篇》中, 主要给小伙伴们讲述了什么是链表? 为什么链表是线性结构? 链表的操作是什么? 链表操作的...

  • 链式存储结构的线性表

    单链表结构可用如下C语言代码描述 建立链表操作 读取操作 插入节点操作 删除一个节点操作 遍历操作 链式存储结构线...

  • 循环链表定义及操作

    循环链表定义 定义与单链表一样,操作时将末结点的指针指向开始结点即可 循环链表操作 初始化循环链表 插入(尾插) ...

  • C语言基础 之 链表操作

    链表的操作 对链表的主要操作有建立链表、结构的查找与输出、结点数据的删除和结点数据的插入示例 动态链表的建立 动态...

网友评论

      本文标题:链表操作

      本文链接:https://www.haomeiwen.com/subject/cjlilltx.html