zoukankan      html  css  js  c++  java
  • c链表

    #include <stdio.h>

    struct node
    {
    int val;
    node* next;
    };
    typedef node* list_node;
    list_node create(int n )
    {
    list_node head , temp ,pre;
    head = new node;
    pre = head;
    while ( n -- )
    {
    temp = new node;
    scanf("%d" , &temp->val);
    pre->next = temp;
    pre = temp;
    }
    pre->next = NULL;
    return head;
    }
    int add(list_node &head,int pos,int value)
    {
    list_node temp = head;
    int count = 0 ;
    while ( temp->next != NULL)
    {
    temp =temp->next;
    count ++;

    if (count == pos )
    break;
    }
    if (temp->next == NULL)
    return -1;
    else
    {
    list_node n = new node;
    n->val = value;
    n->next = temp->next ;
    temp->next = n ;
    return 1;
    }
    }
    int cont(const list_node head)
    {
    list_node temp = head;
    int count = 0 ;
    while ( temp ->next != NULL)
    {
    temp = temp->next;
    count ++;
    }
    return count;
    }
    node* reverse( list_node temp , list_node &head )
    {
    if ( temp == NULL || temp->next == NULL)
    {
    head->next = NULL;
    head = temp;
    return head;
    }
    list_node tmp = reverse( temp->next , head);
    tmp->next = temp;
    return temp;
    }

    int del(list_node &head , int pos)
    {
    int count = 0 ;
    list_node temp = head;
    while ( temp->next != NULL)
    {
    temp = temp->next ;
    count ++;
    if ( count == pos - 1 )
    break;
    }
    if ( temp->next == NULL )
    {
    return -1;
    }
    else
    {
    if ( temp->next->next == NULL)
    temp->next = NULL;
    else
    temp->next = temp->next->next;
    return 1;
    }
    }
    int main()
    {
    list_node head = create(5);

    del(head,3);

    reverse(head,head);
    return 0 ;
    }
  • 相关阅读:
    matlab常见函数汇总
    matlab矩阵合并汇总
    matlab之光谱预处理
    matlab添加高斯噪声
    ArcMap将shp文件批量逐个导出
    hdu 1090 A+B for Input-Output Practice (II)
    c语言插入排序递归法
    c语言最大公约数(辗转相除法)递归
    c语言斐波那契数列递归法
    c语言反转字符串
  • 原文地址:https://www.cnblogs.com/lzhenf/p/2424449.html
Copyright © 2011-2022 走看看