数据结构上机实验源代码
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:";
}
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);
<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
s->next=p->next;
p->next=s;
count++;
cout<<"插入成功!"<<endl;
return success;
}
/***************删除第i个节点*****************************/
error_code delete_element(const int i)
实验任务
说明1:本次实验中的链表结构均为带头结点的单链表。
说明2:为使实验程序简洁直观,下面的部分实验程序中将所需要的函数以调用库函数的形式给出,并假设将库函数放在程序文件“linklist.h”中,同时假设该库函数文件中定义了链表结构中的指针类型为link,结点类型为node,并定义了部分常用运算。
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;
{return head;}
//输出链表中的所有元素
error_code print()
}
}
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;
数据结构上机实验源代码
栈的应用
十进制数转换为八进制数,
逆序输出所输入的数
实验代码:
//stack.h,头文件
class stack{
public:stack();
bool empty()const;
bool full()const;
error_code gettop(elementtype &x)const;
{ห้องสมุดไป่ตู้
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;
x分别为25,85,110和8
<5>将单链表L中的奇数项和偶数项结点分解开,并分别连成一个带头结点的单链表,然后再将这两个新链表同时输出在屏幕上,并保留原链表的显示结果,以便对照求解结果。
实验测试数据基本要求:
第一组数据:链表元素为(1,2,3,4,5,6,7,8,9,10,20,30,40,50,60)
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)
第二组数据:链表长度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),
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;
}
private:int count;
int front;
int rear;
int data[maxlen];
};
//主程序
#include<iostream.h>
enum error_code{overflow,underflow,success};
typedef int elementtype;
}
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;
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;
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;
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;
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;
}
bool stack::full()const
{
return count==maxlen;