-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathList.cc
71 lines (64 loc) · 1.26 KB
/
List.cc
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
#include <iostream>
#include <stdlib.h>
using namespace std;
struct ListNode
{
int data;
ListNode *next;
};
void deleteList(struct ListNode * head)
{
struct ListNode *cur, *next;
next = head->next;
while (next != NULL)
{
cur = next;
next = next->next;
free(cur);
cur = NULL;
}
head->next = NULL;
}
struct ListNode * createList()
{
struct ListNode *head = (struct ListNode *)malloc(sizeof(struct ListNode));
if (head == NULL)
{
cout << "malloc failed" << endl;
return head;
}
head->next = NULL;
struct ListNode *pre = head;
for (int i=0; i<10; ++i)
{
//cout<<i<<endl;
struct ListNode *tmp = (struct ListNode *)malloc(sizeof(struct ListNode));
if (tmp == NULL)
{
deleteList(head);
return NULL;
}
tmp->data = i;
tmp->next = NULL;
pre->next = tmp;
pre = tmp;
}
return head;
}
void print(struct ListNode *head)
{
struct ListNode *p = head->next;
while(p != NULL)
{
cout << p->data << " ";
p = p->next;
}
}
int main()
{
struct ListNode *head = createList();
print(head);
deleteList(head);
print(head);
return 0;
}