链表简单功能的总结

下面我将通过创建链表,实现链表的增删,打印,对链表进行复习

头文件:

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef struct ListNode 
{
    int data;
    struct ListNode* next;
}SLI;
SLI* create(int x);
void ADDFRONT( SLI** head , int x );
void PRINT( SLI* head );
void ADDTAIL( SLI** head ,int x );
void DELETEFRONT( SLI** head );
void DELETETAIL( SLI** head );
SLI* FIND( SLI* head ,int x );
void ADD_SPECIFICFRONT(SLI** head , SLI* pos ,int x );
void ADD_SPECIFICTAIL(SLI** head , SLI* pos ,int x );
void DELETE_SPECIFIC( SLI** head , SLI* pos );

具体实现的函数:

#include "SList.h"

SLI* create(int x)
{
    SLI* pf = (SLI*)malloc(sizeof(SLI));
    if( pf == NULL )
    {
        perror("fail");
        exit(1);
    }
    pf->data = x;
    pf->next = NULL;
    return pf;
}
void ADDFRONT( SLI** head , int x )
{
    SLI* pf = create(x);
    if( (*head) == NULL )
        *head = pf;
    else
    {
    pf->next = *head;
    *head = pf;
    }
}
void PRINT( SLI* head )
{
    while( head )
    {
        printf("%d->",head->data);
        head = head->next ;
    }
    printf("NULL\n");
}
SLI* FINDTAIL(SLI** head)
{
    SLI* pf = *head;
    while( pf->next != NULL )
    {
        pf = pf->next;
    }
    return pf;
}
void ADDTAIL( SLI** head ,int x )
{
    SLI* pf = create(x);
    SLI* tail = NULL;
    
    assert(head);
    if( *head == NULL )
    {
        *head = pf;
    }
    else
    {
    tail = FINDTAIL(head);
    tail->next = pf;
    }
}

void DELETEFRONT( SLI** head )
{
    SLI* pf = (*head)->next;
    assert(head);
    assert(*head);
    free(*head);
    *head = pf;
}
void DELETETAIL( SLI** head )
{
    SLI* pf = *head;
    assert(head);
    assert(*head);
    if( (*head)->next == NULL )
    {
        return;
    }
    else
    {
        while( pf->next->next != NULL )
        {
            pf = pf->next ;
        }
        pf->next = NULL;
        return;
    }
}
void ADD_SPECIFICFRONT(SLI** head , SLI* pos ,int x )
{
    SLI* a = *head;
    SLI* pf = create(x);
    assert(head);
    assert(*head);
    assert(pos);
    if( pos == *head )//相当与头插
    {
        ADDFRONT( head , x );
        return;
    }
    while( (*head)->next != pos )
    {
        *head = (*head)->next ;
    }
    pf->next = (*head)->next ;
    (*head)->next = pf;
    *head = a;
}
SLI* FIND( SLI* head ,int x )
{
    while( head )
    {
        if(head->data == x)
            return head;
        head = head->next ;
    }
    return NULL;
}
void ADD_SPECIFICTAIL(SLI** head , SLI* pos ,int x )
{
    SLI* pf = create(x);
    if( pos->next == NULL )
    {
        pos->next = pf;
        return;
    }
    else
    {
        pf->next = pos->next ;
        pos->next = pf;
        return;
    }
}
void DELETE_SPECIFIC( SLI** head , SLI* pos  )
{
    SLI* pf = *head;
    if( *head == pos )//当pos为首节点
    {
        *head = (*head)->next ;
        free(pf);
        pf = NULL;
        return;
    }
    while( pf->next != pos )
    {
        pf = pf->next;
    }
    pf->next = pf->next->next ;
}

测试函数:这里可以随意实现

#include "SList.h"
int main()
{
    SLI* pf;
    SLI* head = NULL;
    //ADDFRONT( &head , 1 );
    //ADDFRONT( &head , 2 );
    //ADDFRONT( &head , 3 );
    ADDTAIL( &head ,1);
    ADDTAIL( &head ,2);
    ADDTAIL( &head ,3);
    pf = FIND( head ,1 );
    DELETE_SPECIFIC(&head,pf);
    //ADD_SPECIFICTAIL(&head , pf ,2 );
    PRINT(  head );
    /*PRINT(  head );
    DELETETAIL( &head );
    DELETEFRONT( &head );
    PRINT(  head );*/
}

相关推荐

  1. 简单功能总结

    2024-03-11 00:50:02       36 阅读
  2. --简单学习

    2024-03-11 00:50:02       54 阅读
  3. 【数据结构和算法】简单实现

    2024-03-11 00:50:02       64 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-11 00:50:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-11 00:50:02       101 阅读
  3. 在Django里面运行非项目文件

    2024-03-11 00:50:02       82 阅读
  4. Python语言-面向对象

    2024-03-11 00:50:02       91 阅读

热门阅读

  1. Ubuntu设置时区和时间同步

    2024-03-11 00:50:02       53 阅读
  2. 【国产MCU】-窗口看门狗(WWDG)

    2024-03-11 00:50:02       46 阅读
  3. SQL 数据库安全的基本概念和技术

    2024-03-11 00:50:02       36 阅读
  4. php开发100问?

    2024-03-11 00:50:02       37 阅读
  5. Day 7.UDP编程、不同主机之间用网络进行通信

    2024-03-11 00:50:02       32 阅读
  6. HTML基础

    2024-03-11 00:50:02       47 阅读
  7. 使用SpringContextHolder获取 Spring 容器中的 Bean

    2024-03-11 00:50:02       47 阅读
  8. 全局Ceph节点宕机处理

    2024-03-11 00:50:02       43 阅读