1. The purpose of the experiment
Understand and implement the basic functions of mastering sequential linear tables.
2. Experiment content and implementation process steps
Sequential linear table source code and implementation:
#include<iostream>
#include<malloc.h>
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
typedef struct{
int * element;
int length;
int listsize;
}List;
using namespace std;
void InitList_list(List &L);// Construct an empty linear list L.
void Clear_list(List &L);// Clear the original table
int Locate_list(List &L,int e);// Find the position of the element and return the first element position with the same value
void Traverse_list(List &L);// Traverse the elements in the linear list, if you traverse the record type, you need to insert the operator
void buffersort_list(List &L);// Sort, from small to large
int Length_list(List &L);// Calculate the length of the linear table
bool Insert_list(List &L,int i,int e);// Insert elements
bool Delete_list(List &L,int i); // Delete the specified element
Function module area: The following is the implementation code of the above function functions
void InitList_list(List &L)
{
L.elem=(int*)malloc(LIST_INIT_SIZE*sizeof(int));
if(!L.elem)
{
cout<<" Dynamic non-matching failure "<<endl;
exit(1);
}
L.length=0;
L.listsize=LIST_INIT_SIZE;
}
void Clear_list(List &L)// Clear the original table
{
if(L.elem)
{
delete [] L.elem;
L.elem=NULL;
}
L.length=0;
L.listsize=0;
}
int Locate_list(List &L,int e)// Find the position of the element and return the first element position with the same value
{
for(int i=0;i<L.length;i++)
{
if(L.elem[i]==e)
{
cout<<" The position number of the element you are looking for is : "<<i+1<<endl;
return i+1;
}
}
return -1;
}
void Traverse_list(List &L)// Traverse the elements in the linear list, if you traverse the record type, you need to insert the operator
{
for(int i=0;i<L.length;i++)
{
if(i!=(L.length-1))
cout<<L.elem[i]<<" ";
else
cout<<L.elem[i]<<endl;
}
}
void buffersort_list(List &L)// Sort, from small to large
{
for(int i=0;i<L.length-1;i++)
{
for(int j=i+1;j<L.length;j++)
{
if(L.elem[i]>L.elem[j])
{
int temp=L.elem[i];
L.elem [i] = L.elem [j];
L.elem[j]=temp;
}
}
}
}
int Length_list(List &L)// Calculate the length of the linear table
{
return L.length;
}
bool Insert_list(List &L,int i,int e)// Insert elements at the specified position
{
if(i<1||i>L.length+1)
cout<<"The value of i is invalid "<<endl;
if(L.length==L.listsize)
{
cout<<"The current storage space is full "<<endl;
int k=sizeof(int);
L.elem=(int *)realloc(L.elem,(L.listsize+LISTINCREMENT)*k);
if(!L.elem)
{
cout<<" dynamic allocation failed "<<endl;
exit(1);
}
L.listsize=L.listsize+LISTINCREMENT;
}
for(int j=L.length-1;j>=i-1;j--)
{
L.elem [j + 1] = L.elem [j];
}
L.elem[i-1]=e;
L.length++;
return true;
}
bool Delete_list(List &L,int i) // Delete the specified element
{
if(L.length==0)
{
cout<<" Linear table is empty "<<endl;
return false;
}
if(i<1 ||i>L.length)
{
cout<<"The input i value is invalid "<<endl;
return false;
}
int e = L.elem [i-1];
for(int j=i;j<L.length;j++)
{
L.elem [j-1] = L.elem [j];
}
L.length--;
return true;
}
Main function area: The following is my debugging of each function, it does not have to be exactly the same.
intmain()
{
int a[15]={-1};
int i;
cout<<" Please enter 10 positive integers from the linear table : "<<endl;
for(int i=0;i<10;i++)
{
cin>>a[i];
}
cout<<endl;
List list;
InitList_list(list);
for(i=0;i<10;i++)
{
Insert_list(list,i+1,a[i]);
}
cout<<" Iterate over the elements of the newly inserted linear list: "<<endl;
Traverse_list(list);
Locate_list(list,list.length);
cout<<" The length of the linear list is: "<<Length_list(list)<<endl;
buffersort_list(list);
cout<<" The linear table is sorted in ascending order: "<<endl;
Traverse_list(list);
cout<<" Please enter the element you want to delete: " <<endl;
int Delete_element;
cin>>Delete_elem;
int locate=Locate_list(list,Delete_elem);
Delete_list(list,locate);
cout<<"The deleted linear table is: "<<endl;
Traverse_list(list);
system("PAUSE");
return 0;
}
3. Summary of the experiment
(Conclusions drawn through experiments; a deeper understanding of theoretical knowledge related to information systems and signal processing, etc.)
A source code written by myself ( refer to the C language version of the data structure), and the pseudo code is changed into the source code, there may be some problems. Personally, I think it is relatively simple, and some places are not rigorous enough (for example: the sorting is too low , it can be written as sort(<algorithm>) ).
But the basic operation is certainly possible.
The main function is relatively simple to write, mainly because I don't know what to write.
Writing more code like this helps to understand the role of linear tables.
Thank you for watching, and please give me more advice!