
时间:2015-06-28 00:00 来源:IT猫扑网|http://www.itmop.com/ 作者:网管联盟 我要评论(0)
#include <iostream>
using namespace std;
//single link
struct slink_t{
struct slink_t* next;
int data;
slink_t(): next(0), data(0){
}
slink_t(slink_t* p, int d): next(p), data(d){
}
};
/*output data of single link */
ostream& operator<< (ostream& os, const struct slink_t* p)
{
cout << &Output : &;
while (p && cout << p->data << ' ')
p = p->next;
return os;
}
/* reverse the single link */
struct slink_t* slink_reverse(struct slink_t* p)
{
struct slink_t* t = NULL, *tp = NULL;
while (p)
{
tp = t;
t = p;
p = p->next;
t->next = tp;
}
return t;
}
//double link
struct dlink_t{
struct dlink_t* next;
struct dlink_t* prev;
int data;
dlink_t(): next(0), prev(0), data(0){
}
dlink_t(dlink_t* n, dlink_t* p, int d): next(n), prev(p), data(d){
}
};
/* output data of double link */
ostream& operator<< (ostream& os, const struct dlink_t* p)
{
cout << &Output : &;
while (p && cout << p->data << ' ')
p = p->next;
return os;
}
/* reverse the double link */
struct dlink_t* dlink_reverse(struct dlink_t* p)
{
struct dlink_t* t = NULL;
while (p)
{
t = p;
p = p->next;
t->next = t->prev;
t->prev = p;
}
return t;
}
#define TEST__
#ifdef TEST__
/* test */
int main()
{
struct slink_t* sl = new slink_t(new slink_t(new slink_t(new slink_t(new slink_t(new slink_t(NULL, 6), 5), 4), 3), 2), 1);
cout << sl << endl;
sl = slink_reverse(sl);
cout << sl << endl;
struct dlink_t* dl = NULL;
struct dlink_t* h = new dlink_t(NULL, dl, 1);
dl = h;
for (int i = 2; i < 10; i++)
{
struct dlink_t* t = new dlink_t(NULL, dl, i);
dl->next = t;
dl = t;
// dl->prev->next = dl;
}
cout << h << endl;
h = dlink_reverse(h);
cout << h << endl;
cout << &Hello world& << endl;
return 0;
}
#endif
关键词标签:单链表,双链表
相关阅读 安装红帽子RedHat Linux9.0操作系统教程 Tomcat9.0如何安装_Tomcat9.0环境变量配置方法 多种操作系统NTP客户端配置 Linux操作系统修改IP Linux实现SCSI硬盘热插拔及在线识别 Linux下用CDMA modem拨号上网
热门文章
安装红帽子RedHat Linux9.0操作系统教程
Linux服务器:设计高性能网站架构-LLMP
使用Clonezilla迁移到虚拟Linux环境
Linux上的MRTG流量监控中心
Linux 双网卡绑定一个IP原理及实现
linux和windows等系统远程控制ubuntu桌面
人气排行 Linux下获取CPUID、硬盘序列号与MAC地址 dmidecode命令查看内存型号 linux tc实现ip流量限制 安装红帽子RedHat Linux9.0操作系统教程 linux下解压rar文件 lcx.exe、nc.exe、sc.exe入侵中的使用方法 Ubuntu linux 关机、重启、注销 命令 查看linux服务器硬盘IO读写负载 linux命令行浏览器的使用方法 Linux NFS服务固定端口及防火墙配置 U盘安装Ubuntu 10.04 Linux清除用户登录记录和命令历史方法
查看所有0条评论>>