Chain storage of linear tables (2)

1. Preparation

We first prepare the structure for storage (take student information as an example)

typedef struct //Create a structure variable for student information
{
char name[];
int age;
char num[];
}stu;

typedef struct element//创建用于存储的结构体变量
{
stu info;
struct element* Next=NULL;
}ele;

int main ()
{
he * start = (he **) malloc (100 × sizeof (he))

return 0;
}

2. Additions, deletions, modification and release (in order to distinguish, we use ~ instead of *)

1. To add an element, we need to complete these steps
(1) find the position in the linked list
(2) add the element
void Listadd(ele~start,int n,ele~ptr)//n is the added position, ptr is required Added elements
{
assert(n<100);
assert(start);
ele~p=start;
while(p->Next&&n--)
{
p=p->Next;
}
ptr->Next=(p->Next )->Next;
p->Next=ptr;
}
2. Deleting an element is similar to it, so I won’t repeat it.
3. Find an element
(1) find the position of the element
(2) return its pointer
ele~ Listfind(ele~start,int n)
{
ele~p=start;
assert(p);
assert(n<100);
p = p ->Next;
while(p&&n--)
{
p=p->Next;
}
return p;
4. Modify an element
(1) find the element at that position
(2) modify
void Listchange(ele~start,int n,ele~ptr)
{
ele~p=start;
assert(p);
assert(n<100);
while(p&&n--)
{

        p=p->Next;
        }

        ptr->Next=p->Next;
        p->Next=ptr;

}
5.释放链表
void Freelist(ele~start)
{
ele~p=start;
assert(p);
ele~tmp=NULL;
while(p->Next)
{
tmp=p;
p=p->Next;
free(p);
}
p=NULL;
}

3. We will explain the questions asked at the beginning of the previous article

It
is not difficult to arrange all the elements in the two arrays into a large array in descending order , but if you want to save space, you might as well use a linked list

Chain storage of linear tables (2)
As shown in the figure, first arrange the two arrays in order from largest to smallest, then compare the size of the element pointed to by the pointer of p and q, and add one to the pointer to the smaller element; then compare. The first comparison saves the larger element, and in subsequent comparisons, the address of the larger element in each comparison is stored in the pointer field of the larger element in the previous comparison.

Guess you like

Origin blog.51cto.com/14961637/2657485
Recommended