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

   对单链表的相关介绍在这里。之前因为思维不太清晰,想尝试封装一个类来实现单链表的相关操作,但是失败了。现在补上,也算是练练笔吧。

   类ListClass的成员函数与之前的各功能函数实现方法基本一致,链表头head则作为数据成员出现在了类中。此外还维护了尾结点指针tail,但是结点的插入仍然是在链表头部进行的,所以tail几乎没有用到。初始时刻,创建的空链表中,headtail都指向头结点。在单链表——封装了一个类(2)中,结点的插入在链表尾部进行,tail指针也就派上了用场。


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 emptyList();
    void reverseList();
    void sortList();
    void printList();

};

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

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

void ListClass::createList(int *rawData, int n)
{
    int i=1;
    node * tempNode;
    if(!head || !rawData){
        return ;
    }

    tempNode = new node;
    if(!tempNode){
        return;
    }
    tempNode->data = rawData[0];
    tempNode->next = NULL;
    head->next = tempNode;
    head->data ++;
    number ++;
    tail = tempNode;
    while(i<n){
        tempNode = new node;
        if(!tempNode){
            return;
        }
        tempNode->data = rawData[i];
        tempNode->next = head->next;
        head->next = tempNode;
        head->data ++;
        number ++;
        i ++;
    }
}

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

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

bool ListClass::deleteNode(int idx)
{
    node * p,*curNode;
    int length;
    if(!head){
        return false;
    }
    length = getLength();
    if(idx>=length){
        return false;
    }
    p = head;
    curNode = p->next;
    for(int i=0;i<length;i++){
        if(i==idx){
            if(idx == length-1){
                tail = p;
            }
            p->next = curNode->next;
            delete curNode;
            head->data --;
            number --;
            break;
        }
        p = curNode;
        curNode = p->next;
    }
    return true;

}

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;
    }
    tail = head;
    head->data = 0;
    number = 0;
}

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()
{
    node * p,*q;
    int length;
    int i,j;
    int e;
    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::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(6);
    list.printList();

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

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

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

    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值