-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.c
69 lines (68 loc) · 1.44 KB
/
list.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include "list.h"
#include "fatal.h"
#include <stdio.h>
#include <stdlib.h>
void Add(List *pList, ElementType y, ElementType x)
{
Node *p;
//add to linked-list
p = (Node *)malloc(sizeof(Node));
p->y = y, p->x = x;
p->next = NULL;
//attach
if (pList->head)
{
pList->tail->next = p;
pList->tail = p;
}
else
{
pList->head = p;
pList->tail = pList->head;
}
}
void InsertHead(List *plist, ElementType y, ElementType x)
{
Node *p = (Node *)malloc(sizeof(Node));
if (p == NULL)
FatalError("Out of space!");
p->y = y, p->x = x;
p->next = plist->head;
if (plist->head == NULL)
plist->tail = p; // 设置尾巴
plist->head = p;
}
void DeleteTail(List *plist)
{
Node *p = plist->head;
while (p->next != plist->tail)
p = p->next;
free(p->next);
p->next = NULL;
plist->tail = p;
}
// void Delete(List *pList, ElementType number)
// {
// Node *p, *q;
// for (q = NULL, p = pList->head; p; q = p, p = p->next) //这里有q=p啊啊
// {
// if (p->value == number)
// {
// if (q)
// q->next = p->next; //不会出现野指针
// else
// pList->head = p->next;
// free(p);
// break;
// }
// }
// }
void Clear(List *pList)
{
Node *p, *q;
for (p = pList->head; p; p = q)
{
q = p->next;
free(p);
}
}