# Data structure - algorithm and program design

Posted by dswain on Sun, 16 Jan 2022 09:22:29 +0100

## 1, Experimental purpose

1. Understand and master the type definition method of linear table.
2. Master the basic operations in the sequence table, including the creation of the sequence table, the addition and deletion of elements, and the basic operations such as empty and full judgment.

## 2, Experimental requirements

[Title 1 - three methods of function parameter transfer in C / C + + language]

C language provides two methods of function parameter transfer: value transfer and address transfer. In C + +, the reference mode is expanded. Through this project, confirm whether you have mastered the principles of these three methods to prepare for subsequent learning.
The exchange of the values of two variables can be realized in three ways, and the main program can be written respectively:

```//(1) Value transmission
void myswap(int x, int y)
{
int t;
t=x;
x=y;
y=t;
}

void myswap(int *p1, int *p2)    P1=&A
{
int  t;
t=*p1;
*p1=*p2;
*p2=t;
}
//(3) Reference as formal parameter
void myswap(int &x, int &y)
{
int t;
t=x;
x=y;
y=t;
}

int main()
{
int a, b;
printf("Please enter two integers to be exchanged:");
scanf("%d %d", &a, &b);
__________________;  //It is divided into three programs, and the appropriate form of calling myswap is written respectively
printf("The result of calling the swap function is:%d and %d\n", a, b);
return 0;
}

```

First exchange:
code:

```#include <iostream>
#include <stdio.h>
using namespace std;
void myswap1(int x, int y)
{
int t;
t=x;
x=y;
y=t;
}

void myswap2(int *p1, int *p2)    //P1=&A
{
int  t;
t=*p1;
*p1=*p2;
*p2=t;
}
//(3) Reference as formal parameter
void myswap3(int &x, int &y)
{
int t;
t=x;
x=y;
y=t;
}

int main()
{
int a, b;
printf("Please enter two integers to be exchanged:");
scanf("%d %d",&a,&b);
myswap1(a,b);  //It is divided into three programs, and the appropriate form of calling myswap is written respectively
printf("The result of calling the swap function is:%d and %d\n", a, b);
return 0;
}

```

Operation screenshot: Second exchange:
code:

```#include <iostream>
#include <stdio.h>
using namespace std;
void myswap1(int x, int y)
{
int t;
t=x;
x=y;
y=t;
}

void myswap2(int *p1, int *p2)    //P1=&A
{
int  t;
t=*p1;
*p1=*p2;
*p2=t;
}
//(3) Reference as formal parameter
void myswap3(int &x, int &y)
{
int t;
t=x;
x=y;
y=t;
}

int main()
{
int a, b;
printf("Please enter two integers to be exchanged:");
scanf("%d %d",&a,&b);
myswap2(&a,&b);  //It is divided into three programs, and the appropriate form of calling myswap is written respectively
printf("The result of calling the swap function is:%d and %d\n", a, b);
return 0;
}

```

Operation screenshot: The third exchange:
code:

```#include <iostream>
#include <stdio.h>
using namespace std;
void myswap1(int x, int y)
{
int t;
t=x;
x=y;
y=t;
}

void myswap2(int *p1, int *p2)    //P1=&A
{
int  t;
t=*p1;
*p1=*p2;
*p2=t;
}
//(3) Reference as formal parameter
void myswap3(int &x, int &y)
{
int t;
t=x;
x=y;
y=t;
}

int main()
{
int a, b;
printf("Please enter two integers to be exchanged:");
scanf("%d %d",&a,&b);
myswap3(a,b);  //It is divided into three programs, and the appropriate form of calling myswap is written respectively
printf("The result of calling the swap function is:%d and %d\n", a, b);
return 0;
}

```

Operation screenshot: [item 2 - multi document organization of procedures]

The goal of learning data structure is to compile programs with a considerable scale. The practice of putting all the code in one file can not apply to the requirements at this stage.
Through this project, confirm the ability to organize programs with multiple files. For the convenience of later chapters, we define the algorithm library for a data structure and can refer to the algorithm library for practice.
The simplest multi file organization. There are three files in a project:
(1) .h header file: define data types, declare custom functions, define macros, etc
(2).cpp source file 1: used to implement the custom functions declared in the header file
(3).cpp source file 2: define the main() function, which is used to call related functions to achieve the goal of problem solving.

If the procedure:

```#ifndef HEAD_H_INCLUDED
#define MaxStud 50
#define MaxCour 300
struct stud1
{
int no;
char name;
int bno;
};
struct stud2
{
int no;
int cno;
int deg;
};
double studavg(struct stud2 s2[],int m,int i);
double couravg(struct stud2 s2[],int m,int i);
void allavg(struct stud1 s1[],int n,struct stud2 s2[],int m);

```

sources.cpp:

```#include "head.h"
#include <iostream>
#include <stdio.h>
using namespace std;
double studavg(struct stud2 s2[],int m,int i)
{
int j,n=0;
double sum=0;
for(j=0;j<m;j++)
if(s2[j].no==i)
{
n++;
sum+=s2[j].deg;
}
return (sum/n);
}
double couravg(struct stud2 s2[],int m,int i)
{
int j,n=0;
double sum=0;
for(j=0;j<m;j++)
if(s2[j].cno==i)
{
n++;
sum+=s2[j].deg;
}
return (sum/n);
}
void allavg(struct stud1 s1[],int n,struct stud2 s2[],int m)
{
int i,j;
printf("Average student score:\n");
printf("  Student number    Name average\n");
i=0;
while(i<n)
{
j=s1[i].no;
printf("%4d%10s%g\n",s1[i].no,s1[i].name,studavg(s2,m,j));
i++;
}
printf("Average course score:\n");
for(i=1;i<6;i++)
printf(" curriculum%d:%g\n",i,couravg(s2,m,i));
}

```

main.cpp:

```#include "head.h"
#include <iostream>
using namespace std;
int main()
{
int n=7;
int m=21;
struct stud1 s1[MaxStud]=
{
{1,"Zhang Bin",9901},
{8,"Liu Li",9902},
{34,"Li Ying",9901},
{20,"Chen Hua",9902},
{12,"Wang Qi",9901},
{26,"Dong Qiang",9902},
{5,"Wang Ping",9901},
};
struct stud2 s2[MaxCour]=
{
{1,1,67},
{1,2,98},
{1,4,65},
{8,1,98},
{8,3,90},
{8,6,67},
{34,2,56},
{34,4,65},
{34,6,77},
{20,1,68},
{20,2,92},
{20,3,64},
{12,4,76},
{12,5,75},
{12,6,78},
{26,1,67},
{26,5,78},
{26,6,62},
{5,1,94},
{5,2,92},
{5,6,89}
};
allavg(s1,n,s2,m);
return 0;
}

```

Screenshot of program running results: [item 3 - experience complexity]
(1) Running time of two sorting algorithms
Sorting is a basic problem in computer science. There are many algorithms suitable for different situations, and it has always been the focus of algorithm research. This project provides two sorting algorithms: the selective sorting BubbleSort with complexity of O(n2) and the quick sorting quicksort with complexity of O(nlogn). The statistics of running time are added to the main function.
Please read the attached program 1 and program 2, run the program using a file of nearly 100000 data as input data, and feel the difference in running time between the two algorithms.

code:

```#ifndef HEAD_H_INCLUDED
#include<time.h>
#include<stdlib.h>
#define MAXNUM 100000
void BubbleSort(int a[],int n);
void quicksort(int data[],int first,int last);

```

sources.cpp:

```#include "head.h"
#include <iostream>
using namespace std;
void BubbleSort(int a[],int n)
{
int i,j;
bool exchange;
for(i=0;i<n-1;i++)
{
exchange=false;
for(j=n-1;j>i;j--)
{
if(a[j]<a[j-1])
{
swap(a[j],a[j-1]);
exchange=true;
}
}
if(!exchange)
return;
}
}
void quicksort(int data[],int first,int last)
{
int i,j,t,base;
if(first>last)
return;
base=data[first];
i=first;
j=last;
while(i!=j)
{
while(data[j]>=base&&i<j)
j--;
while(data[i]<=base&&i<j)
i++;
if(i<j)
{
t=data[i];
data[i]=data[j];
data[j]=t;
}
}
data[first]=data[i];
data[i]=base;
quicksort(data,first,i-1);
quicksort(data,i+1,last);
}

```

main.cpp:

```#include "head.h"
#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
int x[MAXNUM];
int n=0;
double t1,t2;
FILE *fp;
fp=fopen("numbers.txt","r");
if(fp==NULL)
{
printf("Error opening file! Please download the file and copy it to the same folder as the source program file.\n");
exit(1);
}
while(fscanf(fp,"%d",&x[n])!=EOF)
n++;
printf("Data volume: %d,Start sorting....",n);
t1=time(0);
BubbleSort(x,n);
//    quicksort(x,0,n-1);
t2=time(0);
printf("Time use %d second",(int)(t2-t1));
fclose(fp);
return 0;
}

```

Operation screenshot:  Topics: C C++ data structure