测试代码
singlelinkedlist.h头文件
#ifndef __SINGLELINKEDLIST_H__
#include<stdio.h>
#include<windows.h>
#include<assert.h>
typedef int DataType;
typedef struct ListNode
{
DataType data;
struct ListNode *next;
}ListNode;
ListNode *InitList(DataType num);
void PushBack(ListNode **pplist, DataType num);
void PrintList(ListNode *plist);
void DestoryList(ListNode** pplist);
void PopBack(ListNode **pplist);
void PushFront(ListNode **pplist, DataType num);
void PopFront(ListNode **pplist);
ListNode* Find(ListNode *plist, DataType num);
void Insert(ListNode** pplist, ListNode* pos, DataType x);
void Erase(ListNode** pplist, ListNode* pos);
#endif
- 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
- 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
singlelinkedlist.c实现部分
#include "singlelinkedlist.h"
ListNode *InitList(DataType num)
{
ListNode *node = (ListNode*)malloc(sizeof(ListNode));
node->data = num;
node->next = NULL;
return node;
}
void PushBack(ListNode **pplist, DataType num)
{
if (*pplist == NULL)
{
*pplist = InitList(num);
}
else if ((*pplist)->next == NULL)
{
(*pplist)->next = InitList(num);
}
else
{
ListNode *tail = *pplist;
while (tail->next)
{
tail = tail->next;
}
tail->next = InitList(num);
}
}
void PrintList(ListNode *plist)
{
ListNode *tail = plist;
while (tail)
{
printf("%d->", tail->data);
tail = tail->next;
}
printf("NULL");
printf("\n");
}
void DestoryList(ListNode** pplist)
{
assert(pplist);
ListNode* cur = *pplist;
while (cur)
{
ListNode* next = cur->next;
free(cur);
cur = next;
}
*pplist = NULL;
}
void PopBack(ListNode **pplist)
{
if (*pplist == NULL)
{
return;
}
else if ((*pplist)->next == NULL)
{
free(*pplist);
*pplist = NULL;
}
else
{
ListNode* tail = *pplist;
ListNode* pos = tail;
while (tail->next)
{
pos = tail;
tail = tail->next;
}
free(tail);
tail = NULL;
pos->next = NULL;
}
}
void PushFront(ListNode **pplist, DataType num)
{
if (*pplist == NULL)
{
*pplist = InitList(num);
}
else
{
ListNode *tmp = InitList(num);
tmp->next = *pplist;
*pplist = tmp;
}
}
void PopFront(ListNode **pplist)
{
if (*pplist == NULL)
{
return;
}
else if ((*pplist)->next == NULL)
{
*pplist = NULL;
}
else
{
ListNode *tmp = (*pplist)->next;
free(*pplist);
*pplist = tmp;
}
}
ListNode* Find(ListNode *plist, DataType num)
{
assert(plist);
while (plist)
{
if (plist->data == num)
{
return plist;
}
plist = plist->next;
}
return NULL;
}
void Insert(ListNode** pplist, ListNode* pos, DataType num)
{
assert(*pplist&&pos);
if (((*pplist)->next == NULL) || (pos == *pplist))
{
PushFront(pplist, num);
}
else
{
ListNode* tmp = NULL;
ListNode* tail = *pplist;
while (tail->next != pos)
{
tail = tail->next;
}
tmp = InitList(num);
tail->next = tmp;
tmp->next = pos;
}
}
void Erase(ListNode** pplist, ListNode* pos)
{
assert(*pplist&&pos);
if (((*pplist)->next == NULL) || (*pplist == pos))
{
PopFront(pplist);
}
else
{
ListNode* tmp = *pplist;
while (tmp->next != pos)
{
tmp = tmp->next;
}
tmp->next = pos->next;
free(pos);
pos = NULL;
}
}
- 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
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 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
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
test.c测试部分
#include "singlelinkedlist.h"
void test()
{
ListNode *list = NULL;
PushBack(&list, 1);
PushBack(&list, 2);
PushBack(&list, 3);
PushBack(&list, 4);
PrintList(list);
PopBack(&list);
PrintList(list);
PopBack(&list);
PrintList(list);
PopBack(&list);
PrintList(list);
PopBack(&list);
PrintList(list);
}
void test1()
{
ListNode *list = NULL;
PushFront(&list, 1);
PushFront(&list, 2);
PushFront(&list, 3);
PushFront(&list, 4);
PushFront(&list, 5);
PrintList(list);
PopFront(&list);
PrintList(list);
PopFront(&list);
PrintList(list);
PopFront(&list);
PrintList(list);
PopFront(&list);
PrintList(list);
PopFront(&list);
PrintList(list);
}
void test2()
{
ListNode *list = NULL;
PushFront(&list, 1);
PushFront(&list, 2);
PushFront(&list, 4);
PushFront(&list, 5);
PushFront(&list, 6);
PrintList(list);
ListNode *ret = Find(list, 2);
Insert(&list, ret, 3);
PrintList(list);
Erase(&list, ret);
PrintList(list);
}
int main()
{
test2();
system("pause");
return 0;
}
- 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
- 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
test()测试结果
test1()测试结果
test2()测试结果