您的位置:首页 > 其它

简单单向list的实现

2015-03-30 17:25 218 查看
#ifndef SIGLIST_H

#define SIGLIST_H

#include<iostream>

typedef int DataType;

class Node{

public:

DataType Info;

Node * Next;

public:

//Node() :Info(0), Next(0){}

Node(DataType d = 0, Node * n = 0) :Info(d), Next(n){}//设置Node构造函数

};

class List

{

private:

Node *head, *tail;//list的两个节点指针head和tail

public:

List();

DataType Delete(const int i);//删除list中第i个元素

bool Insert(int i , const DataType d);//在list第i个位置之后插入一个info为d的节点

void Show()const;//显示整个list

void Add(List & B);//数组相加

void Reverse();//翻转list中的info信息

private:

void Swap(Node * n, Node * m);//交换m.n的Info

bool IsEmpty()const ;//检查list是否为空

const int Length()const;//返回list的长度

Node* Getpre(Node * M)const ;//求到list中节点*M的前一个节点并返回

};

Node* List::Getpre(Node * M)const

{

Node * p = head;

Node *
q = head->Next;

if (M == p)return head;

else{

while (q != M)//当q=M时循环停止,这样就q=M;

{

p = p->Next; q = p->Next; //p指向下一个,而q为p的下一个,这样当q=M是,p就是M的前一个节点

}

return p;

}

}

void List::Add(List & B)

{

if (IsEmpty())

{

head = B.head;

tail = B.tail;

}

else {

//tail = B.head;

Getpre(tail)->Next = B.head;//把tail的前一个节点的下一个节点设置为B的头节点,实现连接

tail = B.tail;//并把B的tail设置成相加后的list的tail

}

}

const int List::Length()const

{

int i;

Node * p = head;

if (IsEmpty())return 0;

else

{

for (i=0; p != tail; p = p->Next)

++i;

}

return i;

}

bool List::IsEmpty()const

{

if (head == tail)return true;

else return false;

}

List::List()

{

head=tail = NULL;

}

DataType List::Delete(const int i)

{

Node * p = head;

Node * q;

if (i > Length() || i < 0) return false;

else

{

if (i == 1)

{

return head->Info;

head = head->Next;

}

else

{

for (int n = 1; n < i - 1; ++n)

{

p = p->Next;

}

q = p->Next;

p->Next = q->Next;

return q->Info;//运行到return 时,程序终止。

}

}

}

bool List::Insert(int i , const DataType d)

{

Node* temp = new Node(d,0);

Node * p = head;

if (i > Length()||i < 0) return false;

else

{

if (i == 0)//表示插入在list的最前的位置,即把节点插入到head

{

if (IsEmpty())

{

head = temp;

head->Next = tail;

}

else

{

temp->Next = p;

head = temp;

}

}

else

{

for (int n = 1; n < i; ++n)

{

p = p->Next;

}

temp->Next = p->Next;

p ->Next= temp;

}

}

return true;

}

void List::Show()const

{

Node * p = head;

for (; p != tail;p=p->Next)

{

std::cout << p->Info << "\t";

}

std::cout << "\nLength is " << Length() << std::endl;

}

void List::Swap(Node * n, Node * m)//交换m.n的Info

{

DataType temp;

temp = n->Info;

n->Info = m->Info;

m->Info = temp;

}

void List::Reverse()

{

if (head->Next == tail)return;

else

{

Node * n = head;

Node * m = Getpre(tail);

for (; n!=m&&m->Next!=n;)//当n和m一样时,即两个指针指向一个节点,则交换停止,用于节点数为奇数

//当m的下一个节点就是n时,表示已经交换了全部,用于节点数为偶数

{

Swap(n, m);

n = n->Next;

m = Getpre(m);

}

}

}

#endif

#include<iostream>

#include"SigList.h"

#include<cstdlib>

#include<ctime>

using namespace std;

int main()

{

const int NUM=5;

List H,M;

int i = 0;

srand(time(0));

for (; i < NUM; ++i)

{

H.Insert(0, rand() % NUM);

}

cout << "The list is :\n";

H.Show();

std::cout << std::endl;

H.Insert(3, 99);

cout << "The list is :\n";

H.Show();

std::cout << std::endl;

cout << "The list after delete the "<<H.Delete(5)<<" elements is :\n";

H.Show();

std::cout << std::endl;

for (int i=0; i < NUM; ++i)

{

M.Insert(0, rand() % NUM);

}

cout << "The second list is :\n";

M.Show();

std::cout << std::endl;

H.Add(M);

cout << "The list H add List M is :\n";

H.Show();

std::cout << std::endl;

H.Reverse();

H.Show();

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: