简单单向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;
}
#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;
}
相关文章推荐
- C语言实现一个简单的单向链表list
- 转载的标准文档:C语言实现一个简单的单向链表list
- C语言实现一个简单的单向链表list
- 利用反射 和 comparator 实现List 的简单排序
- 用c++实现简单的单向链表
- Tomcat简单配置实现SSL单向认证
- 最简单的对Java List列表按中文拼音排序的实现方式
- list.c - A linked list by C --- C语言实现的单向链表
- C++通过list和template实现简单的对象池
- C++通过list和template实现简单的对象池
- 左右list javascript简单实现
- list.c - A linked list by C --- C语言实现的单向链表
- 使用list实现一个简单的Listener管理
- c++stl库list简单的实现
- JS Map 和 List 的简单实现。
- 用List实现的Fibonacci 觉得超级简单,请高手指点
- c#简单实现二维数组和二维数组列表List<>的转置
- 基于指针来实现的采用节点的简单list
- 单向链表的简单实现
- 自定义ORM系列(一)利用attribute实现简单的reader=>entity和reader=>List<entity>映射