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"格式的字符串,即八进制格式的代码.尝试去完成这部分程序. 解答: 在这个练 ...
随机推荐
- 解决Scrapy抓取中文结果保存为文件时的编码问题
import json import codecs # Define your item pipelines here # # Don't forget to add your pipeline to ...
- Yii 打造带有缓存功能的AR
继承AR类 重写 findByPk方法为pk 还有afterSave afterDelete 通过对象主键缓存其属性 在insert update delete 操作时候 都会自动更新缓存还是挺方 ...
- Ubuntu下Django初体验(二)——创建工程及应用
一.工程目录详解 创建工程后得到如下目录: 1. manage.py 管理项目.创建数据库.启动服务器等.测试等. 查看子命令: python manage.py 启动服务器: python mana ...
- 【oracle】触发器简单实现
目标:实现实时备份uertest表数据至usertest_temp中,两表结构一致 解决:用oracle触发器实现同步 结果: 1.建表 -- 简单的用户表 create table USERTEST ...
- SQLServer加入域后无法远程连接
如果您更改的SQLServer的远程连接端口(默认1433),加入域后,防火墙会把自定义规则都禁用掉 所以,你得进防火墙,查看,是否防火墙关闭了,我的就是关闭了,找了半天原因
- 【JAVA - SSM】之MyBatis输出映射
MyBatis中的输出映射有两种:resultType和resultMap. 1.resultType 使用resultType进行结果映射时,只有当查询结果中有至少一列的名称和resultType指 ...
- 介绍几种大型的Oracle/SQL Server数据库免费版
我们知道,Oracle和SQL Server等大型数据库也都存在一些免费的版本,这些免费的版本已经能够满足许多中小项目的需求.对于在校大学生来说,从学习的目标上来讲,安装免费版的大型数据库也就足够用了 ...
- Ajax-$.ajax()方法详解
jquery中的ajax方法参数总是记不住,这里记录一下. 1.url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址. 2.type: 要求为String类型的参数,请求方式(p ...
- strcmp函数和strcpy函数
(一)strcmp函数 strcmp函数是比較两个字符串的大小,返回比較的结果.一般形式是: i=strcmp(字符串,字符串); 当中,字符串1.字符串2均可为字符串常量或变量:i 是用于存放比 ...
- 将动态库添加到VC程序中
应用程序使用DLL可以采用两种方式:一种是隐式链接,另一种是显式链接.在使用DLL之前首先要知道DLL中函数的结构信息.Visual C++6.0在VC\bin目录下提供了一个名为Dumpbin.ex ...