Home > Back-end >  Which master can give a template
Which master can give a template

Time:10-27

The experiment should be how to do? After write what to write what ah?

CodePudding user response:

Simple to write, and test yourself
#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; iPrintf (" % d ", pList - & gt; PData [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 (nPosPList - & gt; PData [nPos]=pList - & gt; PData [nPos + 1];
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:

I studied the data structures, a freshman, it should be in c and c + +
  • Related