单链表——封装了一个类(2)

   对单链表的相关介绍在这里单链表——封装了一个类(1)中实现了一种类的封装,不过其中结点的插入仍然在链表头部进行,所以tail指针除了带来一些小麻烦外,形同虚设。在此,将tail指针利用起来,让每一个结点都从链表的尾部插入,因为一开始,让tailhead都指向了头结点,所以不用考虑插入的结点是否是整个链表中的第一个结点了。


ListClass.h

#ifndef _LIST_CLASS_H_
#define _LIST_CLASS_H_

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <iostream>
#include <iomanip>
using namespace std;

typedef struct Node {
    int data;
    struct Node * next;
}node;

class ListClass{
public:
    node * head;
    node * tail;
    int number;

    ListClass();
    ~ListClass();
    void createList(int * rawData, int n);
    int getLength();
    bool insertNode(int e);
    bool deleteNode(int idx);
    void reverseList();
    void sortList();
    void emptyList();
    void printList();
};

ListClass::ListClass()
{
    head = new node;
    if(head){
        head->data = 0;
        number = 0;
        head->next = NULL;
        tail = head;
    }
}

ListClass::~ListClass()
{
    if(head){
        emptyList();
        delete head;
    }
}

void ListClass::createList(int *rawData, int n)
{
    int i;
    node * newNode;
    if(!head){
        return ;
    }
    for(i=0;i<n;i++){
        newNode = new node ;
        if(!newNode){
            return ;
        }
        newNode->data = rawData[i];
        newNode->next = NULL;
        tail->next = newNode;
        tail = newNode;
        head->data ++;
        number ++;
    }
}

int ListClass::getLength()
{
    assert(number == head->data);
    return number;
}

bool ListClass::insertNode(int e)
{
    node * newNode;
    if(!head){
        return false;
    }
    newNode = new node;
    if(!newNode){
        return false;
    }
    newNode->data = e;
    newNode->next = NULL;
    tail->next = newNode;
    tail = newNode;
    head->data ++;
    number ++;
    return true;
}

bool ListClass::deleteNode(int idx)
{
    int length;
    int i;
    node * p, * curNode;

    if(!head){
        return false;
    }
    length = getLength();
    if(idx>=length){
        return false;
    }
    p = head;
    for(i=0;i<length;i++){
        curNode = p->next;
        if(i == idx){
            if(i == length-1){
                tail = p;
            }
            p->next = curNode->next;
            curNode->next = NULL;
            delete curNode;
            head->data --;
            number --;
            break;
        }
        p = curNode;
    }

    return true;
}

void ListClass::reverseList()
{
    node *p1, *p2, *p3;

    if(!head){
        return;
    }
    if(getLength()<=1){
        return;
    }
    p1 = head->next;
    p2 = p1->next;
    p1->next = NULL;
    tail = p1;
    while(p2){
        p3 = p2->next;
        p2->next = p1;
        p1 = p2;
        p2 = p3;
    }

    head->next = p1;
}

void ListClass::sortList()
{
    int i,j;
    int length;
    int e;
    node * p, * q;

    if(!head){
        return;
    }
    length = getLength();
    if(length <= 1){
        return;
    }
    for(i=1;i<length;i++){
        p = head->next;
        q = p->next;
        for(j=length-i;j>=1;j--){
            if(p->data > q->data){
                e = p->data;
                p->data = q->data;
                q->data = e;
            }
            p = q;
            q = p->next;
        }
    }
}

void ListClass::emptyList()
{
    node * curNode;
    if(!head){
        return ;
    }
    curNode = head->next;
    while(curNode){
        head->next = curNode->next;
        curNode->next = NULL;
        delete curNode;
        curNode = head->next;
    }
    head->data = 0;
    tail = head;
}

void ListClass::printList()
{
    node *curNode;
    if(!head){
        return;
    }
    curNode = head->next;
    while(curNode){
        cout << left << setw(5) << curNode->data ;
        curNode = curNode->next;
    }
    cout << endl << "tail node data : " << tail->data << endl;
    cout << endl;
}

#endif

test.cpp

#include "ListClass.h"

int main()
{
    ListClass list;
    int rawData[] = {1, 3, 2, 5, 7, 9, 6, 0};

    list.createList(rawData, sizeof(rawData)/sizeof(int));
    list.printList();

    list.insertNode(10);
    list.printList();

    list.deleteNode(0);
    list.printList();

    list.reverseList();
    list.printList();

    list.sortList();
    list.printList();

    list.emptyList();
    list.printList();

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值