zoukankan      html  css  js  c++  java
  • Link List

    At first, i prepared to go through 《the introduction to algorithm》 ,however , i found some part of the book is difficult to understand; what’s more , i can’t borrow the book in the library. Then i found another book, 《algorithm in C》,which reveal most basic idea of some classical algorithm, so i decide to read this book firstly.

    example 1:josephus funciton

    #include<stdlib.h>
    #include<stdio.h>
    typedef struct node* link;
    struct node
    {
        int item;
        link next;
    };
    
    int main(int argc,char *argv[])
    {
        int i, N, M;
        scanf("%d%d",&N,&M);
        node *t = new node,*x;
        x = t;
        t->item = 1;
        t->next = t;
        for (i = 2; i <= N; i++)
        {
            x->next = new node;
            x = x->next;
            x->item = i;
            x->next = t;
        }
        while (x != x->next)
        {
            for (i = 1; i < M; i++)
            {
                x = x->next;
            }
            x->next = x->next->next;
            N--;
        }
        printf("%d
    ",x->item);
    }

    example2: reverse link list

    #include<stdlib.h>
    #include<stdio.h>
    typedef struct node* link;
    struct node
    {
        int item;
        link next;
        node()
        {
            next = NULL;
        }
    };
    
    link reverse(link x)
    {
        /*
        将y后节点的指针保存在t中,然后将y的链接指向r,再使r移到y,y移到t
        */
        link t, y = x, r = NULL;
        while (y != NULL)
        {
            t = y->next;
            y->next = r;
            r = y;
            y = t;
        }
        return r;
    }
    
    int main(int argc,char *argv[])
    {
        int i, N, M;
        scanf("%d%d",&N,&M);
        node *t = new node,*x,*head;
        x = t;
        head = t;
        t->item = 1;
        t->next = t;
        for (i = 2; i <= N; i++)
        {
            x->next = new node;
            x = x->next;
            x->item = i;
        }
        node *newhead = reverse(head);
        while (newhead != NULL)
        {
            printf("%d ",newhead->item);
            newhead = newhead->next;
        }
    }
  • 相关阅读:
    一次友情协助的渗透测试
    jQuery---微博发布案例
    jQuery---清空节点和删除节点
    jQuery---城市选择案例
    jQuery---创建和添加节点
    jQuery---动态创建节点
    jQuery---音乐导航
    jQuery---停止动画详解 stop();
    jQuery---手风琴案例+stop的使用(解决动画队列的问题)
    jQuery---自定义动画 animate();
  • 原文地址:https://www.cnblogs.com/maverick-fu/p/3994605.html
Copyright © 2011-2022 走看看