实验名称: 线性表的有关操作 |
|
实验室名称: |
实验台号: |
学生姓名: |
专业班级: |
指导教师: |
实验日期:2017-6-8 |
一、实验目的
1、掌握单向链表的存储特点及其实现。
2、理解和掌握单链表的类型定义方法和结点生成方法。
3、掌握单向链表的插入、删除算法及其应用算法的程序实现。
二、实验仪器及环境:
PC计算机;windows XP操作系统、Visual C++6.0、CodeBlocks
1、
#include
#include
#include
#include
using namespace std;
typedef struct node{
int data;
struct node *next;
}Lnode,*Linklist;
Linklist CreateFromHead(){
Linklist L;
Lnode *s;
int x,n,num;
cout<<"请输入创建随机链表总个数:";
cin>>n;num=n;
L=(Linklist)malloc(sizeof(Lnode));
L->next=NULL;
srand((unsigned)time(NULL));
while(num>0){
x=rand()%100+1;
//printf("请输入第%d个数据元素:",n-num+1);
//cin>>x;
s=(Linklist)malloc(sizeof(Lnode));
s->data=x;
s->next=L->next;
L->next=s;
num--;
}
return L;
}
Linklist CreateFromHead_(){
Linklist L;
Lnode *s;
int x,n,num;
cout<<"请输入创建链表总个数:";
cin>>n;num=n;
L=(Linklist)malloc(sizeof(Lnode));
L->next=NULL;
srand((unsigned)time(NULL));
while(num>0){
//x=rand()%100+1;
//printf("请输入第%d个数据元素:",n-num+1);
cin>>x;
s=(Linklist)malloc(sizeof(Lnode));
s->data=x;
s->next=L->next;
L->next=s;
num--;
}
return L;
}
Linklist Daozhi(Linklist head){
Linklist p=NULL,q=NULL,t=NULL;
p=head;t=head;p=p->next;
while(p->next!=NULL){
q=p;
p=p->next;
q->next=t;
t=q;
}
head->next->next=NULL;
head->next=p;
p->next=q;
return head;
}
void Show(Linklist Head){
Linklist p=NULL;
p=Head;
while(p->next!=NULL){
p=p->next;
cout<
}
cout< } Linklist Shanchu(Linklist head){ Linklist p=NULL,q=NULL; p=head; while(p->next!=NULL){ q=p; p=p->next; if(p->data%2==0){ q->next=q->next->next; free(p); p=q; } } return head; } Linklist Charu(Linklist head,int x){ Linklist p=NULL,q=NULL,s; int flag=1; p=head; s=(Linklist)malloc(sizeof(Lnode)); s->data=x; while(p->next!=NULL){ q=p; p=p->next; if(p->data>=x){ s->next=p; q->next=s; flag=0;break; } } if(flag) {p->next=s;s->next=NULL;} return head; } Linklist Sort(Linklist head) { Linklist temp1,temp2,q,p; for(q=head;q->next!=NULL;q=q->next) for(p=q->next;p->next!=NULL;p=p->next) { if(p->next->data if(q->next==p) { temp1=p->next; p->next=p->next->next; temp1->next=q->next; q->next=temp1; p=temp1; } else { temp1=p->next; temp2=q->next; p->next=p->next->next; q->next=q->next->next; temp1->next=q->next; q->next=temp1; temp2->next=p->next; p->next=temp2; } } return head; } Linklist Hecheng(Linklist head1,Linklist head2){ Linklist p; p=head2; while(p->next!=NULL) { p=p->next; Charu(head1,p->data); } return head1; } Linklist Sort_(Linklist head) { Linklist temp1,temp2,q,p; for(q=head;q->next!=NULL;q=q->next) for(p=q->next;p->next!=NULL;p=p->next) { if(p->next->data>q->next->data) if(q->next==p) { temp1=p->next; p->next=p->next->next; temp1->next=q->next; q->next=temp1; p=temp1; } else { temp1=p->next; temp2=q->next; p->next=p->next->next; q->next=q->next->next; temp1->next=q->next; q->next=temp1; temp2->next=p->next; p->next=temp2; } } return head; } int main() { Linklist Head=NULL,p=NULL,Head1=NULL,Head2=NULL; int x; Head=CreateFromHead(); cout<<"随机产生的链表数据如下:"< p=Head; Show(p); cout<<"*********逆置后链表*********"< p=Head; p=Daozhi(p); Show(p); cout<<"*****删除偶数元素后链表*****"< p=Head; p=Shanchu(p); Show(p); cout<<"*******非递减有序链表*******"< p=Head; Sort(p); Show(p); cout<<"请输入要插入的链表元素:"; cin>>x; cout<<"******插入新元素后链表******"< p=Head; Charu(p,x); Show(p); cout<<"请键入两个非递减有序单向链表"< Head1=CreateFromHead_(); p=Head1; Sort(p); Head2=CreateFromHead_(); p=Head2; Sort(p); cout<<"*****合并后的非递减序列*****"< p=Hecheng(Head1,Head2); Show(p); cout<<"*****合并后的非递增序列*****"< p=Daozhi(p); Show(p); return 0; } 1、 2、 3、 4、 5、 6、 7、 8、 创建链表时有头,尾两种创建方法,两种方法遍历数据元素时和创建的次序有所不同。 签名: 年 月 日四、实验心得体会:(包括遇到的问题及解决办法)
五、指导教师意见及成绩