这里给大家一些可以直接放在头文件里使用的链表函数,需要用链表的时候直接调用就可以
以后会继续更新的~
//链表
typedef struct Node
{
struct Node* next;
int data;
int num;
}NODE,*LIST,*LPNODE;
//创建链表
LIST CreateHead()
{
LIST headNode = (LIST)malloc(sizeof(NODE));
assert(headNode);
headNode->next = NULL;
return headNode;
};
//创建节点
LIST CreateNode(LIST headnode,int data)
{
LIST newnode=(LIST)malloc(sizeof(NODE));
newnode->data=data;
newnode->next=NULL;
return newnode;
};
//头插入节点
void inserthead(LIST headnode ,int data)
{
LIST newnode=CreateNode(headnode,data);
newnode->next=headnode->next;
headnode->next=newnode;
headnode->num++;
};
//尾插入节点
void insertback(LIST headnode ,int data)
{
LIST newnode=CreateNode(headnode,data);
LIST pmove=headnode;
while(pmove->next!=NULL)
{
pmove=pmove->next;
}
pmove->next=newnode;
headnode->num++;
}
//中间插入节点
void insertcenter(LIST headnode ,int data,int postdata)
{
LIST newnode=CreateNode(headnode,data);
LIST left=headnode;
LIST right=headnode->next;
while(left!=NULL&&right->data!=postdata)
{
left=right;
right=right->next;
}
if(right==NULL)
{
printf("insert error");
}else
{
left->next=newnode;
newnode->next=right;
}
headnode->num++;
}
//头删除节点
void delhead(LIST headnode)
{
LIST temp=headnode->next;
if (temp == NULL)
{
printf("Node NULL!\n");
return;
}
headnode->next=temp->next;
free(temp);
headnode->num--;
}
//尾删除节点
void delback(LIST headnode)
{
LPNODE left = headnode;
LPNODE right = headnode->next;
while (left != NULL && right->next != NULL)
{
left = right;
right = right->next;
}
if (right == NULL)
{
printf("Node NULL");
}
else
{
free(right);
right = NULL;
left->next = NULL;
}
headnode->num--;
}
//中间删除节点
void delcenter(LIST headnode,int postdata)
{
LIST left=headnode;
LIST right=headnode->next;
while(left!=NULL&&right->data!=postdata)
{
left=right;
right=right->next;
}
if(right==NULL)
{
printf("no find");
}
else
{
LIST temp=left->next;
left->next=right;
free(temp);
temp = NULL;
}
headnode->num--;
}
//打印链表
void printList(LIST headNode)
{
if (headNode == NULL)
{
printf("ERROR\n");
return;
}
//NULL是没有next
LPNODE pmove = headNode->next;
while (pmove != NULL)
{
printf("%d\t", pmove->data);
pmove = pmove->next;
}
printf("\n");
}
//对比两个链表
void listcat(LIST first, LIST second)
{
LIST pmove = second->next;
while (pmove != NULL)
{
insertback(first, pmove->data);
pmove = pmove->next;
}
}
void listcat_v(LIST first, LIST second, LIST third)
{
LIST pFirst = first->next;
while (pFirst != NULL)
{
LIST pSecond = second->next;
while (pSecond != NULL)
{
if (pFirst->data == pSecond->data)
{
insertback(third,pFirst->data);
break;
}
pSecond = pSecond->next;
}
pFirst = pFirst->next;
}
}
void reList(LIST* headNode)
{
LIST preNode = NULL;
LIST curNode = (*headNode)->next;
LIST nextNode = (*headNode)->next;
while (curNode != NULL)
{
nextNode = curNode->next;
curNode->next = preNode;
preNode = curNode;
curNode = nextNode;
}
(*headNode)->next=preNode;
}
//判断链表中指定值的个数(头节点,指定值)
int CountX(LPNODE headNode,int n) {
LPNODE newNode = headNode;
int c = 0;
for (int i = 0; i < headNode->num; ++i) {
if (newNode->data == n) {
c++;
}
if (newNode->next != NULL) {
newNode = newNode->next;
} else {
return c;
}
}
}
//逆置
LPNODE nizhi(LPNODE headNode)
{
LPNODE preNode=headNode;
LPNODE seNode=headNode->next;
LPNODE lastNode=seNode->next;
while (lastNode!=NULL)
{
seNode->next=preNode;
lastNode= lastNode->next;
seNode=seNode->next;
preNode=preNode->next;
}
headNode->next=seNode;
}