loop_list单向循环列表

#include "loop_list.h"

//创建单向循环链表

loop_p create_head()

{

loop_p L=(loop_p)malloc(sizeof(loop_list));

if(L==NULL)

{

printf("create fail\n");

return NULL;

}

L->len = 0;

L->next=L;

return L;

}

//创建节点

loop_p create_node(datatype data)

{

loop_p new=(loop_p)malloc(sizeof(loop_list));

if(new==NULL)

{

printf("create fail\n");

return NULL;

}

new->data=data;

return new;

}

//头插

void insert_head(loop_p L,datatype data)

{

if(L==NULL)

{

printf("data fail\n");

return;

}

loop_p new=create_node(data);

new->next=L->next;

L->next=new;

L->len++;

}

//输出

void out_put_loop(loop_p L)

{

loop_p p=L->next;

while(p!=L)

{

printf("%d--->\n",p->data);

p=p->next;

}

putchar(10);

}

//按位置插入

void insert_pos(loop_p L,datatype data,int pos)

{

loop_p new=create_node(data);

loop_p p=L;

for(int i=0;i<pos-1;i++)

{

p=p->next;

}

new->next=p->next;

p->next=new;

L->len++;

}

//尾删

void del_tail(loop_p L)

{

loop_p p=L;

while(p->next->next!=L)

{

p=p->next;

}

loop_p del=p->next;

p->next=L;

free(del);

L->len--;

}

//按位置删除

int del_pos(loop_p L,datatype pos)

{

loop_p p=L;

for(int i=0;i<pos-1;i++)

{

p=p->next;

}

loop_p del = p->next;

p->next=p->next->next;

free(del);

L->len--;

}

#ifndef LOOP_LIST_H

#define LOOP_LIST_H

#include <stdio.h>

#include <stdlib.h>

typedef int datatype;

typedef struct loop_list

{

union

{

int len;

datatype data;

};

struct loop_list *next;

}loop_list,*loop_p;

//创建单向循环链表

loop_p create_head();

//创建节点

loop_p create_node(datatype data);

//头插

void insert_head(loop_p L,datatype data);

//按位置插入

void insert_pos(loop_p L,datatype data,int pos);

//输出(看现象)

void out_put_loop(loop_p L);

//尾删

void del_tail(loop_p L);

//按位置删除

int del_pos(loop_p L,datatype pos);

#endif

#include "loop_list.h"

int main()

{

loop_p L=create_head();

insert_head(L,1);

insert_head(L,2);

insert_head(L,3);

insert_pos(L,4,2);

//del_tail(L);

del_pos(L,2);

out_put_loop(L);

return 0;

}

相关推荐
丘山望岳34 分钟前
哈希——数据分类存储柜
数据结构·散列表
WL学习笔记3 小时前
链式队列(数据结构)
前端·数据结构·jquery
Darkwanderor4 小时前
Linux进程优先级操作
linux·运维·c语言·c++
智者知已应修善业5 小时前
【P12159蓝桥杯数组翻转】2025-4-24
c语言·c++·经验分享·笔记·算法·蓝桥杯
SuperByteMaster5 小时前
keil link misc controls
c语言
人道领域5 小时前
【LeetCode刷题日记】贪心算法理论与实战:455.分发饼干最优解
java·开发语言·数据结构·算法·leetcode·贪心算法
小巧的蜡烛6 小时前
对CSS中的Position、Float属性的一些深入探讨
数据结构·mysql
疋瓞19 小时前
python和C++对比(1)_数据类型和数据结构
数据结构·c++·python
ALex_zry20 小时前
C++26 std::complex 结构化绑定详解:auto [re, im] = c
c语言·开发语言·c++
如此这般英俊21 小时前
手搓Claude Code-第六章 subagent
数据结构·人工智能·python·语言模型·自然语言处理