码迷,mamicode.com
首页 > 其他好文 > 详细

单链表反转

时间:2018-01-21 00:10:51      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:[]   nod   ever   core   head   nbsp   app   reverse   ++   

#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();
}

 

单链表反转

标签:[]   nod   ever   core   head   nbsp   app   reverse   ++   

原文地址:https://www.cnblogs.com/dosomethingyoulike/p/8322213.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!