数据结构上机实验源代码

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
第二组数据:链表元素为(10,20,30,40,50,60,70,80,90,100)
<6>求两个递增有序链表L1和L2中的公共元素,并以同样方式连接成链表L3。
实验测试数据基本要求:
第一组
第一个链表元素为(1,3,6,10,15,16,17,18,19,20)
第二个链表元素为(1,2,3,4,5,6,7,8,9,10,18,20,30)
count--;
delete u;
cout<<"删除成功"<<endl;
return success;
}
/******************若原链表递增,插入一个元素保持其递增的顺序***********/
error_code insert1(const elementtype x)
{node *p=head->next;
typedef struct node{elementtype data;
struct node *next;
}node;
class list{
private:node *head;
int count;
public:list() //链表的初始化
{head=new node;
head->next=NULL;
例如构建链表、以某种方式显示链表、从文件中读入一个链表、跟踪访问链表结点等。
各运算的名称较为直观,并有相应的注释,因而易于理解和实现。
读者在上机实验时,需要自己设计出所涉及到的库函数,或者将函数放在实验程序中,以方便实验程序的调试。如时间紧的话,也可到作者的网站下载以供参考(不完全相同)。
编写算法实现下列问题的求解。
cin>>n;
Dec_to_Ocx(n);
}
队列的应用
打印n行杨辉三角
实验代码:
//queue.h
class queue{
public:queue(){
count=0;
front=rear=0;}
bool empty(){
return count==0;
}
bool full(){
return count==maxlen-1;
}
bool stack::full()const
{
return count==maxlen;
}
error_code stack::gettop(elementtype &x)const
{
if(empty())return underflow;
else{
x=data[count-1];
return success;
void read_write() //逆序输出所输入的数
{
stack s;
int i;
int n,x;
cout<<"please input num int n:";
cin>>n;
for(i=1;i<=n;i++)
{
cout<<"please input a num:";
cin>>x;
s.push(x);
count--;
return success;
}
//主程序
#include<iostream.h>
enum error_code{overflow,underflow,success};
typedef int elementtype;
const int maxlen=20;
#include"stack.h"
}
}
error_code stack::push(const elementtype x)
{
if(full())return overflow;
data[count]=x;
count++;
return success;
}
error_code stack::pop()
{
if(empty())return underflow;
x分别为25,85,110和8
<5>将单链表L中的奇数项和偶数项结点分解开,并分别连成一个带头结点的单链表,然后再将这两个新链表同时输出在屏幕上,并保留原链表的显示结果,以便对照求解结果。
实验测试数据基本要求:
第一组数据:链表元素为(1,2,3,4,5,6,7,8,9,10,20,30,40,50,60)
<1>求链表中第i个结点的指针(函数),若不存在,则返回NULL。
实验测试数据基本要求:
第一组数据:链表长度n≥10,i分别为5,n,0,n+1,n+2
第二组数据:链表长度n=0,i分别为0,2
<2>在第i个结点前插入值为x的结点。
实验测试数据基本要求:
第一组数据:链表长度n≥10,x=100, i分别为5,n,n+1,0,1,n+2
第二组数据:链表长度n=0,x=100,i=5
<3>删除链表中第i个元素结点。
实验测试数据基本要求:
第一组数据:链表长度n≥10,i分别为5,n,1,n+1,0
第二组数据:链表长度n=0,i=5
<4>在一个递增有序的链表L中插入一个值为x的元素,并保持其递增有序特性。
实验测试数据基本要求:
链表元素为(10,20,30,40,50,60,70,80,90,100),
数据结构上机实验源代码
栈的应用
十进制数转换为八进制数,
逆序输出所输入的数
实验代码:
//stack.h,头文件
class stack{
public:stack();
bool empty()const;
bool full()const;
error_code gettop(elementtype &x)const;
rear=(rear+1)%maxlen;
data[rear]=x;
count++;
return success;
}
error_code serve(){
if(empty())return underflow;
front=(front+1)%maxlen;
count--;
return success;
u->data=x;
u->next=p->next;
p->next=u;
return success;
}
p=p->next;
}
node *u=new node;
u->data=x;
u->next=NULL;
p->next=u;
return success;
}
//取链表头结点的地址
node *gethead()
error_code push(const elementtype x);
error_code pop();
private:
int count;
elementtype data[maxlen];
};
stack::stack(){
count=0;
}
bool stack::empty()const
{
return count==0;
}
private:int count;
int front;
int rear;
int data[maxlen];
};
//主程序
#include<iostream.h>
enum error_code{overflow,underflow,success};
typedef int elementtype;
{return head;}
//输出链表中的所有元素
error_code print()
q.append(1);
for(i=2;i<=n;i++)
{
s1=0;
for(k=1;k<=(n-i)*2;k++)
cout<<" ";
for(j=1;j<=i-1;j++)
{
q.get_front(s2);
q.serve();
cout<<s1+s2<<" ";
q.append(s1+s2);
s1=s2;
}
error_code get_front(elementtype &x){
if(empty())return underflow;
x=data[(front+1)%maxlen];
return success;
}
error_code append(const elementtype x)
{
if(full())return overflow;
实验任务
说明1:本次实验中的链表结构均为带头结点的单链表。
说明2:为使实验程序简洁直观,下面的部分实验程序中将所需要的函数以调用库函数的形式给出,并假设将库函数放在程序文件“linklist.h”中,同时假设该库函数文件中定义了链表结构中的指针类型为link,结点类型为node,并定义了部分常用运算。
n=n/8;
}
cout<<"the ocx of the dec is:";
while(!s1.empty())
{
s1.gettop(x);
cout<<x;
s1.pop();
}
cout<<endl;
}
void main()
{int n;
//read_write();
cout<<"please input a dec:";
{
node *p=head;
int j=0;
while(j!=i-1&&p!=NULL)
{p=p->next;
j++;
}
if(i<1||i>length()){cout<<"此元素不存在"<<endl;
return arrange_error;}
node *u=p->next;
p->next=u->next;
}
while(!s.empty())
{
s.gettop(x);
cout<<x<<" ";
s.pop();
}
cout<<endl;
}
void Dec_to_Ocx(int n) //十进制转换为八进制
{
stack s1;
int mod,x;
while(n!=0)
{
mod=n%8;
s1.push(mod);
{node *p=head;
int j=0;
while(j!=i-1&&p!=NULL)
{p=p->next;
j++;
}
if(i<1||i>count+1){cout<<"该位置前不能插入元素"<<endl;
return arrange_error;}
node *s=new node;
s->data=x;
第二组
第一个链表元素为(1,3,6,10,15,16,17,18,19,20)
第二个链表元素为(2,4,5,7,8,9,12,22)
第三组
第一个链表元素为()
第二个链表元素为(1,2,3,4,5,6,7,8,9,10)
实验代码:
//linklist.h
enum error_code{arrange_error,null,success};
s->next=p->next;
p->next=s;
count++;
cout<<"插入成功!"<<endl;
rewk.baidu.comurn success;
}
/***************删除第i个节点*****************************/
error_code delete_element(const int i)
}
cout<<"1 "<<endl;
q.append(1);
}
}
void main()
{int n;
cout<<"please input n:";
cin>>n;
out_number(n);
}
单链表实验
实验目的:实验目的
(1)理解线性表的链式存储结构。
(2)熟练掌握动态链表结构及有关算法的设计。
(3)根据具体问题的需要,设计出合理的表示数据的链表结构,并设计相关算法。
const int maxlen=20;
#include"queue.h"
void out_number(int n) //打印前n行的杨辉三角
{
int s1,s2;
int i;
int j;
int k;
queue q;
for(i=1;i<=(n-1)*2;i++)
cout<<" ";
cout<<"1 "<<endl;
while(p!=NULL)
{if(j==i)return p;
p=p->next;
j++;}
return NULL;
}
/***************在第i节点之前插入数为x的节点****************/
error_code insert(const int i,const elementtype x)
if(head->next->data>x){node *u=new node;
u->data=x;
u->next=head->next;
head->next=u;
return success;
}
while(p->next!=NULL){
if(p->next->data>x){node *u=new node;
count=0;
}
int length() //求链表的长度
{
return count;
}
/***********取链表中第i个节点的指针**********************/
node * locate(const int i)
{
node *p=head;
int j=0;
if(i<1||i>length())return NULL;
相关文档
最新文档