上机作业111

目录


【线性表】

建立一个静态顺序表(正序),并输出该线性表。

提示:完整的程序包括程序前缀、静态顺序表的结构体类型定义、初始化子函数、表尾插入新元素子函数、输出子函数、主函数等。

#include<iostream>
#include<stdio.h>
using namespace std;

#define LIST_INIT_SIZE 100


//结构体类型定义
struct StaticList {
   
	int elem[LIST_INIT_SIZE]; //表  
	int length; //表长 
	int listsize; //表有可能的最大长度 
} ;


//初始化
void InitList_StaticList(StaticList &L) {
   
	L.length=0;
	L.listsize=LIST_INIT_SIZE;
}


//表尾插入新元素
int Ins_SList(StaticList &L,int x) {
   
	if(L.length>=L.listsize) {
   
		printf ("The list is overflow ! \n");
		return -1;
	} 
	else {
   
		L.elem[L.length]=x;
		L.length++;
		return 1;
	}
}


//输出线性表
void print(StaticList &L) {
   
	for(int i=0; i<=L.length-1 ; i++)
		cout<<L.elem[i]<<" "; //输出
	cout<<endl;
}


int main(int argc,char* argv[]) {
   
	StaticList SL;
	InitList_StaticList(SL);
	cout<<"enter numbers:"<<endl;
	while(1) {
   
		int x;
		cin>>x; //输入
		if(x==9999)
			break; //当输入9999时停止输入 
		else
			Ins_SList(SL, x);
	}
	cout<<"The list elems:"<<endl ;
	print(SL);
	return 0;
}

建立一个静态顺序表(逆序),输出该线性表;再在表的中部添加一个新元素,并输出新的线性表。

提示:完整的程序包括程序前缀、静态顺序表的结构体类型定义、初始化子函数、表头插入新元素子函数、表中插入新元素子函数、输出子函数、主函数等。

#include<iostream>
using namespace std;


#define LIST_INIT_SIZE 100

//结构体类型定义
struct StaticList {
   
	int elem[LIST_INIT_SIZE];
	int length;
	int listsize;
};


//初始化
void InitList_StaticList(StaticList &L) {
   
	L.length=0;
	L.listsize=LIST_INIT_SIZE;
}


//在表头插入新元素x
int Ins_SList1(StaticList &L, int x) {
   
	//顺序表已满(没有多余空间),返回-1,表示插入失败
	if(L.length>=L.listsize) {
   
		printf("The list is overflow ! \n ");
		return -1;
	} else {
   
		for (int i=L.length-1 ; i>=0; i--) {
   
			//下标[0, length-1]的元素逐个后移
			L.elem[i+1]=L.elem[i];
		}
		L.elem[0]=x;
		L.length++;
		return 1 ;
	}
}


//在线性表中下标为i(第i+1个位置)的位置插入数据x
int Ins_SList2(StaticList &L, int i, int x) {
   
	//顺序表已满(没有多余空间),返回-1,表示插入失败
	if(L.length>=L.listsize) {
   
		printf ("The list is overflow ! \n") ;
		return -1;
	} else if (i<0 || i>L.length) {
   
		//当i=L.length时即插入位置为表的最后 
		printf ("The position is error ! \n") ;
		return 0;
	} else {
    // i>=0 && i<=L.length
		for (int j=L.length-1; j>=i; j--) {
   
			//下标[i, length-1]的元素逐个后移
			L.elem[j+1]=L.elem[j];
		}
			
		L.elem[i]=x ;
		L.length++;
		return 1 ;
	}
}


//输出
void print(StaticList &L) {
   
	for(int i=0; i<=L.length-1; i++) {
   
		cout<<L.elem[i]<<" ";
	}
	cout<<endl;
}


int main(int argc, char* argv[]) {
   
	StaticList SL;
	int num, pos;
	
	//初始化函数 
	InitList_StaticList(SL) ;
	cout<<"enter numbers:"<<endl;
	while(1) {
   
		int x;
		cin>>x;
		if(x==9999)
			break;
		else
			Ins_SList1(SL, x);
	}
	
	//输出元素 
	cout<<"The list elems:"<<endl ;
	print(SL);
	
	//输入插入元素 
	cout<<"insert a new element:"<<endl;
	cin>>num;
	
	//输入插入元素下标  
	cout<<"input the inserted position:"<<endl;
	cin>>pos;
	
	//插入元素函数 
	Ins_SList2(SL, pos-1, num);
	
	//输出最终表内元素 
	cout<<"The new list elems:"<<endl ;
	print(SL);
	return 0;
}

建立一个静态顺序表(逆序),输出该线性表;然后依次删除表尾元素、表头元素、表的中部元素,并分别输出新的线性表。

提示:完整的程序包括程序前缀、静态顺序表的结构体类型定义、初始化子函数、表头插入新元素子函数、表尾删除元素子函数、表头删除元素子函数、表中删除元素子函数、输出子函数、主函数等。

#include<iostream>
using namespace std;

#define LIST_INIT_SIZE 100

//结构体类型定义
struct StaticList {
   
	int elem[LIST_INIT_SIZE];
	int length;
	int listsize;
};


//初始化
void InitList_StaticList(StaticList &L) {
   
	L. length=0;
	L.listsize=LIST_INIT_SIZE;
}


//在表头插入新元素x
int Ins_SList1(StaticList &L,int x) {
   
	//顺序表已满(没有多余空间),返回-1,表示插入失败
	if(L.length>=L.listsize) {
   
		printf("The list is overflow ! \n ");
		return -1;
	} else
		for (int i=L.length-1; i>=0; i--) {
   
			L.elem[i+1]=L.elem[i];//下标[0, length-1]的元素逐个后移
		}
	L.elem[0]=x;
	L.length++;
	return 1;
}


//输出
void print(StaticList &L) {
   
	for(int i=0; i<=L.length-1 ; i++) {
   
		cout<<L.elem[i]<<" ";
	}
	cout<<endl;
	cout<<endl;
}


//删除表尾元素
int Del_SList2(StaticList &L) {
   
	//顺序表为空(没有元素可删),返回-1,删除失败
	if(L.length==0) {
   
		printf("The list is empty ! \n");
			return -1 ;
	} else {
   
		L.length--;
		return 1 ;
	}
}


//删除表头元素,第1个元素,下标为0
int Del_SList1(StaticList &L) {
   
	//顺序表为空(没有元素可删),返回-1,删除失败
	if(L.length==0) {
   
		printf("The list is empty ! \n") ;
		return -1;
	} else {
    //数据由后往前移动
		for(int i=1; i<=L.length-1 ; i++) {
   
			L.elem[i-1]=L.elem[i];
		}
		L.length--;
		return 1;
	}
}


//删除表中数组下标为i的元素,第i+1个元素
int Del_SList(StaticList &L, int i) {
   
	//顺序表为空(没有元素可删),返回-1,删除失败
	if(L.length==0) {
   
		printf("The list is empty ! \n") ;
		return -1;
	} else if(i<0 || i>L.length-1) {
   
		printf("The index is error ! \n");
		return 0;
	} else {
    //i>=O && i<=L.length-1
		for(int j=i+1; j<=L.length-1; j++) {
   
			//下标[i+1, length-1]的元素逐个前移
			L.elem[j-1]=L.elem[j];
		}
		L.length--;
		return 1;
	}
}


int main(int argc, char* argv[]) {
   
	StaticList SL;
	int num, pos;
	InitList_StaticList(SL) ;
	cout<<"enter numbers:"<<endl ;
	while(1) {
   
		int x;
		cin>>x;
		if(x==9999)
			break ;
		else
			Ins_SList1 (SL,x) ;
	}
	
	cout<<"The list elems:"<<endl;
	print(SL) ;
	
	//删除表头元素
	cout<<"delete the list_head element:" <<endl ;
	Del_SList1(SL);
	cout<<"The new list elems after deleting list_head element:"<<endl ;
	print(SL);
	
	//删除表尾元素 
	cout<<"delete the list_tail element:" <<endl;
	Del_SList2(SL);
	cout<<"The new list elems after deleting list_tail element:"<<endl ;
	print(SL);
	
	//删除表中间元素
	cout<<"delete the list_middle element:" <<endl;
	//输入删除元素的下标 
	cout<<"input the deleted position:"<<endl;
	cin>>pos;
	Del_SList(SL, pos-1) ;
	cout<<"The new list elems:"<<endl ;
	print(SL);
	
	return 0;
}

建立一个动态顺序表(正序),输出该线性表;再在表的中部添加一个新元素,并输出新的线性表。

提示:完整的程序包括程序前缀、动态顺序表的结构体类型定义、初始化子函数、表头或表尾插入新元素子函数、表中插入新元素子函数、输出子函数、主函数等。

#include<iostream>
using namespace std;

typedef int ElemType;
#define LIST_INIT_SIZE 100

//结构体类型定义
typedef struct {
   
	ElemType *elem;
	int length;
	int listsize;
} SqList; //SqList是结构体类型 


//初始化,构造一个空的线性表
int InitList_Sq(SqList &L) {
   
	/*
	申请动态空间,申请 LIST_INIT_SIZE个 类型为ElemType 的空间。
	L.elem=(ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType));是C语言的格式。
	L.elem=new ElemType[LIST_INIT_SIZE];是C++的格式。
	*/
	L.elem = new ElemType[LIST_INIT_SIZE];
	
	//申请动态空间未成功 
	if (L.elem==0) {
   
		printf("failure! \n") ;
		return 0;
	} else {
   
		L.length=0;
		L.listsize=LIST_INIT_SIZE;
		return 1 ;
	}
}


//在表尾插入元素e
int ListEndInsert_Sq(SqList &L, ElemType e) {
   
	if(L.length>=L.listsize) {
   
		printf("overf1ow! \n");
		return -1;
	}
	L.elem[L.length]=e;
	L.length++;
	return 1;
}
/*
L.elem[L.length]=e;为何用数组:
只使用数组的形式访问线性表中的一个元素而已,这点和指针类似。
比如char *p="abcdef",同样可以通过p[i]来访问字符串中的字符。
*/


//在动态顺序表L的第i个位置插入元素e
int ListInsert_Sq(SqList &L, int i, ElemType e) {
   
	ElemType *p, *q;
	
	//插入位置不合法
	if(i<=0 || i>=L.length) {
   
		printf("The position error! Not inserted! \n");
		return 0;
	}
	
	//空间不足
	if(L.length>=L.listsize) {
   
		printf("overflow! \n") ;
		return -1;
	}
	
	q=&(L.elem[i-1]);//第i个元素,下标为i-1
	
	for(p=&(L.elem[L.length-1]); p>=q; --p) {
   
		*(p+1)=*p;
	}
	
	*q=e; //插入新元素
	L.length++; //表长递增
	
	return 1;
}


//输出动态线性表元素
void printSq(SqList &L) {
   
	ElemType *p;
	for (p=&(L.elem[0]); p<=&(L.elem[L.length-1]); p++) {
   
		cout<<*p<<" ";
	}
	cout<<endl;
}


int main(int argc, char* argv[]) {
   
	SqList SSL;
	int pos;
	ElemType x;
	InitList_Sq(SSL) ;
	
	cout<<"enter numbers:"<<endl ;
	while(1) {
   
		cin>>x;
		if(x==9999)
			break ;
		else
			ListEndInsert_Sq(SSL,x);
	}
	
	cout<<"The list elems:"<<endl;
	printSq(SSL);
	
	cout<<"enter the inserted position:" <<endl;
	cin>>pos;
	
	cout<<"enter the inserted number x:"<<endl;
	cin>>x ;
	
	//ListInsert_Sq(SSL, pos,x);
	if (ListInsert_Sq(SSL, pos, x)==1) {
   
		cout<<"The new list elems:"<<endl;
		printSq(SSL) ;
	}
	
	return 0;
}

建立一个动态顺序表,输出该线性表;然后依次删除表头元素、表尾元素、表的中部元素,并分别输出新的线性表。

提示:完整的程序包括程序前缀、动态顺序表的结构体类型定义、初始化子函数、表头或表尾插入新元素子函数、表头删除元素子函数、表尾删除元素子函数、表中删除元素子函数、输出子函数、主函数等。

#include<iostream>
using namespace std;

typedef int ElemType;
#define LIST_INIT_SIZE 100

//结构体类型定义
typedef struct {
   
	ElemType*elem;
	int length;
	int listsize;
} SqList;


//初始化
int InitList_Sq(SqList &L) {
   
	//申请动态空间
	L.elem=new ElemType[LIST_INIT_SIZE];
	
	//申请动态空间未成功
	if (L.elem==0) {
   
		printf ("error \n");
		return 0;
	} else {
   
		L.length=0;
		L.listsize=LIST_INIT_SIZE;
		return 1 ;
	}
}


//在表尾插入数据
int ListEndInsert_Sq(SqList &L, ElemType e) {
   
	if (L.length==L.listsize) {
   
		printf("overflow! \n") ;
		return -1;
	}
	L.elem[L.length]=e;
	L.length++;
	return 1 ;
}


//删除表头数据元素
int DelHeadList_Sq(SqList &L) {
   
	ElemType *p;
	if (L.length==0) {
   
		printf("UNDERFLOW! \n");
		return -1;
	} else {
   
		for(p=&(L.elem[1]); p<=&(L.elem[L.length-1]); p++) {
   
			*(p-1)=*p;
		}
		L.length--;
		return 1 ;
	}
}


//删除表尾数据元素
int DelEndList_Sq(SqList &L) {
   
	if(L.length==0) {
   
		printf("UNDERFLOW! \n");
		return -1 ;
	} else {
   
		L.length--;
		return 1 ;
	}
}


//删除第i个元素
int DelList_Sq(SqList &L, int i) {
   
	ElemType *p;
	//溢出
	if (L.length==0) {
   
		printf("UNDERFLOW! \n");
		return -1;
	}
	//删除位置不合法
	else if (i<=0 || i>L.length) {
   
		printf("The position error! Not deleted! \n");
		return 0;
	} else {
   
		for (p=&(L.elem[i]); p<=&(L.elem[L.length-1]); p++) {
   
			*(p-1)=*p ;
		}
		L.length--;
		return 1 ;
	}
}


//输出动态线性表元素
void printSq(SqList &L) {
   
	ElemType *p;
	for (p=&(L.elem[0]); p<=&(L.elem[L.length-1]); p++) {
   
		cout<<*p<<" ";
	}
	cout<<endl;
}


int main(int argc, char* argv[]) {
   
	SqList SSL;//定义结构体变量SSL
	int pos, x;
	InitList_Sq(SSL); //初始化线性表
	cout<<"enter numbers:"<<endl;
	while(1) {
   
		cin>>x;
		if(x==9999)
			break;
		else {
   
			ListEndInsert_Sq(SSL, x);//构造一个线性表
		}
	}
	cout<<"The list elems:"<<endl;
	printSq(SSL);
	cout<<"delete the head elem:"<<endl;

	//删除表头元素
	DelHeadList_Sq(SSL);
	cout<<"The new deleted list elems:"<<endl;
	printSq(SSL);

	//删除表尾元素
	cout<<"delete the tail elem: "<<endl;
	DelEndList_Sq(SSL);
	cout<<"The new de1eted 1ist elems:"<<endl;
	printSq(SSL);

	cout<<"enter the deleted position: "<<endl;
	cin>>pos;
	
	//De1List_sq(SSL, pos); //删除第i个数据元素
	if (DelList_Sq(SSL, pos)==1) {
   
		cout<<"The new deleted list elems:"<<endl;
		printSq(SSL);
	}
	
	return 0;
}

合并两个有序动态顺序表LA和LB,生成一个新的有序顺序表LC,并输出该顺序表LC。

提示:完整的程序包括程序前缀、动态顺序表的结构体类型定义、初始化子函数(可选)、表尾插入新元素子函数、合并有序顺序表子函数、输出子函数、主函数等。

#include<iostream>
using namespace std;

typedef int ElemType;

//结构体类型定义
struct SqList {
   
	ElemType *elem;
	int listsize;
	int length;
};


void InputData(SqList &LA);
void MergeList_Sq(SqList LA, SqList LB, SqList &LC);
void print(SqList LC);


int main(int argc, char* argv[]) {
   
	SqList LA, LB, LC;

	//初始化顺序表LA
	LA.elem=new ElemType[100];
	LA.length=0;
	LA.listsize=100;

	//初始化顺序表LB
	LB.elem=new ElemType[100];
	LB.length=0;
	LB.listsize=100;

	cout<<"创建LA表,请按升序输入整数,以9999结束:"<<endl;
	InputData(LA) ;
	cout<<"输出LA表:"<<endl;
	print(LA);
	cout<<endl;

	cout<<"创建LB表,请按升序输入整数,以9999结束:"<<endl;
	InputData(LB);
	cout<<"输出LB表:"<<endl;
	print(LB);
	cout<<endl;

	cout<<"合并LA表和LB表,生成LC表:"<<endl;
	MergeList_Sq(LA, LB, LC);
	cout<<"输出LC表:"<<endl;
	print(LC);
	
	return 0;
}


//构造升序排列的顺序表
void InputData(SqList &LA) {
   
	ElemType num;
	for(int i=0; ; i++) {
   
		cin>>num;
		if (num!=9999) {
   
			LA.elem[i]=num;
			LA.length++;
		} else
			break;
	}
}


//输出
void print(SqList LC) {
   
	for(int i=0; i<LC.length; i++)
		cout<<LC.elem[i]<<" ";
	cout<<endl;
}


//LA、LB中的元素为升序,合并后的LC也为升序
void MergeList_Sq(SqList LA, SqList LB, SqList &LC) {
   
	int i, j, k ;
	i=j=k=0;

	//初始化顺序表LC
	LC.length=LA.length + LB.length;
	LC.listsize=LA.listsize + LB.listsize;
	LC.elem=new ElemType[LC.listsize];

	//当LA、LB同时不为空时,进行下面操作
	while(i<=LA.length-1 && j<=LB.length-1) {
   
		//取LA的第i+1个元素,下标为i
		ElemType pa=LA.elem[i];
		//取LB的第j+1个元素,下标为i
		ElemType pb=LB.elem[j];

		if (pa<=pb) {
   
			LC.elem[k]=pa; //pa插入LC尾部
			k++; //LC下标后移
			i++; //LA下标后移
		} else {
    
			LC.elem[k]=pb;
			k++; //LC下标后移
			j++; //LB下标后移
		}
		
		//如果LA中还有元素,则所有元素全部插入LC尾部
		while(i<=LA.length-1) {
   
			ElemType pa=LA.elem[i];
			LC.elem[k]=pa;
			k++;
			i++;
		}
		
		//如果LB中还有元素,则所有元素全部插入LC尾部
		while(j<=LB.length-1) {
   
			ElemType pb=LB.elem[j];
			LC.elem[k]=pb;
			k++;
			j++;
		}
	}
}

构造一个单链表,分别实现在表头、表中插入新元素的操作,并输出该单链表。

提示:完整的程序包括程序前缀、单链表的结构体类型定义、表尾插入新元素子函数、表头插入新元素子函数、表中插入新元素子函数、求链表表长子函数、输出子函数、主函数等。

#include <iostream>
using namespace std;

typedef int ElemType;

//结构体类型定义
struct LinkNode {
   
	ElemType data;
	struct LinkNode *next;
};


//输出
//将链表L中的数据输出到屏幕上,其中L指向头结点
void print(LinkNode* L) {
   
	LinkNode *p; //定义一个指针变量
	p=L->next; //让p指向链表L的第一个数据
	while(p!=0) {
   
		cout<<p->data<<" "; //输出数据到屏幕上
		p=p->next; //指针后移
	}
	cout<<endl;
}


//在以H为头指针的单链表表尾插入一个数据x
//&可以去掉
void Insert_tail(LinkNode*&H, ElemType x) {
   
	LinkNode *t,*p; //定义指针
	t=new LinkNode; //申请新结点空间
	t->data=x; //向新结点中写入数据
	t->next=0; //把空地址写入新结点中
	p=H; 
  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Fortran是一种科学计算语言,经常用于高性能计算和科学工程领域。对于Fortran上机作业1,我们需要了解具体的题目内容才能给出详细解答。在这里,我将提供一些关于Fortran上机作业1的一般性指导。 通常,Fortran上机作业1会涉及到一些基本的程序编写和计算任务。以下是一些可能的题目类型: 1. 简单的数值计算:要求编写一个Fortran程序,实现基本的数学计算任务,如加法、减法、乘法和除法等。学生需要理解Fortran的基本语法和常用数学函数,并根据题目要求编写程序。 2. 循环结构:题目要求学生使用循环结构编写Fortran程序。这可能包括使用DO循环实现数列的计算、累加操作、求平均值等。学生需要熟悉循环结构的语法和用法,并且能够根据题目要求正确编写程序。 3. 条件语句:题目要求学生使用条件语句编写Fortran程序。例如,根据输入的数据,判断某个条件是否成立,并根据条件选择执行不同的操作。学生需要理解条件语句的语法和逻辑,并且能够正确编写满足要求的程序。 4. 输入和输出:题目要求学生编写带有输入和输出的Fortran程序。这可能包括从文件中读取数据、将计算结果写入文件、显示运算过程等。学生需要理解输入和输出操作的语法和用法,并能够正确处理数据的读取和输出。 无论具体的题目类型是什么,完成Fortran上机作业1的关键是理解题目要求,熟悉Fortran的语法和常用函数,并能够正确地将问题转化为程序。此外,良好的编程习惯和代码可读性也是非常重要的,包括合理的命名方式、适当的注释和代码缩进等。最后,测试程序的正确性也是不可忽视的一步,可以通过调试和对比预期结果来验证程序的正确性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值