zoukankan      html  css  js  c++  java
  • 链表的反转-非递归

    #include <stdio.h>

    typedef struct{

       int data;

       struct Node* next;

    }Node;

     

    Node* CreateNode(int value) {

    Node* temp = (Node*)malloc(sizeof(Node));

    temp->data = value;

    temp->next = NULL;

    return temp;

    }

    Node* head=NULL;

    void InsertNode(int value) {

    Node* temp = CreateNode(value);

    if(head==NULL) {head=temp;}

    else {

    Node* p=head;

    while(p->next) p = p->next;

    p->next=temp;

    }

    }

    void PrintLinkedList() {

    if(head==NULL) {

    printf("LinkedList is empty. ");

    } else {

    Node* temp = head;

    while(temp) {

    printf("%d => ", temp->data);

    temp=temp->next;

    }

    printf("NULL ");

    }

    }

    void RevertLinkedList() {

    if(head==NULL || head->next==NULL) return;

    Node* pre=NULL;

    Node* current=head;

    Node* next=head->next;

    while(next) {

    current->next=pre;

    pre=current;

    current=next;

    next=next->next;

    }

    current->next=pre;

    head=current;

    }

    int main()

    {

       InsertNode(1);

       InsertNode(2);

       InsertNode(3);

       InsertNode(4);

       PrintLinkedList();

       RevertLinkedList();

       PrintLinkedList();

       return 0;

    }

     

     

    1 => 2 => 3 => 4 => NULL

    4 => 3 => 2 => 1 => NULL

     

     

  • 相关阅读:
    java Jquery表单校验代码jsp页面
    IntelliJ IDEA 2016.1.1(64) 长时间激活教程
    maven 仓库
    java学习路线
    json 源码包
    centos 安装docker
    实现高并发
    将MongoDB安装成为Windows服务
    给mongodb设置密码权限
    MongoDB的win安装教程
  • 原文地址:https://www.cnblogs.com/lilideng/p/11291122.html
Copyright © 2011-2022 走看看