5. Function and recursive call (see teaching materials)
6. Structure and Links (Difficulties)
Drawing of the list (here only the one-way not loop chain table)
This list is divided into:
1. Link list without head nodes
N
D1
N
D2
N
D3
NULL
D3
HEAD
1. Link list with headed nodes
HEAD
NULL
N
D1
N
D2
N
D3
NULL
D3
Why have this difference? You think about it first.
2. Insert in the list without head nodes
Divided into two middle conditions (some places are divided into 3 cases)
2.1. On the list
Start data definition typefedef struct slist {int data; slist * next;};
N
D1
N
Di
HEAD
N
Di
NULL
Di
N
Di
New
N
Di
NULL
Di
NULL
DNEW
HEAD
Slist * new; new-> Date = value; new-> next = null;
N
Di
New
N
Di
NULL
Di
N
DNEW
HEAD
NEW-> next = Head;
N
Di
New
N
Di
NULL
Di
N
DNEW
×
HEAD
HEAD = New;
* Note: New-> next = head; head = new; can not be exchanged, why? Do you want to think about?
2.2. In the chain table and tail
N
Di
N
Di
NULL
Di
p
Find the seat to insert
N
Di
New
N
Di
NULL
Di
N
DNEW
p
Slist * new; new-> Date = value; new-> next = null;
N
Di
N
Di
NULL
Di
New
N
DNEW
p
New-> next = p-> next; / * P-> Next is null * /
N
Di
N
Di
NULL
Di
New
N
DNEW
p
×
P-> next = new;
3. Insert in the chain list with headed nodes
N
HEAD
N
Di
NULL
Di
p
HEAD
. . . .
Find the seat to insert
N
HEAD
New
N
Di
NULL
Di
N
DNEW
p
HEAD
. . . .
Slist * new; new-> Date = value; new-> next = null;
. . . .
N
HEAD
N
Di
NULL
Di
New
N
DNEW
p
HEAD
New-> next = p-> next; / * P-> Next is null * /
N
HEAD
N
Di
NULL
Di
New
N
DNEW
p
×
. . . .
P-> next = new;
Inserting and inserting a linked list of tape nodes inserting and without header nodes, you will find that you will find a simpler insertion in the lead node.
4. Delete
N
HEAD
N
Di
NULL
Di
p
HEAD
P-> Next = P-> next -> next;
example 1. N-name students put them on the list of headed nodes. Please prepare a functionality FUN (), its function is: its function is the average number of computing grades and returns it.
For example: grades: 85, 76, 69, 85, 91, 72, 64, 87
Average score: 78.625
Note: Some source programs have been given.
Do not change the contents of the main function main ().
Question procedure:
#include
#define n 8
Struct Slist
{
Double S;
Struct slist * next;
}
Typedef struct slist streec
Double Fun (strec * h)
{
}
STREC * CREATE (Double * S)
{
STREC * H, * P, * q;
INT i = 0;
H = p = (strec *) Malloc (SIZEOF (STREC));
P-> s = 0;
While (i { Q = (strec *) Malloc (SIZEOF (STREC)); Q-> s = s [i]; i ; P-> next = q; P = P-> next; } P-> next = 0; Return H; } Outlist (strec * h) { STREC * P; P = h-> next; Printf ("HEAD"); DO { Printf ("->% 4.1f ", p-> s); P = P-> next; } while (p! = 0); Printf ("/ n / n"); } Main () { Double S [N] = {85, 76, 69, 85, 91, 72, 64, 87}, AVE STREC * H; H = CREATE (S); Outlist (h); AVE = Fun (h); PRINTF ("AVE =% 6.3f / N ", AVE); } Difficulties in this question: How to traverse the entire list, but in the test program. So is not difficult. Reference answer: Double Fun (strec * h) { Double aver = 0.0; While (h! = null) { AVER = H-> S; H = h-> next; } AVER / = N; Return AVER; }