链表简单功能的总结

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

头文件:

#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 );*/

}

相关推荐
CQ_071216 分钟前
自学力扣:最长连续序列
数据结构·算法·leetcode
弥彦_32 分钟前
cf1925B&C
数据结构·算法
好易学·数据结构10 小时前
可视化图解算法56:岛屿数量
数据结构·算法·leetcode·力扣·回溯·牛客网
Ashlee_code15 小时前
裂变时刻:全球关税重构下的券商交易系统跃迁路线图(2025-2027)
java·大数据·数据结构·python·云原生·区块链·perl
闻缺陷则喜何志丹16 小时前
【带权的并集查找】 P9235 [蓝桥杯 2023 省 A] 网络稳定性|省选-
数据结构·c++·蓝桥杯·洛谷·并集查找
jie*16 小时前
python(one day)——春水碧于天,画船听雨眠。
开发语言·数据结构·python·算法·线性回归
草莓熊Lotso18 小时前
【LeetCode刷题指南】--数组串联,合并两个有序数组,删除有序数组中的重复项
c语言·数据结构·其他·刷题
weixin_4196583118 小时前
数据结构之B-树
java·数据结构·b树
H_HX_xL_L18 小时前
数据结构的算法分析与线性表<1>
数据结构·算法
overFitBrain18 小时前
数据结构-2(链表)
数据结构