Data structure (C language version) sequential table correlation algorithm code implementation

Source: Internet
Author: User

This two-day implementation of the sequence table related operations, including sequential table initialization, creation, traversal, the first element before inserting, delete the I element, find the location of the element E, empty order table, destroy the order table, merge two non-descending order table operations.

This time on the Internet to learn a new layout method, the storage structure of the Order table definition, function description part in the header file, the source file is implemented by the main function and function functions.

The compilation environment used this time is vc++6.0

//Header File Section#defineList_init_size 5#defineIncreament 5typedefCharElemtype;typedefintStatus;//Linear table storage structure definitiontypedefstruct{elemtype*Elem;intlength;intListsize;} SqList;//Function Description SectionStatus initlist (SqList &m); Status createlist (SqList&l,intsize); Status traverselist (SqList L); Status Listinsert (SqList&l,inti,elemtype e); Status Listdelete (SqList&l,intI,elemtype &e);intLocateelem (sqlist l,elemtype e,status (*Compare) (Elemtype e,elemtype temp)); Status clearlist (SqList&m); Status destroylist (SqList&m); Status Equalelem (elemtype e,elemtype temp);voidMergelist (sqlist la,sqlist lb,sqlist &Lc);//Source Files Section#include <stdio.h>#include<stdlib.h>//the malloc function is included in the//when you include a custom header file, you can only use ""#include"ListHeader.h"//Program EntryvoidMain () {intSize=6;    SqList list;    Elemtype deletes;    Initlist (list); CreateList (list,size);//traverselist (list);//Listinsert (list,5, ' o ');//traverselist (list);//Listdelete (list,5,deletes);//traverselist (list);//printf ("Find the element sequence that conforms to compare relationship:%d\n", Locateelem (list, ' B ', Equalelem));//clearlist (list);//traverselist (list);//destroylist (list);//traverselist (list);SqList List2;    Initlist (LIST2); CreateList (List2,5);    SqList List3;    Initlist (LIST3);    Mergelist (LIST,LIST2,LIST3);    Traverselist (LIST3); GetChar ();}//initialize a sequential tableStatus initlist (SqList &L) {       //Allocating MemoryL.elem = (Elemtype *) malloc (list_init_size*sizeof(Elemtype)); if(!L.elem) {        return false; } l.length=0; L.listsize=list_init_size; return true;}//Create a sequential table (determine if you need to reallocate memory, read the element in)Status createlist (SqList &l,intsize) {    if(size>l.listsize) {Elemtype*newbase; Newbase= (Elemtype *) realloc (L.elem, (l.listsize+increament) *sizeof(Elemtype)); if(!newbase) {            return false; } L.elem=newbase; L.listsize+=increament; } printf ("input Create order table content: \ n");  for(intI=0; i<size;i++) {scanf ("%c", l.elem+i); GetChar ();//input 6 elements, just enter three times to automatically end, because vc6.0 will enter also counted into the word character, here with GetChar () receive} l.length=size; return true;}//sequential table traversal, using pointers to manipulateStatus traverselist (sqlist L) {if(l.length==0) {printf ("Empty Table! "); return false; } printf ("traversal result: \ n");  for(intI=0; i<l.length;i++) {printf ("%c", * (l.elem++)); printf ("\ n"); }    return true;}//The element is inserted before the first element in the order table, and the subscript is i-1Status Listinsert (SqList &l,intI,elemtype e) {    //Illegal judgment    if(i<0|| I>l.length) {        return false; }    if(l.length>=l.listsize) {Elemtype*newbase;//in order to better judge the spatial distribution results, create a new variable newbaseNewbase= (Elemtype *) realloc (L.elem, (l.listsize+increament) *sizeof(Elemtype)); if(!newbase) {            return false; } l.listsize+=increament; } elemtype*itemp,*ttemp; Itemp=& (l.elem[i-1]); Ttemp=& (l.elem[l.length-1]);  for(ttemp;ttemp>=itemp;ttemp--){        * (ttemp+1)=*ttemp; }    *itemp=e;//the itemp here always points to the position of the I element, so it is better to use him to assign a valuel.length+=1; return true;}//Delete the first element of the order tableStatus Listdelete (SqList &l,intI,elemtype &e) {    if(i<0|| I>l.length) {        return false; } elemtype*itemp=& (l.elem[i-1]); E=*itemp; Elemtype*ttemp=& (l.elem[l.length-1]);  for(itemp;itemp<ttemp;itemp++){        *itemp=* (itemp+1); } l.length--; return true;}//returns the first element in the order table with an e that satisfies the compare () relationship, does not exist return 0intLocateelem (sqlist l,elemtype e,status (*Compare) (Elemtype e,elemtype temp)) {    intI=1; Elemtype*temp=L.elem;  while(! (*compare) (e,*temp) &&i<=l.length) {Temp++; I++; }    if(i>l.length) {i=0; }    returni;}//Empty the order table, just let length=0Status clearlist (SqList &L) {L.length=0; return true;}//Destroy Order table, release elem pointerStatus destroylist (SqList &m) {    if(L.elem) {free (L.elem); } l.length=0; return true;}//Two elements are equalStatus Equalelem (elemtype e,elemtype temp) {if(e==temp) {        return true; }    return false;}//merges two non-descending sequential tables into a new sequential tablevoidMergelist (sqlist la,sqlist lb,sqlist &Lc) {Lc.listsize=la.length+lb.length; Lc.length=lc.listsize; Lc.elem= (Elemtype *) malloc (lc.listsize*sizeof(Elemtype)); Elemtype*pa,*pb,*pa_last,*pb_last,*pc; PA=&la.elem[0]; PB=&lb.elem[0]; Pa_last=& (la.elem[la.length-1]); Pb_last=& (lb.elem[lb.length-1]); PC=&lc.elem[0];  while(pa<=pa_last&&pb<=pb_last) {        if(*pa<*pb) {            *pc++=*pa++; }        Else{            *pc++=*pb++; }    }     while(pa<=pa_last) {            *pc++=*pa++; }     while(pb<=pb_last) {            *pc++=*pb++; }}

Data structure (C language version) sequential table correlation algorithm code implementation

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.