How to establish a sequential table _c language in C + +

Source: Internet
Author: User

Preparing data

Copy Code code as follows:

#define MAXLEN 100//define the maximum length of the order table
struct DATA
{
Char key[10]; Key words for nodes
Char name[20];
int age;
};
struct Sltype//definition of sequential table structure
{
DATA listdata[maxlen+1];//An array of structures to save sequential tables
int Listlen; Number of stored nodes in the sequential table
};

Defines the maximum length maxlen of sequential tables, the type of data elements of sequential tables, and the data structure sltype of sequential tables.

In the data structure Sltype, listen is the number of existing nodes in the sequential table, that is, the length of the current order table, Listdata is an array of structures to store the data nodes.

We think the order table is a record of a class of students. Where key is the school number, name is the student, and age is the ages.

Since the array starts with subscript 0, we start recording the data node from subscript 1 and the location of subscript 0 is not available.

Initialization Order Table

Before using a sequential table, you first create an empty sequential table, which is the initialization order table. Here, in the program only set the order table of the number of nodes Listlen 0 can be. In this way, the data elements that need to be added later are stored from the first location in the sequential table.
Sample code:

Copy Code code as follows:

void Slinit (Sltype * SL)//Initialization Order table
{
sl->listlen=0;
}

calculate the length of a linear table

Calculating the length of a linear table is to calculate the number of nodes in a linear table, because we have defined Listlen in Sltype to represent the number of nodes, so we just need to get the value of this variable.

Copy Code code as follows:

int Sllenght (Sltype *sl)
{
return (Sl->listlen); Returns the number of elements in a sequential table
}

inserting Nodes

The insertion node inserts a new node in the first position of the linear table L, so that the subsequent node number is added 1 in turn.
At this point, after inserting a new node, the length of the linear table L becomes n+1. The difficulty with inserting a node operation is that each subsequent node data is moved backwards and the computer is larger, with the sample code as follows:

Copy Code code as follows:

int Slinsert (sltype *sl,int n,data DATA)
{
int i;
if (Sl->listlen>=maxlen)//sequential table nodes have exceeded the maximum number
{
cout<< "The order table is full and cannot be inserted into the node!" "<<endl;
return 0; Return 0 indicates the insertion was unsuccessful
}
if (n<1| | N>sl->listlen)//The serial number of the insertion node is illegal.
{
cout<< "Insert serial number Error!" "<<endl;
return 0;
}
for (i=sl->listlen;i>=n;i--)//Move the data in the order table backward
{
sl->listdata[i+1]=sl->listdata[i];
}
sl->listdata[n]=data;
sl->listlen++;
return 1;
}

The maximum number of sequential table nodes is first judged in the program, and the number of insertion points is correct. Before the condition is concealed, the data in the order table is moved backwards, insert the node, and update the number of nodes Listlen.

Append nodes

An append node is a node inserted at the end of the sequential table, so there is no need for a large amount of data to be moved, and the code implementation is much simpler than inserting the node.

Copy Code code as follows:

int Sladd (Sltype * sl,data DATA)
{
if (Sl->listlen>=maxlen)
{
cout<< "The order table is full and no more nodes can be added!" "<<endl;
return 0;
}
sl->listdata[++sl->listlen]=data;
return 1;
}

Delete a node

Deleting a node is the deletion of the first node in the linear table L, so that all nodes in the following sequence are reduced by 1. This is, after deleting a node, the length of the linear table L becomes n-1. Deleting a node and inserting a node is similar, requiring a large amount of data to be moved.

Copy Code code as follows:

int Sldelete (sltype *sl,int N)//delete data elements in the order table
{
int i;
if (n<1| | N>sl->listlen)//delete the serial number of the node is illegal.
{
cout<< "Delete serial number Error!" "<<endl;
return 0;
}
for (i=n;i<sl->listlen;i++)//Moves the data in the Order table forward
{
sl->listdata[i]=sl->listdata[i+1];
}
sl->listlen--; Number of sequential table elements minus 1
return 1; Successful deletion returns 1
}

Finding Nodes

The lookup node is the node that looks for x in the linear table L, and returns the position of the nodal point in the linear table L. If a node with a value of x is not found in the linear table, an error flag is returned.
Depending on the type of x, the lookup node can be divided into:

Find nodes by ordinal

For a sequential table, the ordinal is the position of the data element in the array, which is the subscript label of the array. Finding nodes by ordinal is the most common way to find nodes in a sequential table, because the stored order table itself is an array, and the sample code is as follows:

Copy Code code as follows:

Data * Slfindbynum (sltype *sl,int N)//returns the element according to the call sign
{
if (n<1| | N>sl->listlen)//The serial number of the query node is illegal.
{
cout<< "Query serial number Error!" "<<endl;
return 0;
}
Return & (Sl->listdata[n]);
}

find nodes by keyword

A keyword can be any item in a data element.
This is an example of the key keyword, for example, you can find the student's information through key. The sample code is as follows:

Copy Code code as follows:

int Slfindbycont (Sltype * Sl,char *key)//query node by keyword
{
int i;
for (i=1;i<=sl->listlen;i++)
{
if (strcmp (sl->listdata[i].key,key) ==0)//If the node is found
{
return i;
}
}
return 0; Not found in the entire table, returns 0
}

Show all the nodes .

The sample code is as follows:

Copy Code code 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;
}
}

Sequential table Operation Complete Example:

Basically put the above function into a piece, the central display of the function, the code is a bit long, please be patient to read ^.^

Copy Code code as follows:

#include <iostream>
#include <string>
using namespace Std;
#define MAXLEN 100//define the maximum length of the order table
/************** the definition part of the sequential table *****************/
struct DATA
{
String key; Key words for nodes
String name;
int age;
};
struct Sltype//definition of sequential table structure
{
DATA listdata[maxlen+1];//An array of structures to save sequential tables
int Listlen; Number of stored nodes in the sequential table
};
initialization function of/************ sequential table *****************/
void Slinit (Sltype * SL)//Initialization Order table
{
sl->listlen=0;
}
/*********** calculates the length of a linear table *******************/
int Sllenght (Sltype *sl)
{
return (Sl->listlen); Returns the number of elements in a sequential table
}
/********* Insertion Node *******************************/
int Slinsert (sltype *sl,int n,data DATA)
{
int i;
if (Sl->listlen>=maxlen)//sequential table nodes have exceeded the maximum number
{
cout<< "The order table is full and cannot be inserted into the node!" "<<endl;
return 0; Return 0 indicates the insertion was unsuccessful
}
if (n<1| | N>sl->listlen)//The serial number of the insertion node is illegal.
{
cout<< "Insert serial number Error!" "<<endl;
return 0;
}
for (i=sl->listlen;i>=n;i--)//Move the data in the order table backward
{
sl->listdata[i+1]=sl->listdata[i];
}
sl->listdata[n]=data;
sl->listlen++;
return 1; Insert successfully, return 1
}
/*********************** Append node *************************/
int Sladd (Sltype * sl,data DATA)
{
if (Sl->listlen>=maxlen)
{
cout<< "The order table is full and no more nodes can be added!" "<<endl;
return 0;
}
sl->listdata[++sl->listlen]=data;
return 1;
}
/*********************** Delete Node *************************/
int Sldelete (sltype *sl,int N)//delete data elements in the order table
{
int i;
if (n<1| | N>sl->listlen)//delete the serial number of the node is illegal.
{
cout<< "Delete serial number Error!" "<<endl;
return 0;
}
for (i=n;i<sl->listlen;i++)//Moves the data in the Order table forward
{
sl->listdata[i]=sl->listdata[i+1];
}
sl->listlen--; Number of sequential table elements minus 1
return 1; Successful deletion returns 1
}
/******************* find nodes by ordinal ********************/
Data * Slfindbynum (sltype *sl,int N)//Returns the element by ordinal
{
if (n<1| | N>sl->listlen)//The serial number of the query node is illegal.
{
cout<< "Query serial number Error!" "<<endl;
return 0;
}
Return & (Sl->listdata[n]);
}
/******************* find nodes by keyword ********************/
DATA *slfindbycont (Sltype * sl,string name)/query node by keyword
{
int i;
for (i=1;i<=sl->listlen;i++)
{
if (sl->listdata[i].name==name)//If the node is found
{
Return & (Sl->listdata[i]);
}
}
return 0; Not found in the entire table, returns 0
}
/******************* shows all the nodes ********************/
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; Defining a sequential Table variable
Data data; To define a node to save a data type variable
DATA *pdata;//defines a pointer variable that points to a node
String name;
cout<< "Sequential table operation Demo:" <<endl;
Slinit (&AMP;SL);//Initialization Order table
Todo
{//Loop Add node data
cout<< "Please enter the node to add (school number name age):";
cin>>data.key>>data.name>>data.age;
if (data.age)//If the age is not 0
{
if (! Sladd (&sl,data))//If Add node failure
{
Break Exit loop
}
}else
{
Break
}
}while (1);
The order of nodes in the cout<< "sequence table is:" <<endl;
Slall (&AMP;SL); Show all the nodes.
cout<< "Please enter the node number to be removed:";
cin>>i;
Pdata=slfindbynum (&sl,i);//Find nodes by ordinal
if (pdata)
{
cout<< "<<i<<" node is: Key: "<<pdata->key<<", Name: "<<pdata->name<< ", Age:" <<pdata->age<<endl;
}
cout<< "Please enter the name you want to find:";
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 success" <<endl;
Slall (&AMP;SL);
}
cout<< "Please enter the serial number of the node you want to insert:";
cin>>i;
cout<< "Please enter the key,name of the" <<i<< "node, and age" <<endl;
cin>>data.key>>data.name>>data.age;
if (Slinsert (&sl,i,data))
{
cout<< "Insert data Success" <<endl;
Slall (&AMP;SL);
}
return 0;
}

Run the interface:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.