How to create a sequence table in C++

  • 2020-04-02 01:51:25
  • OfStack

To prepare data


#define MAXLEN 100 //Defines the maximum length of the sequence table
struct DATA
{
 char key[10]; //Node keyword
 char name[20];
 int age;
};
struct SLType //Define the order table structure
{
 DATA ListData[MAXLEN+1];//Saves an array of structures for the order table
 int ListLen;   //The number of stored nodes in the sequence table
}; 

The maximum length MAXLEN of the order table, the type DATA of the DATA element of the order table and the DATA structure SLType of the order table are defined.

In the data structure SLType, Listen is the number of stored nodes in the sequence table, that is, the length of the current sequence table, and ListData is an array of structures to store each data node.

We consider the order table to be the record of a class of students. Where, key is the student number, name is the name of the student, and age is the age.

Since arrays all start at index 0, we record data nodes from index 1 for convenience. The position of index 0 is not available.

Initializes the sequence table

Before using the order table, first create an empty order table, that is, initialize the order table. Here, in the program, you simply set the number of nodes in the order list ListLen to 0. In this way, the data elements that need to be added later are stored from the first location in the sequence table.
Sample code:


void SLInit(SLType * SL) //Initializes the sequence table
{
 SL->Listlen=0; 
} 

Calculate the length of the linear table

To calculate the length of the linear table is to calculate the number of nodes in the linear table. Since we define ListLen in SLType to represent the number of nodes, we only need to obtain the value of this variable.


int SLLenght(SLType *SL)
{
 return(SL->ListLen); //Returns the number of elements in the order table
} 

Insert the node

To insert a node is to insert a new node at the ith position of the linear table L, and then add 1 to the node number.
At this point, after inserting a new node, the length of the linear table L will become n+1. The difficulty of inserting nodes is that the data of each node should be moved backward. The computer is relatively large. The sample code is as follows:


int SLInsert(SLType *SL,int n,DATA data)
{
 int i;
 if(SL->ListLen>=MAXLEN) //The number of nodes in the sequence table has exceeded the maximum number
 {
  cout<<" Order table full, cannot insert node! "<<endl;
  return 0;   //Returning 0 indicates that the insert was unsuccessful
 } 
 if(n<1||n>SL->ListLen) //The insertion node number is not valid
 {
  cout<<" Error inserting sequence number! "<<endl;
  return 0;
 }
 for(i=SL->ListLen;i>=n;i--) //Moves the data in the order table backwards
 {
  SL->ListData[i+1]=SL->ListData[i];
 }
 SL->ListData[n]=data;
 SL->ListLen++;
 return 1; 
}

First in the program to determine the number of nodes in the sequence table has exceeded the maximum number, and the insertion point number is correct. After the preceding conditions are hidden, the data in the sequence table is moved backward, while the nodes are inserted, and the number of nodes is updated, ListLen.

Additional nodes

Appending nodes is inserting nodes at the end of the sequence table, so you don't have to move a lot of data, and the code implementation is much simpler than inserting nodes.


int SLAdd(SLType * SL,DATA data)
{
 if(SL->ListLen>=MAXLEN)
 {
  cout<<" The order table is full, no more nodes can be added! "<<endl;
  return 0;
 } 
 SL->ListData[++SL->ListLen]=data;
 return 1;
}

Delete nodes

To delete a node is to delete the ith node in the linear table L, so that all subsequent node Numbers are reduced by 1. Deleting nodes is similar to inserting nodes in that it requires a lot of data movement.


int SLDelete(SLType *SL,int n) //Deletes data elements from the sequence table
{
 int i;
 if(n<1||n>SL->ListLen) //It is illegal to delete the serial number of a node
 {
  cout<<" Error deleting sequence number! "<<endl;
  return 0;
 }
 for(i=n;i<SL->ListLen;i++)//Moves the data forward in the order table
 {
  SL->ListData[i]=SL->ListData[i+1]; 
 } 
 SL->ListLen--;   //Number of elements in the order table minus 1
 return 1;    //Successful deletion returns 1
} 

To find the node

A lookup node is a node whose value is x is found in the linear table L and its position in the linear table L is returned. If no node with value x is found in the linear table, an error flag is returned.
According to the type of x, the search node can be divided into:

Find the node by its serial number

For a sequence table, the ordinal number is the position of the data elements in the array, which is the subscript number of the array. Looking up nodes according to the sequence number is the most commonly used method to look up nodes in the sequence table. This is because the storage of the sequence table itself is an array. The sample code is as follows:


DATA * SLFindByNum(SLType *SL,int n)//Returns a data element based on a call sign
{
 if(n<1||n>SL->ListLen)   //It is illegal to query the serial number of a node
 {
  cout<<" Query sequence number error! "<<endl;
  return 0;
 }
 return &(SL->ListData[n]); 
} 

Find nodes by keyword

A keyword can be any item in a data element.
Here, the key keyword is introduced as an example. For example, the key can be used to find information about students. The sample code is as follows:


int SLFindByCont(SLType * SL,char *key)//Query nodes by keyword
{
 int i;
 for(i=1;i<=SL->ListLen;i++)
 {
  if(strcmp(SL->ListData[i].key,key)==0)//If I find a node
  {
   return i;
  }
 }
 return 0;      //Not found in the entire table, returns 0
} 

Show all the nodes

The sample code is as follows:


void SLALL(SLType *SL)
{
 int i;
 for(i=1;i<SL->ListLen;i++)
 {
  cout<<"key:"<<SL->ListData[i].key<<endl;
  cout<<"name:"<<SL->ListData[i].name<<endl;
  cout<<"age:"<<SL->ListData[i].age<<endl;
  cout<<"============================="<<endl; 
 }
} 

Complete example of sequence table operation:

It's basically putting the above functions together, showing you what they do, the code is a little bit long, please be patient to read ^.^


#include<iostream>
#include<string>
using namespace std;
#define MAXLEN 100 //Defines the maximum length of the sequence table
 
struct DATA
{
 string key; //Node keyword
 string  name;
 int age;
};
struct SLType //Define the order table structure
{
 DATA ListData[MAXLEN+1];//Saves an array of structures for the order table
 int ListLen;   //The number of stored nodes in the sequence table
}; 
 
void SLInit(SLType * SL) //Initializes the sequence table
{
 SL->ListLen=0; 
} 

int SLLenght(SLType *SL)
{
 return(SL->ListLen); //Returns the number of elements in the order table
} 

int SLInsert(SLType *SL,int n,DATA data)
{
 int i;
 if(SL->ListLen>=MAXLEN) //The number of nodes in the sequence table has exceeded the maximum number
 {
  cout<<" Order table full, cannot insert node! "<<endl;
  return 0;   //Returning 0 indicates that the insert was unsuccessful
 } 
 if(n<1||n>SL->ListLen) //The insertion node number is not valid
 {
  cout<<" Error inserting sequence number! "<<endl;
  return 0;
 }
 for(i=SL->ListLen;i>=n;i--) //Moves the data in the order table backwards
 {
  SL->ListData[i+1]=SL->ListData[i];
 }
 SL->ListData[n]=data;
 SL->ListLen++;
 return 1;     //Successful insertion returns 1
}
 
int SLAdd(SLType * SL,DATA data)
{
 if(SL->ListLen>=MAXLEN)
 {
  cout<<" The order table is full, no more nodes can be added! "<<endl;
  return 0;
 } 
 SL->ListData[++SL->ListLen]=data;
 return 1;
}
 
int SLDelete(SLType *SL,int n) //Deletes data elements from the sequence table
{
 int i;
 if(n<1||n>SL->ListLen) //It is illegal to delete the serial number of a node
 {
  cout<<" Error deleting sequence number! "<<endl;
  return 0;
 }
 for(i=n;i<SL->ListLen;i++)//Moves the data forward in the order table
 {
  SL->ListData[i]=SL->ListData[i+1]; 
 } 
 SL->ListLen--;   //Number of elements in the order table minus 1
 return 1;    //Successful deletion returns 1
} 

DATA * SLFindByNum(SLType *SL,int n)//Returns the data element based on the sequence number
{
 if(n<1||n>SL->ListLen)   //It is illegal to query the serial number of a node
 {
  cout<<" Query sequence number error! "<<endl;
  return 0;
 }
 return &(SL->ListData[n]); 
} 

DATA *SLFindByCont(SLType * SL,string name)//Query nodes by keyword
{
 int i;
 for(i=1;i<=SL->ListLen;i++)
 {
  if(SL->ListData[i].name==name)//If I find a node
  {
   return &(SL->ListData[i]);
  }
 }
 return 0;      //Not found in the entire table, returns 0
} 

void SLALL(SLType *SL)
{
 int i;
 for(i=1;i<=SL->ListLen;i++)
 {
  cout<<"key:"<<SL->ListData[i].key<<",name:"<<SL->ListData[i].name<<",age:"<<SL->ListData[i].age<<endl;
 }
} 
int main()
{
 int i;
 SLType SL; //Define a sequence table variable
 DATA data; //Define nodes to hold data type variables
 DATA *pdata;//Defines a pointer to a node
 string name;
 cout<<" Sequence table operation demonstration: "<<endl;
 SLInit(&SL);//Initializes the sequence table
 do
 { //Loop to add node data
  cout<<" Please enter the node (student number) to be added   The name   Age) : ";
  cin>>data.key>>data.name>>data.age;
  if(data.age)  //If the age is not 0
  {
   if(!SLAdd(&SL,data))//If adding node fails
   {
    break;   //Exit the loop
   }
  }else
  {
   break;
  } 
 }while(1);
 cout<<" The order of nodes in the sequence table is: " <<endl;
 SLALL(&SL);    //Show all the nodes
 cout<<" Please enter the node number to be taken out: ";
 cin>>i;
 pdata=SLFindByNum(&SL,i);//Find the node by the serial number
 if(pdata)
 {
  cout<<" The first "<<i<<" Node is: key:"<<pdata->key<<",name:"<<pdata->name<<",age:"<<pdata->age<<endl;
 } 
 cout<<" Please enter the name to look for: ";
 cin>>name;
 pdata=SLFindByCont(&SL,name);
 if(pdata)
 {
  cout<<"key:"<<pdata->key<<",name:"<<pdata->name<<",age:"<<pdata->age<<endl;
 } 
 cout<<" Please enter the serial number of the node you want to delete: ";
 cin>>i;
 if(SLDelete(&SL,i))
 {
  cout<<" Data deletion successful "<<endl;
  SLALL(&SL); 
 }
 cout<<" Please enter the serial number of the node you want to insert: ";
 cin>>i;
 cout<<" Please enter the first "<<i<<" The node number key . name , as well as age"<<endl;
 cin>>data.key>>data.name>>data.age;
 if(SLInsert(&SL,i,data))
 {
  cout<<" Insert data successfully "<<endl;
  SLALL(&SL); 
 } 
 return 0;
}

Operation interface:

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201310/20130925000930031.png ">

Related articles: