写了好久的代码终于能够连着测试代码一起编译了.约瑟夫环留到下次写,略略略
先粘贴上头文件,功能在头文件都能看到,我就不一一写了
#pragma once
typedef char LinkNodeType;
typedef struct LinkNode{
LinkNodeType data;
struct LinkNode *next;
}LinkNode;
typedef struct LinkList{
LinkNode *head;
LinkNode *tail;
}LinkList;
typedef LinkNode *PLinkNode;
void PrintChar(LinkNode *head,const char *msg);
LinkNode* CreateNode(LinkNodeType value);
void LinkListInit(LinkNode** head);
void LinkListPushBack(LinkNode** head,LinkNodeType value);
void LinkListPopBack(LinkNode **head);
void DestoryNode(LinkNode *node);
void LinkListPushFront(LinkNode **head,LinkNodeType value);
void LinkListPopFront(LinkNode **head);
LinkNode* LinkListFind(LinkNode *head,LinkNodeType to_find);
void LinkListInsertBefore(LinkNode** head,LinkNode
*pos,LinkNodeType value);
void LinkListErase(LinkNode **head,LinkNode *pos);
void LinkListErase2(LinkNode **head,LinkNode *pos);
void LinkListRemove(LinkNode **head,LinkNodeType to_delete);
void LinkListRemoveAll(LinkNode **head,LinkNodeType value);
int LinkListEmpty(LinkNode *head);
size_t LinkListSize(LinkNode *head);
void LinkListReversePrint(LinkNode *head);
void LinkListInsertBefore2(LinkNode **head,LinkNode
*pos,LinkNodeType value);
LinkNode* JosephCycle(LinkNode *head,size_t food);
接下来就是函数实现,我的测试函数和实现函数是从中间往外扩散的结构。可能看起来有点糊
#include<stdio.h>
#include"LinkList.h"
#include<stdlib.h>
#define TestHeader printf("\n=========%s========\n",__FUNCTION__)
void LinkListInit(LinkNode** head){
*head = NULL;
}
void PrintChar(LinkNode *head,const char *msg){
printf("[%s]\n",msg);
LinkNode *cur = head;//TODO
for(;cur !=NULL;cur=cur->next)
{
printf("[%c]|%p ",cur->data,cur);
}
printf("\n");
}
LinkNode* CreateNode(LinkNodeType value)
{
LinkNode *new_node=(LinkNode*)malloc(sizeof(LinkNode));
new_node->data=value;
new_node->next=NULL;
}
void DestoryNode(LinkNode *node){
free(node);
}
void LinkListPushBack(LinkNode **head,LinkNodeType value){
if(head==NULL){
return;
}
if(*head == NULL){
*head=CreateNode(value);
}
LinkNode *cur=*head;
while(cur->next != NULL){
cur = cur->next;
}
LinkNode *new_node = CreateNode(value);
cur->next=new_node;
}
void LinkListPopBack(LinkNode **head){
if(head==NULL){
return;
}
if(*head==NULL){
return;
}
if((*head)->next == NULL){
DestoryNode(*head);
return;
}
LinkNode *cur=*head;
LinkNode *pre=NULL;
while(cur->next != NULL){
pre=cur;
cur=cur->next;
}
pre->next=NULL;
DestoryNode(cur);
return;
}
void LinkListPushFront(LinkNode **head,LinkNodeType value){
if(head==NULL){
return;
}
if(*head==NULL){//emptylist
LinkListPushBack(head,value);
}
LinkNode *new_node=CreateNode(value);
new_node->next=*head;
*head=new_node;
}
void LinkListPopFront(LinkNode **head){
if(head==NULL){
return;
}
if(*head==NULL){
return;
}
LinkNode *to_delete=*head;
*head=(*head)->next;
DestoryNode(to_delete);
}
LinkNode* LinkListFind(LinkNode *head,LinkNodeType to_find){
if(head==NULL){
return NULL;
}
LinkNode *cur=head;
while(cur->next != NULL){
if(cur->data == to_find){
break;
return NULL;
}
cur=cur->next;
}
return cur;
}
void LinkListInsertAfter(LinkNode **head,LinkNode *pos,LinkNodeType value){
if(*head==NULL){
return;
}
if(pos==NULL){
return;//inlegal input
}
LinkNode *new_node=CreateNode(value);
new_node->next=pos->next;
pos->next=new_node;
}
void LinkListInsertBefore(LinkNode **head,LinkNode *pos,LinkNodeType value){
if(*head == NULL){
return;
}
if(pos == NULL){
return;
}
if(*head == pos){
LinkListPushFront(head,value);
return;
}
LinkNode *cur=*head;
for(;cur->next != NULL;cur = cur->next){
if(cur == pos){
break;
}
if(cur == NULL){
return;
}//pos not in list
}
LinkListInsertAfter(head,cur,value);
return;
}
void LinkListErase(LinkNode **head,LinkNode *pos){
if(*head == NULL || pos==NULL){
return;
}
if(head == NULL){
return;
}
LinkNode *cur = *head;
LinkNode *pre =NULL;
LinkNode *to_erase = NULL;
for(;cur!= NULL;pre = cur,cur= cur->next){
if(cur == pos){
break;
}
if(cur == NULL){
return;
}
}
to_erase=cur;
pre->next=to_erase->next;;
DestoryNode(to_erase);
return;
}
void LinkListErase2(LinkNode **head,LinkNode *pos){
if(pos==NULL || head==NULL){
return;
}
if(*head== NULL){
return;
}
if(pos->next == NULL){
LinkListErase(head,pos);
return;
}
pos->data=pos->next->data;
LinkNode *to_erase = pos->next;
pos->next=to_erase->next;
DestoryNode(to_erase);
return;
}
void LinkListRemove(LinkNode **head,LinkNodeType to_delete){
if(head==NULL){
return;
}
if(*head == NULL){
return;
}
if((*head)->data == to_delete){
LinkNode *to_delete = *head;
(*head)=(*head)->next;
DestoryNode(to_delete);
return;
}
LinkNode *cur = *head;
for(;cur->next!=NULL;cur=cur->next){
if(cur->data == to_delete){
LinkNode *to_delete=cur;
cur->next=to_delete->next;
DestoryNode(to_delete);
return;
}
}
return;
}
void LinkListRemoveAll(LinkNode **head,LinkNodeType value){
if(head == NULL){
return;
}
if(*head == NULL){
return;
}
LinkNode *cur = *head;
for(;cur->next!=NULL;cur=cur->next){
if(cur->data == value){
LinkListRemove(head,value);
}
}
return;
}
int LinkListEmpty(LinkNode *head){
return head==NULL?1:0;
}
size_t LinkListSize(LinkNode *head){
if(head == NULL){
return 0;
}
size_t count=1;
LinkNode *cur=head;
for(;cur->next != NULL;cur= cur->next){
count++;
}
return count;
}
void LinkListReversePrint(LinkNode *head){
if(head == NULL){
return;
}
LinkListReversePrint(head->next);
printf("[%c]%p",head->data,head);
}
void LinkListInsertBefore2(LinkNode **head,LinkNode *pos,LinkNodeType value){
if(*head == NULL || pos == NULL){
return;
}
LinkNode *new_node=CreateNode(pos->data);
LinkNode *after_new_node=pos->next;
pos->next=new_node;
new_node->next=after_new_node;
pos->data=value;
return;
}
LinkNode* JosephCycle(LinkNode *head,size_t food){
if(head == NULL){
return;
}
}
void TestInsertBefore2(){
TestHeader;
LinkNode *head;
LinkListInit(&head);
LinkListPushBack(&head,'a');
LinkListPushBack(&head,'b');
LinkListPushBack(&head,'c');
LinkListPushBack(&head,'d');
LinkNode *pos=head->next->next->next;
LinkListInsertBefore2(&head,pos,'x');
PrintChar(head,"\n");
}
void TestReversePrint(){
TestHeader;
LinkNode *head;
LinkListInit(&head);
LinkListPushBack(&head,'a');
LinkListPushBack(&head,'b');
LinkListPushBack(&head,'c');
LinkListPushBack(&head,'d');
LinkListReversePrint(head);
printf("\n");
}
void TestSize(){
TestHeader;
LinkNode *head;
LinkListInit(&head);
LinkListPushBack(&head,'a');
LinkListPushBack(&head,'a');
LinkListPushBack(&head,'a');
LinkListPushBack(&head,'d');
int len=LinkListSize(head);
printf("the size is %d\n",len);
}
void TestEmpty(){
TestHeader;
LinkNode *head;
int ret=LinkListEmpty(head);
printf("result is :%d\n",ret);
}
void TestRemoveAll(){
TestHeader;
LinkNode *head;
LinkListInit(&head);
LinkListPushBack(&head,'a');
LinkListPushBack(&head,'a');
LinkListPushBack(&head,'a');
LinkListPushBack(&head,'d');
LinkListRemoveAll(&head,'a');
PrintChar(head,"\n");
}
void TestRemove(){
TestHeader;
LinkNode *head;
LinkListInit(&head);
LinkListPushBack(&head,'a');
LinkListPushBack(&head,'b');
LinkListPushBack(&head,'c');
LinkListPushBack(&head,'d');
LinkListRemove(&head,'b');
PrintChar(head,"\n");
}
void TestErase2(){
TestHeader;
LinkNode *head;
LinkListInit(&head);
LinkListPushBack(&head,'a');
LinkListPushBack(&head,'b');
LinkListPushBack(&head,'c');
LinkListPushBack(&head,'d');
LinkNode *pos = head->next->next;
LinkListErase2(&head,pos);
PrintChar(head,"\n");
}
void TestErase(){
TestHeader;
LinkNode *head;
LinkListInit(&head);
LinkListPushBack(&head,'a');
LinkListPushBack(&head,'b');
LinkListPushBack(&head,'c');
LinkListPushBack(&head,'d');
LinkNode *pos = head->next->next;
LinkListErase(&head,pos);
PrintChar(head,"\n");
}
void TestInsertBefore(){
TestHeader;
LinkNode *head;
LinkListInit(&head);
LinkListPushBack(&head,'a');
LinkListPushBack(&head,'b');
LinkListPushBack(&head,'c');
LinkListPushBack(&head,'d');
LinkNode *pos=head->next;
LinkListInsertBefore(&head,pos,'H');
PrintChar(head,"\n");
}
void TestInsertAfter(){
TestHeader;
LinkNode *head;
LinkListInit(&head);
LinkListPushBack(&head,'a');
LinkListPushBack(&head,'b');
LinkListPushBack(&head,'c');
LinkListPushBack(&head,'d');
LinkNode *pos=head->next;
LinkListInsertAfter(&head,pos,'o');
PrintChar(head,"\n");
}
void TestFind(){
TestHeader;
LinkNode *head;
LinkListInit(&head);
LinkListPushBack(&head,'a');
LinkListPushBack(&head,'b');
LinkListPushBack(&head,'c');
LinkListPushBack(&head,'d');
LinkNode *dest=LinkListFind(head,'c');
PrintChar(head,"\n");
printf("the address of c is[%p]\n",dest);
}
void TestPopFront(){
TestHeader;
LinkNode *head;
LinkListInit(&head);
LinkListPushBack(&head,'a');
LinkListPushBack(&head,'b');
LinkListPushBack(&head,'c');
LinkListPushBack(&head,'d');
LinkListPopFront(&head);
LinkListPopFront(&head);
PrintChar(head,"\n");
}
void TestPushFront(){
TestHeader;
LinkNode *head;
LinkListInit(&head);
LinkListPushBack(&head,'a');
LinkListPushBack(&head,'b');
LinkListPushBack(&head,'c');
LinkListPushBack(&head,'d');
LinkListPushFront(&head,'x');
LinkListPushFront(&head,'y');
PrintChar(head,"\n");
}
void TestPopBack(){
TestHeader;
LinkNode *head;
LinkListInit(&head);
LinkListPushBack(&head,'a');
LinkListPushBack(&head,'b');
LinkListPushBack(&head,'c');
LinkListPushBack(&head,'d');
LinkListPopBack(&head);
LinkListPopBack(&head);
PrintChar(head,"\n");
}
void TestPushBack(){
TestHeader;
LinkNode *head;
LinkListInit(&head);
LinkListPushBack(&head,'a');
LinkListPushBack(&head,'b');
LinkListPushBack(&head,'c');
LinkListPushBack(&head,'d');
PrintChar(head,"\n");
}
void TestInit(){
TestHeader;
LinkNode *head;
LinkListInit(&head);
}
void main(){
TestInit();
TestPushBack();
TestPopBack();
TestPushFront();
TestPopFront();
TestFind();
TestInsertAfter();
TestErase();
TestErase2();
TestRemove();
TestRemoveAll();
TestEmpty();
TestSize();
TestReversePrint();
TestInsertBefore2();
}