单链表反转

2018-06-17 21:21:52来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

#ifndef MYLIST_H
#define MYLIST_H
#include <stdio.h>
class Node
{
public:
    Node(int v){value = v;next=NULL;}
    int value;
    Node * next;
};

class List
{
public:
    List(){
        head = tail = NULL;
    }
    void addNode(Node *node){
        if(head==NULL){
            head = node;
        }
        if(tail==NULL){
            tail = node;
        }else{
            tail->next = node;
            tail = node;
        }
    }
    void ReverseList(List * srcList){
        Node * p = srcList->head;
        Node * t = NULL;
        Node * q = NULL;
        while(p){
            t = p;
            p = p->next;
            t->next = q;
            q = t;
        }
        srcList->head = q;
    }
    void printList(){
        Node * p = head;
        while(p){
            printf("value:%d ",p->value);
            p = p->next;
        }
    }

public:
    Node * head,*tail;
};

#endif // MYLIST_H
#include <QCoreApplication>
#include "mylist.h"
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    List * myList = new List();
    for(int i=1;i<=5;i++){
        Node * node = new Node(i);
        myList->addNode(node);
    }
    myList->ReverseList(myList);
    myList->printList();

    return a.exec();
}

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:2017 ICPC区域赛(西安站)--- J题 LOL(DP)

下一篇:C++雾中风景6:拷贝构造函数与赋值函数