Data structure-sequential table correlation algorithm

Source: Internet
Author: User

#include <stdio.h>
#include <stdlib.h>

#define LIST_INIT_SIZE 100
#define Listincrement 10
#define OVERFLOW-2
#define OK 1
#define ERROR 0
typedef int ELEMTYPE;

//Sequential table structure
typedef struct {
Elemtype *elem;
int length;
int listsize;
}sqlist;

int initlist_sq (sqlist *l);
int listinsert_sq (sqlist *l,int i,elemtype e);
void listinput_sq (SqList *l);
void listoutput_sq (SqList *l);
int listlength_sq (sqlist *l);
int listdelete_sq (sqlist *l,int i,elemtype *e);
int locateelem_sq (sqlist *l,elemtype e,int (*compare) (Elemtype,elemtype));
int compare (elemtype a,elemtype b);
void Mergelist_sq (sqlist *la,sqlist *lb,sqlist *LC);
void Getelem (sqlist *l,elemtype i,elemtype *e);
int emptylist (sqlist *l);
void Listinverse (SqList *l);

int main ()
{
SqList SQ,SQ1,SQ2;
int len,e,loc,e1,f;
e1 = 1;
//Sequential table initialization
INITLIST_SQ (&AMP;SQ);

//Enter elements in the sequential table
LISTINPUT_SQ (&AMP;SQ);

//elements in the output order table
LISTOUTPUT_SQ (&AMP;SQ);

//inserting elements into the sequential table
LISTINSERT_SQ (&sq,3,5);
LISTOUTPUT_SQ (&AMP;SQ);

//Output order table length
Len = listlength_sq (&AMP;SQ);
printf ("The length of the linear table is:%d\n", Len);

//Delete an element from a position in the order table and return the deleted element
LISTDELETE_SQ (&sq,3,&e);
printf ("Deleted elements:%d\n", e);
LISTOUTPUT_SQ (&AMP;SQ);

//The bit order of an element in the Output order table
loc = LOCATEELEM_SQ (&sq,3,&compare);
printf ("This element is ordered as:%d\n", loc);

//Merge two sequential tables
INITLIST_SQ (&AMP;SQ1);
LISTINPUT_SQ (&AMP;SQ1);
MERGELIST_SQ (&AMP;SQ,&AMP;SQ1,&AMP;SQ2);
LISTOUTPUT_SQ (&AMP;SQ2);

//Output order table is not empty
f = emptylist (&AMP;SQ2);
if (f==0) {
printf ("is the order table empty?") False\n ");
} else {
printf ("is the order table empty?") True\n ");
}

//Output order table elements of a position
Getelem (&sq2,4,&e);
printf ("The first element is:%d\n", e);

//Reverse the Order table
Listinverse (&AMP;SQ2);
LISTOUTPUT_SQ (&AMP;SQ2);
return 0;
}
//Initialize linear table
int initlist_sq (SqList *l) {
L->elem = (Elemtype *) malloc (list_init_size*sizeof (elemtype));
if (! L->elem) exit (OVERFLOW);
l->length = 0;
L->listsize = list_init_size;
return OK;
}
//Enter elements into a linear table
void listinput_sq (SqList *l) {
int i,m,n;
printf ("Please enter the number of elements in the linear table: \ n");
scanf ("%d", &n);
printf ("Please INPUT element: \ n");
for (i=0; i<n; i++) {
scanf ("%d", &m);
L->elem[i] = m;
l->length++;
}

Return
}

//Output element from linear table
void listoutput_sq (SqList *l) {
int i;
if (L->length = = 0) {
printf ("There are no elements in this linear table!") \ n ");
} else {
printf ("The element in the linear table is:");
for (i=0; i<l->length; i++) {
printf ("%d", l->elem[i]);
}
}
printf ("\ n");
Return
}
//Returns the number of elements in a linear table
int listlength_sq (SqList *l) {
Return l->length;
}
//inserting elements into a linear table
int listinsert_sq (sqlist *l,int i,elemtype e) {
SqList *newbase;
int *p,*q;
int m;
if (i<1 | | i>l->length+1) return ERROR;
if (l->length >= l->listsize) {
Newbase = (Elemtype *) realloc (L->elem, (l->listsize+listincrement) *sizeof (elemtype));
if (!newbase) exit (OVERFLOW);
L->elem = Newbase;
L->listsize + = listincrement;
}
/*q = & (L->elem[i-1]);
For (p=& (l->elem[l->length-1]);p >=q;--p) {
* (p+1) = *p;
}
*q = e;*/
For (m=l->length-1; m>=i-1;--m) {
L-&GT;ELEM[M+1] = l->elem[m];
}
L->elem[i-1] = e;
++l->length;
return OK;
}
//delete elements in a linear table
int listdelete_sq (sqlist *l,int i,elemtype *e) {
int m;
int *p,*q;
if (i<1 | | i>l->length+1) return ERROR;
E = l->elem[i-1];
for (m=i-1;m<l->length-1;++m) {
L-&GT;ELEM[M] = l->elem[m+1];
}

/*p = & (L->elem[i-1]);
*e = *p;
Q = l->elem+l->length-1;
for (++p; p<=q; ++p) {
* (p-1) = *p;
}*/
--l->length;
return OK;
}
//Find the position of an element in a linear table
int locateelem_sq (sqlist *l,elemtype e,int (*compare) (Elemtype,elemtype)) {
int i;
i = 1;
while (I<=l->length &&!) ( *compare) (l->elem[i-1],e)) {
++i;
}
if (i<=l->length) return i;
else return 0;
}
int compare (Elemtype A,elemtype b) {
if (a = = b) {
return 1;
} else {
return 0;
}
}
//merge sort merges two sequential tables (these two sequential tables are ordered)
void Mergelist_sq (sqlist *la,sqlist *lb,sqlist *lc) {
int i,j,k;
i=0;j=0;k=0;
Lc->listsize = Lc->length = La->length + lb->length;
Lc->elem = (elemtype*) malloc (lc->listsize*sizeof (elemtype));
if (! Lc->elem) exit (OVERFLOW);
while (I<la->length && j<lb->length) {
if (La->elem[i] <= lb->elem[j]) lc->elem[k++] = la->elem[i++];
else lc->elem[k++] = lb->elem[j++];
}
while (i<la->length) lc->elem[k++] = la->elem[i++];
while (j<lb->length) lc->elem[k++] = lb->elem[j++];
Return
}
//Returns the value of element I in the order table
void Getelem (sqlist *l,elemtype i,elemtype *e) {
*e = l->elem[i-1];
}
//Determines whether the order table is non-null, is empty, returns True, otherwise, returns false.
int emptylist (SqList *l) {
if (l->length==0) {
return 1;
} else {
return 0;
}
}
//Sequential table reverse
void Listinverse (SqList *l) {
int i,temp;
for (i=0; i<l->length/2;i++) {
temp = l->elem[i];
L->elem[i] = l->elem[l->length-1-i];
L->elem[l->length-1-i] = temp;
}
Return
}







Data structure-sequential table correlation algorithm

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.