链表是一种十分常见的数据结构,也是非常灵活的一种数据结构,应用十分广阔,在实际工作会经常用到,尤其保存映射关系时非常方便和灵活,经典的论坛拉链结构就是一种高效的和灵活的链表结构。
#include<iostream>
using namespace std;
struct node
{
int element;
node *next;
};
//创建长度为N的链表
node *create_list(int n)
{
if(n <= 0)
{
return NULL;
}
node *head = NULL;
node *p = new node;
if(p == NULL)
{
return NULL;
}
head = p;
while(--n)
{
node *q = new node;
if(q == NULL)
{
return NULL;
}
p->next = q;
p = q;
}
p->next = NULL;
return head;
}
//求出链表的长度
int list_length(const node* head)
{
int len = 0;
const node *p = NULL;
p = head;
while(p != NULL)
{
len++;
p = p->next;
}
return len;
}
//在第I个节点前面插入新的节点
bool insert_element(node* &a