C + + implementation of the common operation of the order table (insert out Find output) _c language

Source: Internet
Author: User

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!

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.