前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >链表操作

链表操作

作者头像
小飞侠xp
发布2021-04-22 15:45:37
3920
发布2021-04-22 15:45:37
举报
代码语言:javascript
复制
#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;
}
代码语言:javascript
复制
#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;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档