zoukankan      html  css  js  c++  java
  • 【计蒜课】【数据结构】【链表的创建、插入、遍历操作的复习】

    #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 ");
    return head;
    }
    head = node;
    printf("success ");
    return head;
    }
    if (index == 0) {
    node->next = head;
    head = node;
    printf("success ");
    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 ");
    }
    if(count != index-1){
    printf("failed ");
    }

    return head;
    }

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

    }

    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);
    while(n>=1 && n<=100){
    int p;
    int q;
    scanf("%d %d",&p,&q);
    Node *node=(Node*)malloc(sizeof(Node));
    node->data=q;
    node->next=NULL;
    linkedlist=insert(linkedlist,node,p);
    n--;
    }
    output(linkedlist);
    clear(linkedlist);
    return 0;
    }

  • 相关阅读:
    python实现求解列表中元素的排列和组合
    python3.7 安装dlib和face_recognition
    Python 魔法函数
    python的68个内置函数
    生成器和生成器函数,推倒式
    函数名的应用,闭包,迭代器
    函数的进阶(动态参数,命名空间和作用域,函数的嵌套,gloabal和nonlocal关键字)
    python 函数
    文件操作
    字典和列表的删除问题, 深浅拷贝
  • 原文地址:https://www.cnblogs.com/P201821430045/p/11829166.html
Copyright © 2011-2022 走看看