数据结构程序
1.主函数
#include<iostream>
#include"hestia1.h"
#include<stdlib.h>
using namespace std;
int main(){
while(true){
rq(2024,4);
zwjs();
cout<<endl;
cout<<"***************************"<<endl;
cout<<"**请选择你要使用的功能: **"<<endl;
cout<<"**1.九九乘法表 **"<<endl;
cout<<"**2.大小写转换 **"<<endl;
cout<<"**3.素数 **"<<endl;
cout<<"**4.闰年查询 **"<<endl;
cout<<"**5.顺序表 **"<<endl;
cout<<"**6.链表 **"<<endl;
cout<<"**7.顺序栈 **"<<endl;
cout<<"**0.退出程序 **"<<endl;
cout<<"***************************"<<endl;
int xyz;
cin>>xyz;
switch(xyz){
case 1:
cfb_99();
break;
case 2:
dxx();
break;
case 3:
ss();
break;
case 4:
rnpd();
break;
case 5:
main_sxb();
break;
case 6:
lb();
break;
case 7:
xsz();
break;
case 0:
exit(0);
default:
system("pause");
system("cls");
}
}
return 0;
}
2.头文件
void cfb_99();
void zwjs();
void dxx();
void rq(int a,int b);
void rnpd();
void ss();
void main_sxb();
int lb();
void xsz();
3.九九乘法表
#include<iostream>
#include<stdio.h>
using namespace std;
void cfb_99(){
for(int i=1;i<=9;i++){
for(int m=1;m<=i;m++){
cout<<i<<"*"<<m<<"="<<i*m<<" ";
}cout<<endl;
}
system("pause");
system("cls");
}
4.自我介绍
#include<iostream>
#include<stdio.h>
using namespace std;
void zwjs(){
cout<<"我是Hestia,我的课程是数据结构,我要好好完成每一个实验!"<<endl;
}
5.大小写转换
#include<iostream>
#include<stdio.h>
using namespace std;
void dxx(){
cout<<"请输入字母!"<<endl;
char h;
cin>>h;
if(h>='a'&&h<='z'||h>='A'&&h<='Z'){
if(h>='a'&&h<='z'){
h=h-32;}
else if(h>='A'&&h<='Z'){
h=h+32;
}
}
char c=h;
cout<<c;
system("pause");
system("cls");
}
6.日期显示
#include<iostream>
#include<string>
#include<ctime>
using namespace std;
int rn(int c)
{
if(c%4==0&&c%100!=0||c%400==0){
return 1;
}
return 0;
}
void rq(int a,int b)
{
cout <<a<<"年"<<b<<"月";
switch(b)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:cout<<" "<<"该月有31天"<<" ";
break;
case 4:
case 6:
case 9:
case 11:cout<<" "<<"该月有30天"<<" ";
break;
case 2:
if(rn(a)==1){
cout << " " << "该月有29天" <<" ";
}else if(rn(a)==0)
cout<<" "<<"该月有28天"<<" ";
}
time_t now = time(0);
tm *ltm = localtime(&now);
cout << "时间:" << ltm->tm_hour << ":";
cout << ltm->tm_min << ":";
cout << ltm->tm_sec << endl;
}
7.闰年判断
#include<iostream>
using namespace std;
void rnpd(){
cout<<"请输入一个年份:"<<endl;
int year;
cin>>year;
if(year%4==0&&year%100!=0||year%400==0)
{
cout<<year<<"为闰年!"<<endl;
}else{
cout<<year<<"不是闰年!"<<endl;
}
system("pause");
system("cls");
}
8.素数
#include<iostream>
using namespace std;
void ss(){
cout<<"请输入范围:"<<endl;
int www;
cin>>www;
for(int i=1;i<=www;i++){
int a=2;
for(int j=2;j<i;j++){
if(i%j==0){
break;
}
a++;
}
if(a==i)
{
cout<<i<<" ";
}
}
system("pause");
system("cls");
}
9.顺序表
#include<iostream>
#define MAXSIZE 100
#include<stdlib.h>
using namespace std;
typedef struct{
int *js;
int hestia_length;
}Hestia;
int sxb(Hestia &L){
L.js=new int[MAXSIZE];
if(L.js==NULL){
cout<<"初始化失败!"<<endl;
exit(0);
}else{
cout<<"初始化成功!"<<endl;
L.hestia_length=0;
}
}
void Input(Hestia &L,int n){
cout<<"请输入"<<n<<"个整数:"<<endl;
for(int i=0;i<n;i++){
cin>>L.js[i];
L.hestia_length=n;
}
}
void Output(Hestia L){
cout<<"输出数据为:"<<endl;
for(int i=0;i<L.hestia_length;i++){
cout<<L.js[i]<<" ";
}
}
//cout<<""<<endl;
void Insert(Hestia &L,int i,int e){
int j;
for(j=L.hestia_length;j>=i-1;j--)
{
L.js[j+1]=L.js[j];
}
L.js[i-1]=e;
L.hestia_length++;
cout<<"\n成功在第"<<i<<"位置上插入数据"<<e<<endl;
}
void Delete(Hestia &L,int i){
int j,m;
m=L.js[i-1];
for(j=i;j<=L.hestia_length;j++){
L.js[j-1]=L.js[j];}
L.hestia_length--;
cout<<"成功删除第"<<i<<"位置上的数据"<<m<<endl;
}
void main_sxb(){
Hestia H;
sxb(H);
Input(H,5);
Output(H);
Insert(H,1,10);
Delete(H,3);
Output(H);
system("pause");
system("cls");
}
10.链表
#include<iostream>
#include<iomanip>
using namespace std;
typedef struct Hes
{
int data;
struct Hes *next;
}Hes,*Hestia;
int In(Hestia &H)
{
H=new Hes;
H->next=NULL;
cout<<"链表初始化成功!"<<endl;
}
void lbsr(Hestia &H)
{
Hestia p,t;
int n;
cout<<"请输入你要加入数据的个数:" <<endl;
cin>>n;
p=H;
while(p->next!=NULL)
{
p=p->next;
}
for(int i=1;i<=n;i++)
{
t=new Hes;
p->next=t;
p=p->next;
cout<<"请输入第"<<i<<"个元素的值:";
cin>>p->data;
p->next=NULL;
}
}
void lbsc(Hestia H)
{
Hestia p;
p=H->next;
cout<<"链表H的数据输出:";
while(p!=NULL)
{
cout<<p->data<<" ";
p=p->next;
}
cout<<endl;
}
void lb_qcr(Hestia &H)
{
Hestia p,s;
p=H;
cout<<"请输入你要插入的位置:"<<endl;
int n,j=0;
cin>>n;
cout<<"请输入你要插入的数据:"<<endl;
int m;
cin>>m;
while(p&&(j<n-1))
{
p=p->next;
j++;
}
s=new Hes;
s->data=m;
s->next=p->next;
p->next=s;
cout<<"\n成功在第"<<n<<"位置上插入数据"<<m<<endl;
}
void gs(Hestia &H)
{
Hestia p;
p=H->next;
int temp=0;
while(p!=NULL)
{
p=p->next;
temp++;
}
cout<<"链表个数为:"<<temp<<endl;
}
int lb()
{
Hestia H,p,t;
In(H);
system("pause");
lbsr(H);
lb_qcr(H);
system("pause");
lbsc(H);
system("pause");
gs(H);
system("pause");
system("cls");
}
11.顺序栈
#include<iostream>
#include<iomanip>
using namespace std;
typedef int Hes;
#define MAXSIZE 100
typedef struct
{
Hes *base;
Hes *top;
int sxz_size;
}Hestia;
void IH(Hestia &H)
{
H.base=new Hes[MAXSIZE];
if(!H.base)
{
exit(0);
}
H.top=H.base;
H.sxz_size=MAXSIZE;
cout<<"链表初始化成功!"<<endl;
}
void push(Hestia &H,int a)
{
if(H.top-H.base==H.sxz_size)
{
cout<<"栈满!"<<endl;
}
else
{
*H.top=a;
H.top++;
cout<<"元素"<<a<<"已入栈!"<<endl;
}
}
int push_n(Hestia &H)
{
if(H.top-H.base==H.sxz_size)
{
cout<<"栈满!"<<endl;
return 0;
}
int n;
cout<<"请输入插入元素数量:"<<endl;
cin>>n;
for(int i=0;i<n;i++){
if(H.top-H.base==H.sxz_size)
{
cout<<"栈满!"<<endl;
return 0;
}
else
{
int a;
cout<<"请输入要插入的元素:"<<endl;
cin>>a;
*H.top=a;
H.top++;
cout<<"元素"<<a<<"已入栈!"<<endl;
}
}
return 1;
}
int out(Hestia &H)
{
int temp;
if(H.top==H.base)
{
cout<<"栈空!"<<endl;
return 0;
}
while(1){
if(H.top==H.base)
{
return 0;
}else{
H.top--;
temp=*H.top;
cout<<temp<<"出栈!"<<endl;
}
}
}
void xsz(){
Hestia H;
IH(H);
//push(H,23);
//push(H,22);
push_n(H);
out(H);
system("pause");
system("cls");
}