-
Notifications
You must be signed in to change notification settings - Fork 0
/
List.c
119 lines (110 loc) · 2.21 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
* =====================================================================================
*
* Filename: List.c
*
* Description:
*
* Version: 1.0
* Created: 11/29/2014 03:14:11 PM
* Revision: none
* Compiler: gcc
*
* Author: 张世龙 (mn), [email protected]
* Company: free
*
* =====================================================================================
*/
#include "Public.h"
#include "List.h"
void InitList(LinkList *list)
{
*list = (LinkList)malloc(sizeof(struct Node));
assert(*list);
(*list)->next = NULL;
(*list)->data = 0;
}
int ListEmpty(LinkList list)
{
assert(list);
return list->data == 0;
}
int ClearList(LinkList list)
{
assert(list);
Node *NodePtr = list->next;
while(NodePtr){
Node *tmpNodePtr = NodePtr->next;
free(NodePtr);
NodePtr = tmpNodePtr;
list->data--;
}
if(list->data != 0)
return ERROR;
list->next = NULL;
return SUCCESS;
}
int GetElem(LinkList list,int i,ElemType *e)
{
assert(list && e);
if(i<1 || i>list->data)
return ERROR;
Node *foundNodePtr = list->next;
while(--i)
foundNodePtr = foundNodePtr->next;
*e = foundNodePtr->data;
return SUCCESS;
}
int LocateElem(LinkList list,ElemType *e)
{
assert(list && e);
Node *nodePtr = list->next;
int pos = 0;
while(nodePtr){
++pos;
if(nodePtr->data == *e)
break;
nodePtr = nodePtr->next;
}
return nodePtr!=NULL?pos:0;
}
int ListInsert(LinkList list,int i,ElemType *e)
{
assert(list && e);
if(i<1)
return ERROR;
if(i>list->data)
i = list->data+1; //插入到最后
Node *nodePtr = list;
while(--i)
nodePtr = nodePtr->next;
Node *newNodePtr = (Node*)malloc(sizeof(struct Node));
newNodePtr->data = *e;
Node *tmpNodePtr = nodePtr->next;
nodePtr->next = newNodePtr;
newNodePtr->next = tmpNodePtr;
list->data++;
return SUCCESS;
}
int ListDelete(LinkList list,int i,ElemType *e)
{
assert(list && e);
if(i<1 || i>list->data)
return ERROR;
Node *nodePtr = list;
Node *parentNode = list;
while(i--){
parentNode = nodePtr;
nodePtr = nodePtr->next;
}
*e = nodePtr->data;
parentNode->next = nodePtr->next;
free(nodePtr);
list->data--;
return SUCCESS;
}
int ListLength(LinkList list)
{
assert(list);
return list->data;
}