#include <bits/stdc++.h>
using namespace std;
struct node
{
int data; // 数据
node* next; // 指针
};
node* create(int Array[])
{
node *p, *pre, *head; // head头节点, pre当前节点的前驱节点,p当前节点
head = new node;
head->next = NULL;
pre = head;
for (int i = 0; i < 5; i++)
{
// 创建新节点
p = new node;
// 将Array[i]赋给新建的节点作为数据域,也可以scanf输入
p->data = Array[i];
p->next = NULL;
pre->next = p;
// 更新
pre = p;
}
return head;
}
int search(node* head, int x)
{
int count = 0;
node* p = head->next;
while (p != NULL)
{
if(p->data == x)
{
count++;
}
p = p->next;
}
return count;
}
void insert(node* head, int pos, int x)
{
node* p = head; // 保护头指针
// 找到位置
for(int i = 0; i < pos - 1; i++)
{
p = p->next; // pos - 1是为了找到位置的前一个节点
}
node* q = new node;
q->data = x;
q->next = p->next;
p->next = q;
}
void del(node* head, int x)
{
node* p = head->next;
node* pre = head;
while(p != NULL)
{
if(p->data == x)
{
pre->next = p->next;
delete(p);
}
else
{
pre = p;
p = p->next;
}
}
}
int main()
{
int Array[5] = {5, 3, 6, 1, 2};
node* L = create(Array);
L = L->next;
while(L != NULL)
{
printf("%d", L->data);
L = L->next;
}
system("pause");
}
原文:https://www.cnblogs.com/kinologic/p/14702058.html