链表(第一次错误尝试)
2017-08-01 23:29
246 查看
今天 正式开始学习数据结构的 知识 开始先写一个小链表
暑假训练也正式开始了不能再水了
我的牵挂 我的渴望 直至以后 (尴尬 尴尬 下面的都是错误的 因为自己刚开始也不懂 没有看什么书 瞎写了呢么多 唉 误导人 今天让 学长看了一下 我写的都是错误的 等看看书在更)
本来想看看 别人的博客 学习一个 简单的小程序 看了好多博客都是几百行的 看着 也看不懂 用了一大堆也不知道什么东西 还是自己看书 写最简单的 我想我的应该是 最简单的 小链表了吧 看这个 应该也通俗易懂
// 链表实现 假的冒泡排序 看网上的 一个比一个 长 一个比一个 难以理解 自己 写的 一个 很菜 只是交换了 其中的 值 并没交换他们的地址 代码如下
输入 9 6 3
输出 3 6 9
以下为正确 不是标准的 创建的链表 唉
using namespace std;
struct ac{
int x;
struct ac *next;
}a;
int main(){
int j;
ac *p,*head,*p1,*p2,*b,*s,*p3;
head=&a;
p=head;
for(j=0;j<5;j++){
b=new ac;
p->next=b;
p=p->next;
cin>>p->x;
}
p->next=NULL;
p=head;
while(p->next!=NULL){
cout<<p->next->x<<" ";
p=p->next;
}
cout<<endl;
p=head;
for(j=0;j<4;j++){
int mi=999;
for(p1=head;p1->next!=NULL;p1=p1->next){
if(p1->next->x<mi){
mi=p1->next->x;
s=p1;
}
}
p2=head->next;
p3=s->next->next;
head->next=s->next;
s->next->next=p2->next;
s->next=p2;
p2->next=p3;
head=head->next;
}
while(p->next!=NULL){
cout<<p->next->x<<" ";
p=p->next;
}
return 0;
}
暑假训练也正式开始了不能再水了
我的牵挂 我的渴望 直至以后 (尴尬 尴尬 下面的都是错误的 因为自己刚开始也不懂 没有看什么书 瞎写了呢么多 唉 误导人 今天让 学长看了一下 我写的都是错误的 等看看书在更)
本来想看看 别人的博客 学习一个 简单的小程序 看了好多博客都是几百行的 看着 也看不懂 用了一大堆也不知道什么东西 还是自己看书 写最简单的 我想我的应该是 最简单的 小链表了吧 看这个 应该也通俗易懂
//链表 小练习 #include<bits/stdc++.h> using namespace std; struct student{ int x; student *next; ///结构体指针 }; int main(){ int l=0 ; student a[10],*p,*b,*head; a[0].x=0; for(int j=1;j<10;j++){ cin>>a[j].x; a[j-1].next=&a[j]; ///将链表完整的串联起来 } a[9].next=NULL; p=&a[0]; head=&a[0]; b=new student; a[0].next=b; ///在第二个位置增加新元素 b->x=10;b->next=&a[1]; a[7].next=&a[9]; ///删除a[8]这个元素 while(p!=NULL){ ///遍历链表; cout<<p->x<<" "; p=p->next; } cout<<endl; p=head; a[9].next=head; //cout<<a[9].next<<endl; while(p!=NULL){ ///循环链表 cout<<p->x<<" "; if(l%10==0) cout<<endl; p=p->next; if(l==100) break; l++; } return 0; }
// 链表实现 假的冒泡排序 看网上的 一个比一个 长 一个比一个 难以理解 自己 写的 一个 很菜 只是交换了 其中的 值 并没交换他们的地址 代码如下
//链表实现冒泡排序 #include<bits/stdc++.h> using namespace std; struct stu{ int x; struct stu *next; }a[3]; int main(){ cin>>a[0].x>>a[1].x>>a[2].x; stu *p,*head,*p1,*p2; int y; head=&a[0]; p=head; a[0].next=&a[1]; //串联 a[1].next=&a[2]; a[2].next=NULL; for(p1=p;p1!=NULL;p1=p1->next){ //将参数换位地址 for(p2=p;p2->next!=NULL;p2=p2->next){ if((p2->x)>(p2->next->x)){ y=p2->x; //冒泡实现 交换 p2->x=p2->next->x; p2->next->x=y; } } } p=head; while(p!=NULL){ cout<<p->x<<endl; p=p->next; } }
输入 9 6 3
输出 3 6 9
以下为正确 不是标准的 创建的链表 唉
#include<bits/stdc++.h> using namespace std; struct stu{ int x; struct stu *next; }; int main(){ stu *b,*p,*head,*a,*p1,*p2; p=NULL; head=NULL; for(int j=1;j<10;j++){ b=new stu; cin>>b->x; // a=b; if(head==NULL){ //标记首地址 head=b; p=b; } else{ p->next=b; //是新创建的 链表穿起来 p=b; } } p->next=NULL; a=head; while(a!=NULL){ cout<<a->x<<" "; a=a->next; } p=head; for(p1=p;p1!=NULL;p1=p1->next){ //将参数换位地址 for(p2=p;p2->next!=NULL;p2=p2->next){ if((p2->x)>(p2->next->x)){ int y=p2->x; //冒泡实现 交换 p2->x=p2->next->x; p2->next->x=y; } } } cout<<endl; a=head; while(a!=NULL){ cout<<a->x<<" "; a=a->next; } }链表实现 选择排序 5 个数#include<bits/stdc++.h>
using namespace std;
struct ac{
int x;
struct ac *next;
}a;
int main(){
int j;
ac *p,*head,*p1,*p2,*b,*s,*p3;
head=&a;
p=head;
for(j=0;j<5;j++){
b=new ac;
p->next=b;
p=p->next;
cin>>p->x;
}
p->next=NULL;
p=head;
while(p->next!=NULL){
cout<<p->next->x<<" ";
p=p->next;
}
cout<<endl;
p=head;
for(j=0;j<4;j++){
int mi=999;
for(p1=head;p1->next!=NULL;p1=p1->next){
if(p1->next->x<mi){
mi=p1->next->x;
s=p1;
}
}
p2=head->next;
p3=s->next->next;
head->next=s->next;
s->next->next=p2->next;
s->next=p2;
p2->next=p3;
head=head->next;
}
while(p->next!=NULL){
cout<<p->next->x<<" ";
p=p->next;
}
return 0;
}
相关文章推荐
- 在ubuntu上第一次导入pylab出现错误解决方法
- 第一次认真画用例图,错误很多,请大家来指点
- 第一次用matlab的funtool出现error in的错误提示
- 第一次尝试
- C#控件开发学习问题一:安全透明方法……尝试访问安全关键方法……错误
- 尝试的基于 Windows Server 2003 计算机上运行 SQL Server 2005 实例上安装 UDDI 服务数据库组件时可能收到错误信息
- C#控件开发学习问题一:安全透明方法……尝试访问安全关键方法……错误
- 异步串口通讯第一次接收数据错误
- 新建asp.net web应用程序出现这样的错误,"尝试创建Web项目或打开位于URL"http://localhost/WebApplication1"的Web项目时,Web服务器服告了以下错误."http/1.1 500 Server Error""
- 当您尝试查看 Web 页作为宿主的 IIS 6.0 上时出现错误消息: 页面无法显示"
- 新手第一次使用python爬取豆瓣电影top250遇到的错误
- L2TP连接尝试失败,因为安全层在初始化与远程计算机的协商时遇到了一个处理错误
- MFC在鼠标自定义的一定的范围内实现拖动窗口移动、错误:不支持尝试执行的操作、实现进度条颜色的渐变或者背景和前景色的改变
- 【weex】第一次尝试,weex windows下的环境搭建到第一个完整的app
- 在Exchange Server 2007中的Outlook Web Access中尝试向电子邮件附加大文件时可能会收到错误消息
- 第一次尝试Crack(逆向异或算法)
- 今天第一次写项目过程中遇到的错误
- Android Vector的第一次尝试