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;

}

相关推荐
迈巴赫车主1 小时前
蓝桥杯20560逃离高塔
java·开发语言·数据结构·算法·职场和发展·蓝桥杯
平凡灵感码头2 小时前
C语言 printf 数据打印格式速查表
c语言·开发语言·算法
x_xbx2 小时前
LeetCode:1. 两数之和
数据结构·算法·leetcode
玲娜贝儿--努力学习买大鸡腿版2 小时前
hot 100 刷题记录(1)
数据结构·python·算法
努力中的编程者3 小时前
二叉树(C语言底层实现)
c语言·开发语言·数据结构·c++·算法
宵时待雨3 小时前
C++笔记归纳14:AVL树
开发语言·数据结构·c++·笔记·算法
爱编码的小八嘎4 小时前
C语言完美演绎5-3
c语言
山川行4 小时前
关于《项目C语言》专栏的总结
c语言·开发语言·数据结构·vscode·python·算法·visual studio code
呜喵王阿尔萨斯4 小时前
C and C++ code
c语言·开发语言·c++
星辰徐哥4 小时前
C语言游戏开发:Pygame、SDL、OpenGL深度解析
c语言·python·pygame