这是单链表的创建 复用的时候需更改模块在注释中有(按需求更改)
其中selectNode()反回的是目标节点的上一个节点,这个函数的返回值可用于其它函数使用
增加节点按顺序调用
initData();
initNode();
insertNode();
删除节点按顺序调用
selectNode();
deleteNode();
查询节点按顺序调用
selectNode();
更改节点按顺序调用
selectNode();
然后更改反回的节点指向下一节点的数据
#include"stdio.h"
#include"stdlib.h"
//节点数据 (需根据要求更改)
typedef struct Data{
} Data;
//节点
typedef struct Node{struct Node* next;struct Data* data;int length;
} Node;
//初始化 反回头节点
Node* initList(){Node* top=(Node*)malloc(sizeof(Node));Node* end=(Node*)malloc(sizeof(Node));end->data=NULL;end->next=NULL;end->length=0;top->data=NULL;top->length=0;top->next=end;return top;
}//初始化数据(需根据要求更改)
Data* initData( char a){Data* nowData=(Data*)malloc(sizeof(Data));return nowData;
}
//初始化一个小结点
Node* initNode(Data* data){Node* node=(Node*)malloc(sizeof(Node));node->data=data;node->length=0;node->next=NULL;return node;
}
//用头插法插入节点 参数列表( 头节点指针,需插入节点指针)
void insertNode(Node* top,Node* node){top->length+=1;node->next=top->next;printf("\n----%c-----\n",*node->data->Name);top->next=node;
}
//查询某一数据反回查询节点的上一个结点 参数列表(头指针,需匹配数据)(需根据要求更改)
Node* selectNode(Node* top){Node* selectNode=top; while(selectNode->next->next!=NULL){if()//数据匹配条件return selectNode;elseselectNode=selectNode->next;}printf("你要的数据不存在\n");
}//删除某一结点 参数列表(需删除节点的上一节点)
void deleteNode(Node* selectNode){Node* deleteNode=selectNode->next;selectNode->next=selectNode->next->next;free(deleteNode);
}//更改某一结点数据 参数列表(需更改节点的上一节点,更改后的数据可利用)
void changeNode(Node* changeNode,Data* data){changeNode->next->data=data;
}
//创建数据节点用于更新数据(需根据要求更改)
Data* createData(char name){Data* data=(Data*)malloc(sizeof(Data));return data;
}
//显示所有数据用于查看程序正常否 参数列表(头节点指针) (需根据要求更改)void showAll(Node* top){Node* selectNode=top;while(selectNode->next!=NULL){if(selectNode->data==NULL){ selectNode=selectNode->next;continue;} //输出数据(更改)selectNode=selectNode->next;} }//主函数(需按要求更改)
int main(){char a='a';Node* top=initList();printf("创建数据\n");Data* nowData=createData(a);Node* nowNode=initNode(nowData);insertNode(top,nowNode);Node* selectedNode=selectNode(top,'a');deleteNode(selectedNode);showAll(top);return 0;
}