今天 正式开始学习数据结构的 知识 开始先写一个小链表
暑假训练也正式开始了不能再水了
我的牵挂 我的渴望 直至以后 (尴尬 尴尬 下面的都是错误的 因为自己刚开始也不懂 没有看什么书 瞎写了呢么多 唉 误导人 今天让 学长看了一下 我写的都是错误的 等看看书在更)
本来想看看 别人的博客 学习一个 简单的小程序 看了好多博客都是几百行的 看着 也看不懂 用了一大堆也不知道什么东西 还是自己看书 写最简单的 我想我的应该是 最简单的 小链表了吧 看这个 应该也通俗易懂
//链表 小练习
#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;
}