struct node {//建立node结构体
int data;
node* next;
};
node* creatHeadNode(node* n) {
n = new node;
n->next = NULL;
return n;
}
void headInsert(node* n,int data) {
node* nn = new node;
nn->data = data;
nn->next = n->next;
n->next = nn;
}
void tailInsert(node* n, int data) {
node* tail = n;
node* nn = new node;
nn->next = NULL;
nn->data = data;
if (tail->next == NULL) {
tail->next = nn;
}
else {
while (tail->next != NULL) {
tail = tail->next;
}
tail->next = nn;
}
}
void printList(node* n) {
node* current = n;
while (current->next!=NULL) {
current = current->next;
cout << "data: " << current->data << endl;
}
}
int main() {
node* preList = new node;
preList->next = NULL;
//node* preList = creatHeadNode(preListT);
headInsert(preList, 1);
headInsert(preList, 2);
headInsert(preList, 3);
headInsert(preList, 4);
headInsert(preList, 5);
printList(preList);
cout << "end-------------------------------------"<next = NULL;
tailInsert(tailList, 1);
tailInsert(tailList, 2);
tailInsert(tailList, 3);
tailInsert(tailList, 4);
tailInsert(tailList, 5);
printList(tailList);
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