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"格式的字符串,即八进制格式的代码.尝试去完成这部分程序. 解答: 在这个练 ...
随机推荐
- datatables配置及数据传输
var merchant_url = "index.php?op=merchant"; var table_merchant_setting ={ "ajax" ...
- 格式化URL
//格式化url查询参数为json function formatUrl(url){ var reg=/(?:[?&]+)([^&]+)=([^&]+)/g; var data ...
- 传输层之TCP
---恢复内容开始--- 坞无尘水槛清,相思迢递隔重城. 秋阴不散霜飞晚,留得枯荷听雨声. --李商隐 上一篇中我们了解了socket编程是基于TCP或者UDP,所以我们有必要对TCP,和UDP ...
- openSuSE12.1 zypper LAMP
LAMP是由Apache MySQL PHP组成的,是在Linux下最受欢迎的软件组合之一,目前互联网上有很多网站运行在LAMP服务器上. Linux - 是富有情味的开源操作系统:Apache - ...
- 小Q书桌的下载、安装和使用
最近,无意之间,在某大牛电脑里,使用到了这款软件.感谢! 确实,挺实用和方便的,强烈推荐!!! 1. 下载 http://qdesk.qq.com/ 2. 安装 3. 使用 多么方便啊!
- 让你的Xcode8 支持 iOS7
Xcode8 发布,更新后,发现支持从iOS8开始,可是公司要求从iOS7开始,,,这和苹果相悖. 不过没关系. 跳转 www.cnblogs.com/starainDou/p/5325643.htm ...
- UVA 10047 The Monocycle (状态记录广搜)
Problem A: The Monocycle A monocycle is a cycle that runs on one wheel and the one we will be consi ...
- C# winform如何在textbox中判断输入的是字母还是数字?
1.用正规式using System.Text.RegularExpressions; string pattern = @"^\d+(\.\d)?$";if(Text1.Text ...
- hadoop错误Cannot load libsnappy.so.1 (libsnappy.so.1 cannot open shared object file No such file or directory)!
报如下错误 解决方法: 1.下载libsnappy.so.1(https://yunpan.cn/cSHRHTBJGVVX6 访问密码 c992) 2.上传到linux系统 3.安装 4.安装完成后 ...
- Cocos2d-x中获取设备语言的方法
1.cocos2dx获取设备语言的方法:CCApplication::sharedApplication()->getCurrentLanguage() 2.cocos2dx 2.1.4支持识别 ...