CodePudding user response:
Feelings are not quite rightCodePudding user response:
What do youto c or c + +Give you wrote a C, and then you came here to ask
C + +If in c + +, directly to encapsulate a class out
Yourself with the
#include
#include
#include
//linear table definition, use an int array to achieve
Typedef struct LineListType {int nMaxSize; Int nSize; Int * pData; } LineList.
//create an empty linear table
LineList * CreateLineList (void) {
LineList * pList=(LineList *) malloc (sizeof (LineList));
PList - & gt; NMaxSize=8;
PList - & gt; NSize=0;
PList - & gt; PData=https://bbs.csdn.net/topics/(int *) malloc (pList -> nMaxSize * sizeof (int));
Return pList.
};
//in the back of the linear table, an insert an array
Void AppendList LineList * pList, int n, int * (v) {
If (pList==NULL) return;
If (pList - & gt; NMaxSize pList - & gt; NSize & lt; N) {
PList - & gt; NMaxSize +=(n + 7) & gt;> 3 & lt; <3;
PList - & gt; PData=https://bbs.csdn.net/topics/(int *) realloc (pList -> pData, pList -> nMaxSize * sizeof (int));
}
Memcpy (pList - & gt; PData + pList - & gt; NSize, v, n * sizeof (int));
PList - & gt; NSize +=n;
};
//print output linear table
Void PrintList (LineList * pList) {
for (int i=0; i
printf("\n");
};
//insert in the specified location, range of 0 ~ nSize, tail can be inserted
Int InsertList (LineList * pList, int nPos, int v)
{
If (NULL==pList) return 0;
If (nPos & lt; 0 | | nPos & gt; PList - & gt; NSize) {
Printf (" InsertList: Wrong postion \ n \ 007 ");
return 0;
}
If (pList - & gt; NMaxSize & lt; PList - & gt; NSize + 1) {
PList - & gt; NMaxSize +=8;
PList - & gt; PData=https://bbs.csdn.net/topics/(int *) realloc (pList -> pData, pList -> nMaxSize * sizeof (int));
};
Int t=pList - & gt; NSize++;
While (t> NPos) {
PList - & gt; PData [t]=pList - & gt; PData [t - 1);
- t;
}
PList - & gt; PData [t]=v;
return 1;
}
//delete the specified location for
//return 0 means falure, return 1 means success
Int DeleteAtList (LineList * pList, int nPos) {
If (NULL==pList) return 0;
If (nPos & lt; 0 | | nPos & gt;=pList - & gt; NSize) {
Printf (" DeleteAtList: Wrong Position \ n ");
return 0;
};
While (nPos
NPos++;
};
- pList - & gt; NSize;
return 1;
};
//sort with a comparison function, to the tree using the
Int comp (const void * a, const void * b) {
Return * (a) (int *) & gt; * (b) (int *);
};
//linear table sorting, if you like, the structure of the extended linear table
Void SortList (LineList * pList) {
Tree (pList - & gt; PData, pList - & gt; NSize, sizeof (int), comp);
};
//linear table to sort the already do insert
Int InsertSortedList (LineList * pList, int v) {
//write yourself
}
Int main () {
LineList * pList=CreateLineList ();
Int init []={12, 25, 7, 42,19,38};
AppendList (pList, 6, init);
PrintList (pList);
InsertList (pList, 3, 2);
PrintList (pList);
InsertList (pList, 12, 4);
DeleteAtList (pList, 2);
PrintList (pList);
SortList (pList);
PrintList (pList);
return 0;
}
CodePudding user response:
#include# define maxsize 100
Typedef struct
{
Int elem [maxsize];
int length;
} Sqlist;
Sqlist L;
Initlist (Sqlist * L, int n)//initialization 1
{
//L - & gt; Length=0;
int i;
//printf (" please enter the % d element: ", n);
L - & gt; Length=n;
For (I=0; i
}
If ((I)==n) {printf (" * * * * * * * * * * * * * * * * * * initializes the success!!! * * * * * * * * * * * * * * * * * * \ n "); Listtlength (L); }
}
Listtlength length (Sqlist * L)//o 2
{
Printf (" the length of the linear list is: % d \ n ", L - & gt; Length);
}
TraverseList (Sqlist * L, int n)//traverse the sequence table 3
{
int i;
Printf (" traversal results: ");
For (I=0; i
printf("\n"); Listtlength (L); printf("\n");
}
GetElem (Sqlist * L, int, int * e1)//read specify the location of the element
4{
* e1=L - & gt; Elem [I];
}
LocateElem (Sqlist * L, int n, int * e2)//find some elements, and returns the position of the first appeared 5
{
Int I, flag=0;
For (I=0; i
Flag=1;
break; }
} the if (flag==1) printf (" the element in the first % d linear table position \ n ", I + 1);
Else printf (" this element is not in linear table \ n ");
}
ListInsert (Sqlist * L, int, int * e3)//insert element to specify the location 6
{
int j=0;
(L - & gt; Length) + +;
For (j=L - & gt; Length; J> i; J -) {
L - & gt; Elem [1]=L - & gt; Elem [j - 2];
}
L - & gt; nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull