首页 > 其他 > 详细

寻找链表中倒数第k个数

时间:2014-03-03 19:31:41      阅读:456      评论:0      收藏:0      [点我收藏+]
bubuko.com,布布扣
#include <stdio.h>
#include <stdlib.h>

typedef struct Node{
    int content;
    struct Node* next;
}LNode;

init(LNode** head){
    *head = (LNode*)malloc(sizeof(LNode));
    (*head)->content = 0;
    (*head)->next = NULL;
}

insert(LNode* head, int num){
    LNode* newNode = (LNode*)malloc(sizeof(LNode));
    newNode -> content = num;
    newNode -> next = head -> next;
    head -> next = newNode;
}

printL(LNode* head){
    head = head -> next;
    while(head != NULL){
        printf("%d ", head -> content);
        head = head -> next; 
    }
}

find(LNode* head, int n){
    int i = 0;
    LNode* temp = head;
    temp = temp -> next;
    head = head-> next;
    while(i < n){
        temp = temp -> next;
        i++;
    }
    while(temp != NULL){
        temp = temp -> next;
        head = head-> next;
    }

    printf("%d", head -> content);
}

main(){
    LNode* head;
    init(&head);

    insert(head, 1);
    insert(head, 2);
    insert(head, 3);
    insert(head, 4);
    insert(head, 5);
    insert(head, 6);
    insert(head, 7);
    insert(head, 8);
    insert(head, 9);

    printL(head);
    find(head, 3);
}
bubuko.com,布布扣

寻找链表中倒数第k个数,布布扣,bubuko.com

寻找链表中倒数第k个数

原文:http://www.cnblogs.com/yutoulck/p/3577738.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!