"Data Structure-linear table" sequential table

Source: Internet
Author: User

About order Tables

A sequential table structure is a contiguous storage space where data elements are stored sequentially
You can think of a sequential table as a team, run a queue, or a person out of the team. These operations are insert and delete operations for sequential tables

Sequential table Features
    • The logical relationship is adjacent to the two elements, the physical location must also be adjacent (for example, the No. 0 element and the 1th element, their position in the order table is adjacent, they are also adjacent in memory)
    • Can randomly access elements in a table
    • Operational complexity O (n)
Insert operations on sequential tables

Insert operation is the same as the queue, Xiao Ming to jump in the queue, then this position and the back of the students are to move back a position empty out of this position, Xiao Ming can squeeze in

    1. First check that the position of the inserted element is correct, not less than the No. 0 position of the order table, greater than the length of the sequential table-1
    2. The element of the position to be inserted and its subsequent elements are moved one bit backwards, vacated position
    3. Insert element, sequential table length +1
/ * Insert data into the sequential table, list represents the order table, Loc indicates where to insert the element, and num represents the element to insert * /voidInsert (List *List,intLocintNUM) {if(Loc <0|| Loc>List->length) {//Determine if LOC values meet the requirements        return; }Else{ for(inti =List->length; i > loc; i--) {List->data[i] =List->data[i-1];//The element that will insert the position and all subsequent elements move backward one}List-&GT;DATA[LOC] = num;//Assign a sequential table loc location to num        List->length++;//Sequential table length +1}}
Delete operations for sequential tables

delete operation and the same as the team, Xiaoming is in line, suddenly belly pain to go to the toilet, Xiao Ming walked, empty out of this position, this position after all the students have to forward a position, to fill this vacancy
1. First check if the location you want to delete is legal
2. Save the element that you want to delete
3. Move all the elements from this position forward one at a time, overriding the elements of this position
4. Sequential table Length-1

/ * Remove elements from the linear table, Loc represents the deleted position, and temp represents the deleted element * /int Delete(List *List,intLocint*temp) {if(Loc <0|| Loc>List->length-1) {return 0; }Else{*temp =List->data[loc]; for(inti = loc; I <List->length-1; i++) {List->data[i]=List->data[i +1];//To delete a position after all elements move forward one}List->length--;//Sequential table length-1        return 1; }}
Query operations for sequential tables query elements where
/*找出elem在顺序表中的位置*/int locateElem(List *listint elem) {    int0;    whilelist1list->data[i] != elem) {//当出界或者找到这个元素结束循环        i++;    }    iflist->length) {        return0;    }    else {        return i;    }}
Who is the element that queries the LOC location?
/ * The ordinal table position is the element value of Loc * /int Getelem (List *List, int loc) {if(Loc<0 ||Loc>List -Length- 1) {printf ("Take the decimal place out of bounds");return 0; }Else{return List -Data[Loc]; }}
Implementing a sequential table using the C language
#include <stdio.h>#define SIZE/ * Sequential table structure * /typedef struct{intData[size];//Data storage array    intLengththe current length of the//order table, with an initial value of 0}list;/ * Insert data into the sequential table, list represents the order table, Loc indicates where to insert the element, and num represents the element to insert * /voidInsert (List *List,intLocintNUM) {if(Loc <0|| Loc>List->length) {//Determine if LOC values meet the requirements        return; }Else{ for(inti =List->length; i > loc; i--) {List->data[i] =List->data[i-1];//The element that will insert the position and all subsequent elements move backward one}List-&GT;DATA[LOC] = num;//Assign a sequential table loc location to num        List->length++;//Sequential table length +1}}/ * Remove elements from the linear table, Loc represents the deleted position, and temp represents the deleted element * /int Delete(List *List,intLocint*temp) {if(Loc <0|| Loc>List->length-1) {return 0; }Else{*temp =List->data[loc]; for(inti = loc; I <List->length-1; i++) {List->data[i]=List->data[i +1];//To delete a position after all elements move forward one}List->length--;//Sequential table length-1        return 1; }}/ * The ordinal table position is the element value of Loc * /intGetelem (List *List,intLOC) {if(loc<0|| Loc>List->length-1) {printf("Take the decimal place out of bounds");return 0; }Else{return List->data[loc]; }}/ * Find the location of the Elem in the order table * /intLocateelem (List *List,intElem) {inti =0; while(I <List->length-1&&List->data[i]! = elem) {//When out of bounds or find this element end loopi++; }if(I >=List->length) {return 0; }Else{returnI }}/ * All elements in the output order table * /voidDisplay (List *List) { for(inti =0; I <List->length; i++) {printf("Elements in the sequential table are%d\n",List->data[i]); }}intMain () {ListList;List. length =0; Insert (&List,0,1); Insert (&List,1,2); Insert (&List,2,3); Insert (&List,3,4); Insert (&List,4,5); Display (&List);inttemp =0;Delete(&List,2, &temp);printf("deleted element is%d\n", temp); Display (&List);intLOC = Locateelem (&List,4);printf("The position of the order table where element 4 is%d", loc);return 0;}

"Data Structure-linear table" sequential table

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.