简单单向list的实现

#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;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值