Implementation of the sequence table insertion, deletion, lookup, output operation in C language is often used. The following small series for everyone to clean up the implementation code, together look at it
The code looks like this:
#include <iostream> using namespace std;
#define MAXSIZE typedef int DATATYPE;
typedef struct {DataType data[maxsize];//usually an array is used to describe the data storage of the sequential table int seqlength;/* Linear table length */} seqlist; Seqlist *init_seqlist (); Initialization sequence table void Define_seqlist (Seqlist *l,int N); The contents of the Fill order table void Display_seqlist (Seqlist *l); Extracts the element int insert_seqlist (seqlist *l,int i,datatype x) in the sequential table; Adds an element to the specified position (from start) int delete_seqlist (seqlist *l,int i);
Deletes the element at the specified location (from the start) "Sequence.cpp" #include "Sequence.h" #include <iostream> using namespace std;
Seqlist *init_seqlist ()//Sequence table initialization algorithm, the order table empty {seqlist *l;
L=new seqlist; l->seqlength=0;
/* length to -1*/return L; } void Define_seqlist (Seqlist *l,int N)//sequence table definition algorithm {cout<< "Enter the elements to be stored in the order table:" <<endl; for (int i=0;i<n;i++
{cin>>l->data[i];//input array element l->seqlength++;}} void Display_seqlist (Seqlist *l)//sequential table output algorithm {cout<< "the element stored in the sequential table is" <<endl; int i; for (i=0;i<=l->
seqlength-1;i++) {cout<<l->data[i]<< "";}
cout<<endl;
}int insert_seqlist (seqlist *l,int i,datatype x)//sequential table Insert algorithm {cout<< "insert element" <<x<< "to position" <<i<
< "Up" <<endl;
Int J; if (l->seqlength==maxsize-1)//array length equals set value-1, the table full {cout<< "table full" <<endl; return-1;} if (i<1| | I>l->seqlength+1//Insertion position before first, or inserted to a length greater than the current array +1 {cout<< "positional error" <<endl; return 0;} for (j=l->
seqlength-1;j>=i;j--)//i all after the {l->data[j+1]=l->data[j];} l->data[i]=x;
Fills an element to a blank position l->seqlength++;
cout<< "Insert Success" <<endl;
Display_seqlist (L);
return 1; The delete algorithm for the int delete_seqlist (seqlist *l,int i)//order Table {cout<< "Removes the element with position <<i<<" <<endl; Int J; if (i& lt;1| | I>l->seqlength) {cout<< "no" <<i<< "element" <<endl; return 0;} for (j=i;j<=l->
seqlength-1;j++) {l->data[j]=l->data[j+1]; after//i index all forward} l->seqlength--;
cout<< "Delete Success" <<endl;
Display_seqlist (L);
return 1; }
"Test_sequence.cpp"
#include "Sequence.h"
#include <iostream>
using namespace std;
int main ()
{
seqlist *l;//sequence table definition
l=init_seqlist ();//sequence table initialization
define_seqlist (l,6);//define Order table
Display_seqlist (L); the output insert_seqlist (l,4,3) of the sequential table
;//the Insertion
insert_seqlist (l,6,21)
of the sequential table; Insert_seqlist (l,2,15);
Delete_seqlist (l,5);//The deletion
delete_seqlist (l,3)
of the order table; Delete_seqlist (l,12);
return 0;
}
The effect is as follows:
The above is a small set to introduce the C + + implementation of the Common Order table operation (insert delete Find output), I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!