/* MAIN2-8.C Inspection BO2-8.C Main program///#include "c1.h" * C1.h (program name)/#include <string.h> #include <ctype.h> #i nclude<malloc.h>/* malloc () * * * #include <limits.h>/* Int_max <stdio.h>/* EOF (=^z or F6), NU LL/#include <stdlib.h>///* ATOI ()/#include <io.h>/* EOF ()/#include <math.h>/Floor (), ceil (), a BS ()/#include <process.h>/////////////////#define TRUE 1 #define FALSE 0 #define OK 1 #define ER ROR 0 #define INFEASIBLE-1/* #define OVERFLOW-2 since the value of OVERFLOW defined in MATH.H is 3, remove this line/typedef int STATUS; /* Status is the type of function, whose value is the function result status code, such as OK/typedef int Boolean; /* Boolean is a Boolean type whose value is true or FALSE */typedef int ELEMTYPE; * * #define LISTINCREMENT 2/* Linear table storage space allocation increment/typedef struct {elemtype *elem; * Storage space base */int length; * Current Length * /int listsize; /* Current allocated storage capacity in sizeof (EleMTYPE) * * *}sqlist; #include "bo2-8.c"/* bo2-8.c sequence of linear tables (storage structure defined by C2-1.h) extension Operations (10)//#include "bo2-1.c"//* bo2-1.c sequential linear table (storage structure by c2-1. H definition) basic operations (12)/Status initlist (sqlist *l)/* Algorithm 2.3/{/* Operation result: Construct an empty sequential linear table * * (*L). elem= (elemtype*) malloc (list_in
It_size*sizeof (Elemtype)); if (!) ( *l). Elem) exit (OVERFLOW); /* Storage Allocation failure//(*L). length=0; /* Empty table length is 0 */(*l). Listsize=list_init_size;
* Initial Storage capacity */return OK; Status destroylist (sqlist *l) {//* initial condition: sequential linear table L already exists.
Operation Result: Destroy order Linear table L/Free ((*l). Elem);
(*l). Elem=null;
(*l). length=0;
(*l). listsize=0;
return OK; Status clearlist (sqlist *l) {//* initial condition: sequential linear table L already exists.
Action Result: Reset L to an empty table//(*L). length=0;
return OK; } Status Listempty (sqlist l) {*/* initial condition: sequential linear table L already exists.
Operation Result: If L is an empty table, returns True, otherwise returns false/if (l.length==0) return true;
else return FALSE; int Listlength (sqlist l) {/* Initial condition: sequential linear table L already exists.
Operation Result: Returns the number of data elements in L/return l.length; Status Getelem (sqlist l,int i,elemtype *e) {/* earlyFirst condition: order Linear table L already exists, 1≤i≤listlength (l)//////////////////////if (l) returns the value of the I data element in E/if (i<1| |
i>l.length) exit (ERROR);
*e=* (l.elem+i-1);
return OK; int Locateelem (sqlist l,elemtype e,status (*compare) (Elemtype,elemtype)) {/* Initial condition: sequential linear table L already exists, compare () is a data element decision function (satisfies 1 * * * If such a data element does not exist, the return value is 0.
Algorithm 2.6 * * Elemtype *p; int i=1; /* I is the initial value of the 1th element of the bit order/P=l.elem;
/* p is the initial value of the 1th element storage location */while (I<=l.length&&!compare (*p++,e)) ++i;
if (i<=l.length) return i;
else return 0; } Status Priorelem (sqlist l,elemtype cur_e,elemtype *pre_e) {/* Initial condition: sequential linear table L already exists////* Operation Result: If Cur_e is the data element of L, and is not the first, then use PR
E_e return to its predecessor, * * or operation failure, pre_e no definition/int i=2;
Elemtype *p=l.elem+1;
while (i<=l.length&&*p!=cur_e) {p++;
i++;
} if (i>l.length) return infeasible;
else {*pre_e=*--p;
return OK; } Status Nextelem (sqlist l,elemtype Cur_e,eleYi * * int i=1;
Elemtype *p=l.elem;
while (i<l.length&&*p!=cur_e) {i++;
p++;
} if (i==l.length) return infeasible;
else {*next_e=*++p;
return OK; } Status Listinsert (sqlist *l,int i,elemtype e)/* Algorithm 2.4/{*/* initial condition: sequential linear table L already exists, 1≤i≤listlength (L) +1/* operation result: in L
Insert the new data element before the first position in e,l the length plus 1 * * Elemtype *newbase,*q,*p; if (i<1| |
I> (*l). length+1)/* I value is not legal * * return ERROR; if ((*l). length>= (*l). listsize)//* The current storage space is full, increase the allocation/{newbase= (Elemtype *) realloc ((*l). Elem, ((*l). Listsize+listi
ncrement) *sizeof (elemtype)); if (!newbase) exit (OVERFLOW); /* Storage Allocation failure//(*L). Elem=newbase; /* NEW Base Address * * (*l) listsize+=listincrement; /* Increase storage capacity */} q= (*l). elem+i-1;
/* Q is the insertion position */for (p= (*l). elem+ (*l). Length-1;p>=q;--p)/* The insertion position and the following element right Move * * (p+1) =*p; *q=e;
/* Insert E */ + + + (*l). length;
* * Table grow 1/return OK; } Status listdelete (sqlist *l,int i,elemtype *e)/* Algorithm 2.5/{*/* initial condition: sequential linear table L already exists, 1≤i≤listlength (l)/* Operation Result: Delete L
The number of elements, and the return of the value of E, L length minus 1/elemtype *p,*q; if (i<1| |
I> (*l). Length)/* I value is not legal * * return ERROR; P= (*l). elem+i-1; /* p for the location of the deleted elements * * *E=*P; /* The value of the deleted element is assigned to E/q= (*l). elem+ (*l). length-1;
/* The position of the footer element//for (++P;P<=Q;++P)/* The element after the deleted element left-Move * * (p-1) =*p; (*l). length--;
* * Table length minus 1/return OK;
Once VI () failed, the operation failed//* VI () of the form to participate in ' & ', indicating that by calling VI () to change the value of the element * * Elemtype *p;
int i;
P=l.elem;
for (i=1;i<=l.length;i++) VI (p++);
printf ("\ n");
return OK;
Ype *newbase,*p;
int k; if ((*l). length>= (*l). Listsize)/* The current storage space is full, increase the allocation */{NEWBAse= (Elemtype *) realloc ((*l). Elem, ((*l). Listsize+listincrement) *sizeof (elemtype)); if (!newbase) exit (OVERFLOW); /* Storage Allocation failure//(*L). Elem=newbase; /* NEW Base Address * * (*l) listsize+=listincrement;
/* Increase storage capacity */} p= (*l). Elem;
For (k=1;k<= (*l). length;k++) if (e>*p) p++;
else break; Listinsert (l,k,e);
, l of length plus 1 * * Elemtype *newbase,*p;
int k; if ((*l). length>= (*l). listsize)//* The current storage space is full, increase the allocation/{newbase= (Elemtype *) realloc ((*l). Elem, ((*l). Listsize+listi
ncrement) *sizeof (elemtype)); if (!newbase) exit (OVERFLOW); /* Storage Allocation failure//(*L). Elem=newbase; /* NEW Base Address * * (*l) listsize+=listincrement;
/* Increase storage capacity */} p= (*l). Elem;
For (k=1;k<= (*l). length;k++) if (e<*p) p++;
else break; Listinsert (l,k,e);
/* function in bo2-1.c/} Status Headinsert (SqList *l,elemtype e) {/* Initial condition: sequential linear table L already exists.
Operation Result: Insert new data element e,l length in L head plus 1 */Elemtype *p,*q,*newbase; if ((*l). length>= (*l). Listsize) {newbase= (Elemtype) realloc ((*l) Elem, ((*l). Listsize+listincrement) *sizeof (El
Emtype));
if (!newbase) exit (OVERFLOW);
(*l). Elem=newbase;
(*l). Listsize+=listincrement;
} q= (*l). Elem;
For (p= (*l). elem+ (*l). Length-1;p>=q;--p) * (p+1) =*p;
*q=e;
(*l). length++;
return OK; Status Endinsert (sqlist *l,elemtype e) {*/* initial condition: sequential linear table L already exists.
Operation Result: Inserts the new data element e,l in the tail of l the length to add 1 * * * elemtype *q,*newbase; if ((*l). length>= (*l). listsize)//* The current storage space is full, increase the allocation/{newbase= (Elemtype *) realloc ((*l). Elem, ((*l). Listsize+listi
ncrement) *sizeof (elemtype)); if (!newbase) exit (OVERFLOW); /* Storage Allocation failure//(*L). Elem=newbase; /* NEW Base Address * * (*l) listsize+=listincrement; /* Increase storage capacity */} q= (*l). elem+ (*l). length;
/* Q for the insertion position * * *Q=E;
(*l). length++;
return OK; } Status Deletefirst (sqlist *l,elemtype *e) {/* Initial condition: sequential linear table L has been savedif (Listempty (*L))/* Empty table Cannot delete/return ERROR; P= (*l). Elem;
/* p points to the first element/*e=*p; Q= (*l). elem+ (*l). length-1; /* Q points to the last element */for (++p;p<=q;++p) * (p-1) =*p; /* from the 2nd element, all elements move forward one position//(*L). length--;
/* Current length minus 1/return OK;
Status deletetail (sqlist *l,elemtype *e) {*/* initial condition: order Linear table L already exists and has not less than 1 elements///////////////////////////////////////////
Elemtype *p; if (!) (
*l. Length) * * Empty table/return ERROR; P= (*l). elem+ (*l). length-1; /* The position of the last data element * * *E=*P; /* The value of the deleted element is assigned to E/* (*l). length--;
* * Table length minus 1/return OK;
The Status Deleteelem (sqlist *l,elemtype e) {/* Deletes the element with the value E in the table and returns true; if there is no this element, return false */int i=0,j;
while (i< (*l). length&&e!=* (*l). elem+i)) i++;
if (i== (*l). Length)/* Not found/return FALSE; else {for (j=i;j< (*l). length;j++) * ((*l). Elem+j) =* ((*l). elem+j+1);/* The elements behind are moved forward/(*l). length--; * Current length minus 1 */return TRUE } Status Replaceelem (sqlist l,int i,elemtype e) {/* to represent the value of the I element in L with E */if (i<1| |
I>L.LENGTH)/* I value is not valid * * exit (ERROR);
* (l.elem+i-1) =e;
return OK;
The Status creatascend (sqlist *l,int N) {/* Sets the linear table/int i,j of n elements in a non descending order;
Elemtype e;
Initlist (L);
printf ("Please enter%d elements: \ n", n);
scanf ("%d", &e); Listinsert (l,1,e);
/* Insert the 1th element in the empty table */for (i=1;i<n;i++) {scanf ("%d", &e);
For (j=0;j< (*l). length;j++) if (e<=* (*l). Elem+j)) the break; Listinsert (l,j+1,e);
/* insert in Table/* return TRUE;
The Status creatdescend (sqlist *l,int N) {/* Sets the linear table/int i,j of n elements in a non ascending order;
Elemtype e;
Initlist (L);
printf ("Please enter%d elements: \ n", n);
scanf ("%d", &e); Listinsert (l,1,e);
/* Insert the 1th element in the empty table */for (i=1;i<n;i++) {scanf ("%d", &e);
For (j=0;j< (*l). length;j++) if (e>=* (*l). Elem+j)) the break; Listinsert (l,j+1,e);
/* insert in Table/* return TRUE; } void Visit (Elemtype *c)/* ListTraverse () called function (type to be consistent) * */{printf ("%d", *c);
int main () {sqlist L;
Elemtype d,e;
Status i;
int n;
printf ("Create the linear table L of n elements in non descending order, enter the number of elements N:");
scanf ("%d", &n);
Creatascend (&l,n);
printf ("Output the elements of L in turn:");
Listtraverse (L,visit); Insertascend (&l,10);
/* Insert element in non-descending order/printf ("After inserting element 10 in descending order, linear table L is:");
Listtraverse (L,visit); Headinsert (&l,12); /* Insert A/Endinsert (&l,9) in the head of L;
/* At the tail of L Insert 9/printf ("Insert 12 in the head of L, after the tail is inserted 9, linear table L is:");
Listtraverse (L,visit);
printf ("Enter the value of the element you want to delete:");
scanf ("%d", &e);
I=deleteelem (&l,e);
if (i) printf ("Successfully delete%d\n", e);
else printf ("no element%d!\n", e);
printf ("Linear table L is:");
Listtraverse (L,visit);
printf ("Enter the new value for the ordinal element of the element to be replaced:");
scanf ("%d%d", &n,&e);
Replaceelem (l,n,e);
printf ("Linear table L is:");
Listtraverse (L,visit);
Destroylist (&L);
printf ("Destroy L, in non ascending order to re-establish n elements of the linear table L, enter the number of elements N (>2):");
scanf ("%d", &n);
Creatdescend (&l,n);
printf ("Output the elements of L in turn:"); Listtraverse (L,visit); Insertdescend (&l,10);
/* Insert element in non-ascending order/printf ("After inserting element 10 in non-ascending order, linear table L is:");
Listtraverse (L,visit);
printf ("Enter the value of the element you want to delete:");
scanf ("%d", &e);
I=deleteelem (&l,e);
if (i) printf ("Successfully delete%d\n", e);
else printf ("no element%d!\n", e);
printf ("Linear table L is:");
Listtraverse (L,visit);
Deletefirst (&l,&e);
Deletetail (&L,&D);
printf ("Delete table header element%d and footer element%d, linear table L is: \ n", e,d);
Listtraverse (L,visit); }