include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef char LinkType;
typedef struct LinkNode
{
LinkType data;
struct LinkNode* next;
} LinkNode;
/**
* * @brief 初始化链表
* *
* * @param head
* */
void LinkListInit(LinkNode** head)
{
if(head==NULL)
{
printf("There is no head pointer!");
exit(0);
}
*head=NULL;
}
/**
* * @brief 尾插一个元素到链表中
* *
* * @param head
* * @param value
* */
LinkNode* LinkListPushBack(LinkNode** head, LinkType value)
{
if(head==NULL)
{
printf("There is no head pointer!");
exit(0);
}
if(*head==NULL)
{
*head=(LinkNode*)malloc(sizeof(LinkNode));
(*head)->data=value;
(*head)->next=NULL;
}
else
{
LinkNode *p = *head;
while(p->next!=NULL)
{
p=p->next;
}
LinkNode *tmp=(LinkNode*)malloc(sizeof(LinkNode));
tmp->data=value;
tmp->next=NULL;
p->next=tmp;
}
}
/**
* * @brief 尾删一个元素
* *
* * @param head
* */
void LinkListPopBack(LinkNode** head)
{
if(head==NULL)
{
printf("There is no head pointer!");
exit(0);
}
if(*head==NULL)
{
printf("The head is NULL!");
exit(0);
}
if((*head)->next==NULL)
{
free(*head);
*head=NULL;
}
else
{
LinkNode * p=*head;
while(p->next->next!=NULL)
{
p=p->next;
}
free(p->next);
p->next=NULL;
}
}
/**
* * @brief 头插一个元素
* *
* * @param head
* * @param value
* */
void LinkListPushFront(LinkNode** head, LinkType value)
{
if(head==NULL)
{
printf("There is no head pointer!");
exit(0);
}
if(*head==NULL)
{
*head=(LinkNode*)malloc(sizeof(LinkNode));
(*head)->data=value;
(*head)->next=NULL;
}
else
{
LinkNode* p=(LinkNode*)malloc(sizeof(LinkNode));
p->data=value;
p->next=*head;
*head=p;
}
}
/**
* * @brief 头删一个元素
* *
* * @param head
* */
void LinkListPopFront(LinkNode** head)
{
if(head==NULL)
{
printf("There is no head pointer!");
exit(0);
}
if(*head==NULL)
{
printf("The head is NULL!");
exit(0);
}
if((*head)->next==NULL)
{
free(*head);
*head=NULL;
}
else
{
LinkNode *p=*head;
*head=(*head)->next;
free(p);
p=NULL;
}
}
/**
* * @brief 查找元素在链表中的位置
* *
* * @param head
* * @param to_find 要查找的值
* *
* * @return 这个值对应的节点的地址
* */
LinkNode* LinkListFind(LinkNode* head, LinkType to_find)
{
if(head==NULL)
{
printf("The head is NULL!");
exit(0);
}
while(head!=NULL)
{
if(head->data== to_find)
{
return head;
}
head=head->next;
}
}
/**
* * @brief 在pos之前插入元素
* *
* * @param head
* * @param pos
* * @param value
* */
void LinkListInsert(LinkNode** head, LinkNode* pos, LinkType value)
{
if(head==NULL)
{
printf("There is no head pointer!");
exit(0);
}
if(*head==NULL)
{
printf("The head is NULL!");
exit(0);
}
LinkNode *cur= NULL;
LinkNode *pre= NULL;
for(cur=*head;cur!=NULL;cur=cur->next)
{
if(cur==pos)
{
if(pre!=NULL)
{
LinkNode *p=(LinkNode*)malloc(sizeof(LinkNode));
p->data=value;
pre->next=p;
p->next=cur;
break;
}
else
{
LinkListPushFront(head,value);
break;
}
}
pre=cur;
}
if(cur==NULL)
{
printf("The pos is not in the list!");
exit(0);
}
}
/**
* * @brief 在pos之后插入元素
* *
* * @param head
* * @param pos
* * @param value
* */
void LinkListInsertAfter(LinkNode** head, LinkNode* pos, LinkType value)
{
if(head==NULL)
{
printf("There is no head pointer!");
exit(0);
}
if(*head==NULL)
{
printf("The head is NULL!");
exit(0);
}
LinkNode * cur=NULL;
for(cur=*head;cur!=NULL;cur=cur->next)
{
if(cur==pos)
{
LinkNode *p=(LinkNode*)malloc(sizeof(LinkNode));
p->data=value;
p->next=cur->next;
cur->next=p;
break;
}
}
if(cur==NULL)
{
printf("The pos is not in the headlist!");
exit(0);
}
}
/**
* * @brief 删除指定位置的元素
* */
void LinkListErase(LinkNode** head, LinkNode* pos)
{
if(head==NULL)
{
printf("There is no head pointer!");
exit(0);
}
if(*head==NULL)
{
printf("The head is NULL!");
exit(0);
}
LinkNode*cur=NULL;
LinkNode*pre=NULL;
for(cur=*head;cur!=NULL;cur=cur->next)
{
if(cur==pos)
{
if(pre!=NULL)
{
pre->next=cur->next;
free(cur);
break;
}
else
{
break;
}
}
pre=cur;
}
if(cur==NULL)
{
printf("The pos is not in the headlist!");
exit(0);
}
}
// I don't quite understand what it mean.
// void LinkListErase2(LinkNode** head, LinkNode** pos);
/**
* * @brief 删除指定值的元素
* *
* * @param head
* * @param to_delete
* */
void LinkListRemove(LinkNode** head, LinkType to_delete)
{
if(head==NULL)
{
printf("There is no head pointer!");
exit(0);
}
if(*head==NULL)
{
printf("The head is NULL!");
exit(0);
}
LinkNode *cur=NULL;
LinkNode *pre=NULL;
for(cur=*head;cur!=NULL;cur=cur->next)
{
if(cur->data==to_delete)
{
if(pre!=NULL)
{
pre->next=cur->next;
free(cur);
break;
}
else
{
free(*head);
*head=NULL;
break;
}
}
pre=cur;
cur=cur->next;
}
}
/**
* * @brief 指定值的所有元素都删掉.
* *
* * @param head
* * @param value
* */
void LinkListRemoveAll(LinkNode** head, LinkType value)
{
if(head==NULL)
{
printf("There is no head pointer!");
exit(0);
}
if(*head==NULL)
{
printf("The head is NULL!");
exit(0);
}
LinkNode *cur=NULL;
LinkNode *pre=NULL;
LinkNode *to_delete=NULL;
for(cur=*head;cur!=NULL;cur=cur->next)
{
if(cur->data==value)
{
if(pre!=NULL)
{
pre->next=cur->next;
to_delete=cur;
cur=pre;
free(to_delete);
to_delete=NULL;
}
else
{
free(*head);
*head=NULL;
break;
}
}
pre=cur;
}
}
/**
* * @brief 判定链表为空
* *
* * @return 链表为空, 返回1, 否则返回0
* */
int LinkListEmpty(LinkNode* head)
{
if(head==NULL)
{
return 1;
}
else
{
return 0;
}
}
/**
* * @brief 求链表的元素个数
* *
* * @param head
* *
* * @return
* */
size_t LinkListSize(LinkNode* head)
{
size_t count=0;
while(head!=NULL)
{
count++;
head=head->next;
}
return count;
}
/**
* * @brief 逆序打印单链表.
* *
* * @param head
* */
void LinkListReversePrint(LinkNode* head)
{
if(head==NULL)
{
printf("The linklist is NULL!");
exit(0);
}
if(head->next!=NULL)
{
LinkListReversePrint(head->next);
}
printf("%c->",head->data);
}
/**
* * @brief 不允许遍历链表, 在 pos之前插入
* *
* * @param head
* * @param pos
* * @param value
* */
void LinkListInsertBefore(LinkNode** head, LinkNode* pos, LinkType value)
{
if(head==NULL)
{
return ;
}
if(*head==NULL)
{
printf("The head is NULL!");
exit(0);
}
LinkNode* p=(LinkNode*)malloc(sizeof(LinkNode));
p->data=pos->data;
pos->data=value;
p->next=pos->next;
pos->next=p;
}
LinkNode* JosephCycle(LinkNode* head, size_t food)
{
if(head==NULL)
{
printf("The head linklist is NULL!");
return;
}
LinkNode*cur=head;
LinkNode*to_delete=NULL;
size_t i;
while(cur->next!=cur)
{
for(i=1;i<food;i++)
{
cur=cur->next;
}
cur->data=cur->next->data;
to_delete=cur->next;
cur->next=to_delete->next;
free(to_delete);
to_delete=NULL;
}
return cur;
}
int main()
{
LinkNode*p;
LinkNode*tmp;
LinkListInit(&p);
LinkListPushBack(&p,'b');
LinkListPushFront(&p,'a');
LinkListPushBack(&p,'c');
LinkListPushFront(&p,'x');
LinkListPopBack(&p);
LinkListPopFront(&p);
LinkListInsert(&p, p->next,'c');
tmp=LinkListFind(p, 'a');
printf(" %c ",tmp->data);
LinkListErase(&p,p->next );
LinkListInsertAfter(&p, p->next, 'z');
LinkListRemove(&p, 'z');
LinkListPushFront(&p,'a');
LinkListPushFront(&p,'a');
LinkListPushFront(&p,'b');
LinkListPushFront(&p,'b');
LinkListPushFront(&p,'b');
LinkListPushFront(&p,'b');
int i=LinkListEmpty(p);
if(i!=1)
{
printf("The list is not empty!\n");
}
LinkListRemoveAll(&p, 'a');
i=LinkListSize(p);
printf("There is %d node in the linkelist.\n",i);
LinkListReversePrint(p);
LinkListInsertBefore(&p, p->next,'a');
LinkListPushFront(&p,'a');
LinkListPushFront(&p,'a');
printf("\n");
LinkListReversePrint(p);
LinkNode *end=p;
while(end->next!=NULL)
{
end=end->next;
}
end->next=p;
printf("\n");
LinkNode*cur= JosephCycle(p, 2);
printf("The serviver is %c \n.",cur->data);
}
单链表操作
最新推荐文章于 2023-05-07 21:40:08 发布