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