Home > Back-end >  Where there is wrong, the code to solve
Where there is wrong, the code to solve

Time:09-25

# include "stdio.h"
# include "stdlib. H"

Typedef char ListItem.

Struct alist {
int n;/* long table, when the table is empty, n=0 */
Int maxsize; The maximum length of the linear table *//* said
Int length;
ListItem * data;/* */table element array
};

Typedef struct alist * List;

The List ListInit (int size);/* */initialization function
Int ListEmpty (List L);/* determine whether linear table L is empty */
Int ListLength (List L); Long table/* o */
Void ListInsert (int k, ListItem x, the List L);/* after the first k elements to insert a new element x */
Int ListLocate (ListItem x, the List L);/* return to the positions of the elements in table x */
ListItem ListRetrieve (int k, the List L);/* to obtain the first k in linear table data element content */
Void PrintList (List L);/* output the entire linear table */
ListItem ListDelete (int k, the List L);/* will delete the first k elements */

Int main (int arg c, char * argv [])
{
The List of L=ListInit (10);

ListInsert (0, 'a', L);
ListInsert (1, 'b', L);
ListInsert (2, 'c', L);
ListInsert (3, 'd', L);
ListInsert (4, 'e', L);

PrintList (L);

ListDelete (4 L);

PrintList (L);

Int loc=ListLocate (3 L);
Printf (" the position of the element 3: % c \ n \ n ", loc);

If (ListEmpty (L))
{
Printf (" linear table L is empty! \ n \ n ");
}
The else
{
Printf (" linear table L do not empty! \ n \ n ");
}

Int length=ListLength (L);
Printf (" long linear table L table: % d \ n \ n ", length);

/*========================================*/
/* can further test the functionality of the function you write below */
/*========================================*/


return 0;
}

/* */initialization function
The List ListInit (int size)
{
The List of L;
L=(List) malloc (sizeof (struct alist));

/* please use malloc function or new operation, for the initial spatial distribution of linear table */


L - & gt; Maxsize=size;
L - & gt; n=0;/* the current linear table length s */
return L;/* successful, returns the L */
}

/* determine whether linear table L is empty */
Int ListEmpty List (L)
{
/* please write the code, the realization of function */

If (L - & gt; Length==0) {return 1; }
return 0;

}

Long table/* o */
Int ListLength List (L)
{
If (L==NULL) {return 0; } return L - & gt; Length;
/* please write the code, the realization of function */

}

/* after the first k elements to insert a new element x */
Void ListInsert (int k, ListItem x, the List L)
{
Printf (" % d % c insert elements in the position of \ n \ n ", k, x);
int i;
If (k & lt; 0 | | k & gt; L - & gt; N)
{
Printf (" out of bounds ");
exit(0);
}

If (L - & gt; N==L - & gt; Maxsize)
{
Printf (" out of memory ");
exit(0);
}
The else
{
For (k=I; K & lt;=L - & gt; Length; K -)
{
L - & gt; Data [k + 1)=L - & gt; Data [k];
}
L - & gt; Data [I - 1]=x;
L - & gt; Length++;//the length of the data table 1
}/* please add code here to realize after the first k elements inserted value for the functionality of the new element x */

}

/* return to the positions of the elements in table x */
Int ListLocate (ListItem x, the List L)
{
/* please add code here to realize functions */
Int k;
For (k=0; K & lt; L - & gt; Length; K++)
{
If (L - & gt; Data [k]==x)
{
//k for the corresponding array subscript e, in the table corresponding to the serial number shall be the k + 1
The return of k + 1;
}
}
return 0;
}

/* to obtain the first k in linear table data element content */
ListItem ListRetrieve (int k, the List L)
{
/* determine whether k value is reasonable, if not reasonable, the ERROR */
If (k<1 | | k> L - & gt; N)
{
Printf (" ERROR: out of bounds ");
exit(0);
}

/* please add code here to realize functions */
If (L - & gt; Length & lt; 1 | | (L - & gt; Length & gt; ListLength (L)))
{
return 0;
}
//data element of the serial number starting from 1, the array in the table below starting from 0, the first k elements corresponding to the array subscript k - 1;
The return of L - & gt; Data [k - 1);
}

/* output the entire linear table */
Void PrintList (List L)
{
/* please write the code, the realization of function */
Int k;
If (ListEmpty (L))
{
Printf (" list is empty! \n");
return;
}
For (k=0; K & lt; L - & gt; Length; K++)
{
Printf (" % 3 d, L - & gt; Data [k]);
}
printf("\n");
}

/* will delete the first k elements */
ListItem ListDelete (int k, the List L)
{
Int I; ListItem x;
If (k<1 | | k> L - & gt; N)
{
Printf (" ERROR: out of bounds ");
exit(0);
}

Printf (" to delete the % d element \ n \ n ", k);
X=L - & gt; Data [k - 1);

X=L - & gt; Data [I - 1);
For (k=I; K & lt; L - & gt; Length; K++)
{
L - & gt; Data [k - 1]=L - & gt; Data [k];
}
L - & gt; Length -;


Return the x;
}

CodePudding user response:

You should tell us what you said what is the phenomenon of "wrong", is a compilation error or run short of expectations? Otherwise it is difficult for us to help you locate

CodePudding user response:

Man you don't take the debug problems?

Oid ListInsert (int k, ListItem x, the List L)
{
Printf (" % d % c insert elements in the position of \ n \ n ", k, x);
int I; //not initialize
If (k & lt; 0 | | k & gt; L - & gt; N)
{
Printf (" out of bounds ");
exit(0);
}

If (L - & gt; N==L - & gt; Maxsize)
{
Printf (" out of memory ");
exit(0);
}
The else
{
for (k=I; K & lt;=L - & gt; Length; K -) //a for loop will not perform
{
L - & gt; Data [k + 1)=L - & gt; Data [k];
}
L - & gt; Data [I - 1]=x; //an array, the coredump

CodePudding user response:

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related