结果
整体代码

主要实现
- /*
- *实现
- * */
- #include "./linklist.h"
-
- linklist* create_linklist(datatype param){
- linklist* node=(linklist*)malloc(sizeof(linklist));
-
- if(NULL==node){
- puts("节点创建失败");
- }
- node->param=param;
- node->pnext=NULL;
-
- puts("结点创建成功");
-
- return node;
- }
- //头插
- void insert_head_linklist(linklist* head,datatype param){
- linklist* newNode=(linklist*)create_linklist(param);
-
- newNode->pnext=head->pnext;
- head->pnext=newNode;
- head->param++;
- }
- //尾插
- void insert_last_linklist(linklist* head,datatype param){
- linklist* newNode=(linklist*)create_linklist(param);
-
- linklist* p=head;
-
- while(NULL!=p->pnext){
- p=p->pnext;
- }
-
- p->pnext=newNode;
- newNode->pnext=NULL;
- head->param++;
- }
-
- //遍历
- void foreach_linklist(linklist* head){
-
- linklist* p=head;
- puts("-------遍历-------");
- printf("链表总长度:");
- while(NULL!=p->pnext){
- printf("%d \n",p->param);
- p=p->pnext;
- }
- printf("%d \n",p->param);
- puts("-----------------");
-
- }
-
- //卸载堆空间
- void free_null(linklist** node){
- printf("正在卸载%d的堆空间\n",(*node)->param);
- free(*node);
- *node=NULL;
- puts("已卸载堆空间");
- }
- //尾删
- void delete_last_linklist(linklist* head){
-
- if(head == NULL || head->pnext == NULL){
- puts("链表为空或只有头结点");
- return;
- }
-
- linklist* p=head;
-
- while(NULL!=p->pnext->pnext){
- p=p->pnext;
- }
-
- free_null(&(p->pnext));
- head->param--;
- puts("尾删成功");
- }
- //头删
- void delete_head_linklist(linklist* head){
- if(head == NULL || head->pnext == NULL){
- puts("链表为空或只有头结点");
- return;
- }
- linklist* temp=head->pnext;
- head->pnext=head->pnext->pnext;
-
- free_null(&temp);
- head->param--;
- puts("头删成功");
- }
-
- void insert_index_linklist(linklist *head, int index, datatype num)
- {
- if(index > head->param+1 || index <= 0)
- {
- return;
- }
-
- linklist *p = head;
- for(int i=0; i<index-1; i++)
- {
- p = p->pnext;
- }
-
- linklist *temp = (linklist*)malloc(sizeof(linklist));
- temp->param = num;
- temp->pnext = p->pnext;
- p->pnext = temp;
-
- head->param++;
- puts("按位插入成功");
- }
- //指定位置删除节点
- void delete_index_linklist(linklist *head, int index)
- {
- if(NULL == head->pnext || NULL == head || index > head->param+1 || index <= 0)
- {
- return;
- }
- linklist *p = head;
- for(int i=0; i<index-1; i++)
- {
- p = p->pnext;
- }
- linklist *temp = (linklist*)malloc(sizeof(linklist));
- temp = p->pnext;
- p->pnext = temp->pnext;
-
- free_null(&temp);
-
- head->param--;
-
- puts("按位删除成功");
- }