Exercise DS
#include <iostream>
using namespace std; typedef struct Node
{
Node *next;
int data;
}Node, *List; typedef struct DNode
{
DNode *prior;
DNode *next;
int data;
}DNode, *DList; void creatList(List &l)
{
l = new Node;
Node *p = l;
int data;
while ((cin>>data) && data != -)
{
Node *q= new Node;
q ->data = data;
p ->next = q;
p = p->next;
}
p ->next = nullptr;
} void printList(List &l)
{
Node *p = l->next;
while (p)
{
cout<<p->data<<" ";
p = p->next;
}
cout<<endl;
} void reversePrint(List &l)
{
Node *p =l;
if (p->next != nullptr)
reversePrint(p->next);
cout<<p->data<<" ";
} void removeMin(List &l)
{
Node *p = l->next;
int min = p->data;
Node *flag = nullptr;
while (p)
{
if (p->next ==nullptr) break ;
if (p->next->data < min)
{
min = p->next->data;
flag = p;
}
p = p->next;
} Node *q = flag ->next;
flag ->next = q->next;
free(q);
} void reverseList (List &l)
{
Node *p = l->next;
l->next = nullptr;
Node *q;
while (p)
{
q = p->next;
p->next = l->next;
l->next = p;
p = q;
}
} void selectSort(List &l)
{
Node * p = l->next;
while (p)
{
Node *q = p->next;
Node *flag = p;
while (q)
{
if (q->data < flag->data)
flag = q;
q = q->next;
}
swap (p->data,flag->data);
p = p->next;
}
} void removeMM(List &l)
{
Node* p = l->next;
Node* min = p;
Node* max = p;
while (p)
{
if (p->data > max->data)
max = p;
if (p->data < min->data)
min = p;
p = p->next;
} Node* q = l; while (q)
{
Node * s = q->next;
if ((s->data>min->data) && (s->data<max->data ))
{
q ->next = s->next;
free(s);
}
q = q->next;
}
} int length(List &l)
{
int i = ;
if (!l || !l->next)
return ;
Node *p = l->next;
while (p)
{
i++;
p = p->next;
}
return i;
} Node* Search_1st_common(List &l1,List &l2)
{
int len1 = length(l1);
int len2 = length(l2); List longList = (len1>len2)?l1:l2;
List shortList = (len1<len2)?l1:l2;
int dist = (len1>len2)?(len1-len2):(len2-len1); while (dist--) longList = longList->next; while (longList)
{
if (longList->data == shortList->data)
return longList;
else
{
longList = longList->next;
shortList = shortList->next;
}
}
return nullptr;
} void freeALL(List &l)
{
while (l->next)
{
Node* pre = l;
Node*p = pre->next; while (p->next)
{
if (p->next->data<pre->next->data)
pre = p;
p = p->next;
}
printf("%d ",pre->next->data);
Node *u = pre->next;
pre->next =u->next;
free (u);
}
free(l);
} void divideList(List &l,List &r1)
{
Node *p = l->next;
r1 = new Node;
Node *r = r1;
while (p->next)
{
Node* s = new Node;
s->data = p->next->data;
Node* t = p->next;
p ->next = t->next;
r->next = s;
r = r-> next;
free(t);
p = p->next;
}
r->next = nullptr;
} void removeCF(List &l)
{
Node *p = l->next;
while (p->next)
{
Node *q = p->next;
if (q->data == p->data)
{
p->next = q->next;
free(q);
}
else
p = p->next;
}
} void MergeList(List &l1,List &l2)
{
Node *p = l1->next;
Node *q = l2->next;
l1->next = nullptr;
Node *s;
while (p && q)
{
if (p->data<q->data)
{
s = p->next;
p->next = l1->next;
l1->next = p;
p = s;
}
else {
s = q->next;
q->next = l1->next;
l1->next = q;
q = s;
}
} if (p) q = p; while (q)
{
s = q->next;
q->next = l1->next;
l1->next = q;
q = s;
}
} void inserthead(List &l)
{
l = new Node;
l ->next =nullptr; Node *s;
int data;
while (cin>>data && data != -)
{
s = new Node;
s ->data = data;
s ->next = l->next;
l ->next = s;
}
} List getCommon(List &l1,List &l2)
{
List l3 = new Node;
Node *p = l1->next;
Node *q = l2->next;
Node *t = l3;
while (p && q)
{
if (p->data == q->data)
{
Node* s = new Node;
s->data = p->data;
t ->next = s;
t = t->next;
p = p->next;
q = q->next;
}
else if (p->data > q->data)
q = q->next;
else p = p->next;
}
t->next = nullptr;
return l3;
} void UnionList(List &l1,List &l2)
{
Node *p = l1->next;
Node *q = l2->next;
Node *s = l1, *u; while (p && q)
{
if (p->data == q->data)
{
s->next = p;
s = p;
u = q;
q = q->next;
p = p->next;
free (u);
}
else if (p->data > q->data)
{
u = q;
q = q->next;
free (u);
}
else { u = p ; p = p->next ; free (u);}
}
while (p) { u = p; p = p->next; free (u);}
while (q) { u = q; q = q->next; free (u);}
s ->next = nullptr;
free (l2);
} bool isSubseq(List &l1,List &l2)
{
Node *p = l1->next;
Node *q = l2->next; Node *s = p;
while (p && q)
{
if (p->data == q->data)
{
p = p->next;
q = q->next;
}
else
{
s = s->next;
p = s;
q = l2->next;
}
}
if (!q) return true;
else return false;
} void crtDbCirList(DList &l)
{
l = new DNode;
DNode*p = l; int data;
while (cin>>data && data != -)
{
DNode* q = new DNode;
q ->data = data;
p ->next = q;
q ->prior = p;
p = p->next;
}
p ->next = l->next;
l ->next ->prior = p;
} bool isSymmetry(DList &dl)
{
DNode *p = dl->next;
DNode *q = dl->next->prior; while ( p != q && (p->next != q->prior))
{
//printf ("fuck");
if (p->data == q->data)
{
p = p->next;
q = q->prior;
}
else return false;
} return true;
} int main()
{
/*List s;
List s2;
creatList(s);
creatList(s2);
*/
//printList(s);
//printList(s2);
//reversePrint(s);
//removeMin(s);
//reverseList(s);
//selectSort(s);
//printList(s);
//cout<<length(s)<<endl<<length(s2)<<endl;
//cout<<Search_1st_common(s, s2)->data<<endl; /*List s2;
divideList(s, s2);
printList(s);
printList(s2);
*/ //removeCF(s);
//MergeList(s,s2);
//List s3 = getCommon(s, s2);
//printList(s3); //UnionList(s, s2);
//printList(s);
//cout<<isSubseq(s, s2)<<endl; DList dl;
crtDbCirList(dl);
DNode *q = dl->next;
DNode *f = dl->next->prior; cout<<q->data<<" "<<f->prior->data<<endl; if (isSymmetry(dl)) cout<<"success"<<endl;
else cout<<"fail"<<endl; return ;
}
Exercise DS的更多相关文章
- MIT 6.828 JOS学习笔记5. Exercise 1.3
Lab 1 Exercise 3 设置一个断点在地址0x7c00处,这是boot sector被加载的位置.然后让程序继续运行直到这个断点.跟踪/boot/boot.S文件的每一条指令,同时使用boo ...
- MIT 6.828 JOS学习笔记3. Exercise 1.2
这篇博文是对Lab 1中的Exercise 2的解答~ Lab 1 Exercise 2: 使用GDB的'si'命令,去追踪ROM BIOS几条指令,并且试图去猜测,它是在做什么.但是不需要把每个细节 ...
- 《MIT 6.828 Lab 1 Exercise 7》实验报告
本实验链接:mit 6.828 lab1 Exercise 7. 题目 Exercise 7. Use QEMU and GDB to trace into the JOS kernel and st ...
- 《MIT 6.828 Lab 1 Exercise 3》实验报告
本实验的网站链接:mit 6.828 lab1 Exercise 3. 题目 Exercise 3. Take a look at the lab tools guide, especially th ...
- symmetry methods for differential equations,exercise 1.4
tex文档: \documentclass[a4paper, 12pt]{article} % Font size (can be 10pt, 11pt or 12pt) and paper size ...
- 创建Azure DS 虚拟机并附加SSD硬盘
$subscriptionName = "Windows Azure Enterprise Trial" #订阅名称 $location = "China East&qu ...
- MIT 6.828 JOS学习笔记12 Exercise 1.9
Lab 1中Exercise 9的解答报告 Exercise 1.9: 判断一下操作系统内核是从哪条指令开始初始化它的堆栈空间的,以及这个堆栈坐落在内存的哪个地方?内核是如何给它的堆栈保留一块内存空间 ...
- MIT 6.828 JOS学习笔记13 Exercise 1.10
Lab 1 Exercise 10 为了能够更好的了解在x86上的C程序调用过程的细节,我们首先找到在obj/kern/kern.asm中test_backtrace子程序的地址, 设置断点,并且探讨 ...
- MIT 6.828 JOS学习笔记11 Exercise 1.8
Exercise 1.8 我们丢弃了一小部分代码---即当我们在printf中指定输出"%o"格式的字符串,即八进制格式的代码.尝试去完成这部分程序. 解答: 在这个练 ...
随机推荐
- Android学习笔记(十四)方便实用的首选项-PreferenceActivity
突然发现已经好多天没更新博客了,最近公司项目正在进行一个大跨度的重构,又碰上有新需求,一连好多天都是很晚才到家.其实这篇博文在草稿箱里面也存了很久了,本来想着不发了,不过感觉PreferenceAct ...
- [ZETCODE]wxWidgets教程六:事件处理
本教程原文链接:http://zetcode.com/gui/wxwidgets/events/ 翻译:瓶哥 日期:2013年12月7号星期六 邮箱:414236069@qq.com 主页:http: ...
- Hadoop之hive安装过程以及运行常见问题
Hive简介 1.数据仓库工具 2.支持一种与Sql类似的语言HiveQL 3.可以看成是从Sql到MapReduce的映射器 4.提供shall.Jdbc/odbc.Thrift.Web等接口 Hi ...
- phonegap与google analytics整合
用phonegap开发的app接近尾声,需要整一个谷歌分析进去. 1.首先申请一个GA帐号,在“what would you like to track”下选择APP
- hibernate.Session简介
★→→SessionFactory (org.hibernate.SessionFactory) 包含已经编译的映射(mappings),是制造session的工厂,可能含有一些可以在各个事务(tra ...
- String类、 StringBuffer、基本数据类型对象包装类
一.概述 Sting s1 = "abc"; //s1是一个类类型变量,"abc"是一个对象. String s2 = new String(" ...
- VS2013/2012 下无法打开 源 文件“stdafx.h”的解决方法
VS2013/2012下代码一写上去保存就报错了,下方提示无法打开 源 文件“stdafx.h” 如图: 百度了一下,对于VS2010有这样的方法可以解决: 在项目属性中展开C/C++,选择常规,在附 ...
- Cas Server中各配置文件介绍
Cas Server中所有的配置文件都是放在WEB-INF目录及其子目录下的. 在WEB-INF/classes下的配置文件有: l cas-theme-default.properties:该文件 ...
- java版本 ueditor 在线编辑器 配置
上传文件大小的配置 1. ueditor\dialogs\video\video.js 搜索 file_size_limit 修改这个数值 (这是前台 flash的限制) 2. ueditor ...
- 字符集详解 ------------------------ UNICODE +UTF8
http://my.oschina.net/goldenshaw/blog?catalog=3294521 http://my.oschina.net/goldenshaw/blog?catalog= ...