1、单链表结构
typedef struct ListNode {
int data;
ListNode *next;
} ListNode;
2、单链表创建
//创建一个单链表
ListNode *create_list() {
int len = 0;
ListNode *head, *p, *q;
int data;
head = (ListNode*)malloc(sizeof(ListNode));
q = head;
while(1) {
cout << "Please input the data: " ;
cin >> data;
if(data == 0) break;
p = (ListNode*)malloc(sizeof(ListNode));
p->data = data;
q->next = p;
q = p;
}
q->next = NULL;
return head;
}
3、单链表打印
//单链表的打印
void print_list(ListNode *head) {
ListNode *p = head->next;
if(p == NULL) {
cout << "List is empty." << endl;
return;
}
while(p != NULL) {
cout << p->data;
p = p->next;
if(p != NULL) cout << " -> ";
}
cout << endl;
}
4、单链表长度
//测试单链表的长度
int length_list(ListNode *head) {
ListNode *p = head->next;
int len = 0;
while(p != NULL) {
len++;
p = p->next;
}
return len;
}
5、单链表查找
//单链表的查找
ListNode *search_list(ListNode *head, int pos) {
ListNode *p = head->next;
if(pos < 0) {
cout << "Incorrect postion to search list." << endl;
return NULL;
}
if(pos == 0) {
return head;
}
if(p == NULL) {
cout << "List is empty." << endl;
return NULL;
}
while(--pos) {
if((p = p->next) == NULL) {
cout << "Incorrenct postion to search list." << endl;
return NULL;
}
}
return p;
}
6、单链表插入
//单链表的插入
ListNode *insert_list(ListNode *head, int pos, int data) {
ListNode *p, *item;
item = (ListNode*)malloc(sizeof(ListNode));
item->data = data;
if(pos == 0) {
item->next = head->next;
head->next = item;
return head;
}
p = search_list(head, pos);
if(p != NULL) {
item->next = p->next;
p->next = item;
}
return head;
}
7、单链表删除
//单链表的删除
ListNode *delete_list(ListNode *head, int pos) {
ListNode *p = head->next;
ListNode *item = NULL;
if(p == NULL) {
cout << "List is empty." << endl;
return NULL;
}
p = search_list(head, pos - 1);
if(p != NULL && p->next != NULL) {
item = p->next;
p->next = item->next;
free(item);
}
return head;
}
8、单链表逆置
//将单链表逆置
//例如原链表为: 1->2->3->4;逆置后为: 4->3->2->1
ListNode *reverse_list(ListNode *head) {
ListNode *p, *q;
if(head->next == NULL) {
return head;
}
p = head->next;
q = p->next;
p->next = NULL;
while(q != NULL) {
ListNode *r = q->next;
q->next = head->next;
head->next = q;
q = r;
}
return head;
}
9、单链表查找中间值
//查找单链表中间的元素,只需要一次遍历
ListNode *search_middle(ListNode *head) {
ListNode *current = NULL;
ListNode *middle = NULL;
int i = 0, j = 0;
current = middle = head->next;
while(current != NULL) {
if(j < i / 2) {
j++;
middle = middle->next;
}
i++;
current = current->next;
}
return middle;
}
10、单链表测试
int main()
{
int pos, data;
ListNode *head, *item;
head = create_list();
cout << endl;
print_list(head);
cout << endl;
int len = length_list(head);
cout << "The length is : " << len << endl;
cout << endl;
cout << "Please input search pos: ";
cin >> pos;
item = search_list(head, pos);
if(item != NULL) {
cout << "The pos " << pos << "'th data is: " << item->data << endl;
}
cout << endl;
cout << "The input insert pos and data is: ";
cin >> pos >> data;
head = insert_list(head, pos, data);
print_list(head);
cout << endl;
cout << "Please input the delete pos: ";
cin >> pos;
head = delete_list(head, pos);
print_list(head);
head = reverse_list(head);
print_list(head);
item = search_middle(head);
cout << "The middle data is : " << item->data << endl;
}